I am new to C++ coding.
I wanted to execute a Perl script (that controls an equipment) inside a C++ Visual Studio Win32 application. I was wondering what would be the most optimized 开发者_JS百科approach in this regards?
- Call system function and execute that particular Perl file.
- Create some kind of DLL and call that DLL inside my C++ code.
Option 2 I have never used, but I am willing to learn and implement if that's the best way.
I believe the "most optimized" approach is to embed Perl in your application.
Your second option is not really feasible, AFAIK. Since Perl is not compiled, your only other option would be to invoke the interpreter (e.g. through a system()
or exec()
-family call), as per your first option.
Now, there is quite a difference between embedding an interpreter in your code, and writing one line of code to call an external program, so you have to consider exactly how much optimization you need. The performance difference might even be minimal, so I strongly suggest that you try the easy way first, measuring your success.
I agree with Oystein that you're not likely to find a good way to compile the perl script into a shared library.
However, spawning perl from your C++ program isn't the only other option. You could also pipe data between the two programs, use a socket to pass data between them, and other IPC methods.
In my backup tool I once tried invoking system("perl script.pl")
, but it ran very slow on Windows (20-30 calls per second or so even though script.pl in a test benchmark did nothing). On Linux it was much, much quicker.
精彩评论