简体   繁体   中英

Execute Perl script from C program

I currently have a program that I have written in C, and I am interested in integrating into it a fairly lengthy Perl script that someone else wrote that accomplishes a similar function but does so in a different way. I am not interested in translating the entire Perl script into C by hand.

The way I see it there are a few different options:

  1. Because the script communicates using stdin and stdout, I know that I could use a system call, eg system("perl script.pl") and pipe the input and output, but I feel like this solution is not portable and doesn't feel very elegant.
  2. I could embed the Perl interpreter into my program in order to process the script, however, I feel like this would probably be bulky and would be overkill for executing a script that does not change. This does offer the advantage that instead of piping the input and output I could call the Perl subroutines directly.
  3. I could use some sort of perl-to-c translation tool (like perlcc ) to compile the Perl source into C source at compile time and then just include it into the rest of my program. Although at first glance I thought this would be the best option there seems to be a wide consensus in the Perl community (or at least the part of it that google showed me) that doing this sort of translation is not a very good idea (this might just be for speed considerations, which are not a problem at all for me). This also (might?) allow me to call the Perl subroutines from within C code which would be a huge plus.
  4. Rewrite the script by hand.

So my question is this: which of these options (or none of the above) is the most elegant and portable? I understand that calling this Perl program from within a C program is already a messy hack to start with, but the least messy solution would probably be best.

Thank you!

Of the solutions:

  • Is hacky but certainly doable. You probably don't want to use system() itself, as you'll want to read and write the output likely from pipe() calls. IE, you'll need to create the necessary sockets, fork and then execute the perl script in the child and read the output from the parent. Though this is probably less code, it's not necessarily easier to get right than:
  • embedding perl: takes a bit to get set up, but once it's set up it's actually really slick. I wrote an entire embedded perl system for Net-SNMP which works wonderfully. Once you get it set up it's pretty amazing how little effort it takes to get it up and running. That being said, it's pretty scary code.
  • perl-to-c may be nice in theory, but I wouldn't expect it to work all the time. If you only have to do this once, it might be worth a shot. But you'll need a good testing suite to ensure the output is what you expect.
  • rewriting the script by hand: honestly, this is certainly the "cleanest" solution. If the script isn't going to change in the future, this is likely the way to go unless it's a daunting task because it's actually that big (you don't say).

Each of the above methods has advantages and disadvantages. It depends on what you're concerned about:

  • Doing it frequently (ie, the perl is going to keep changing): use embedded perl
  • the least amount of time: then it depends on how big the script is. Either 1, 2, 3 or 4 is likely to be the fastest depending on what the script looks like. If it's long, then skip #4. If it's really short, then go straight to number 4.

In the end, if it were me, I'd select either embedding or translating depending on whether it is going to change again.

Now... I wrote a whole system for geocaching that I actually want to translate into Qt because I think it'll be more portable. What's my right choice? Translating. But it's going to take a lot of time, hence the reason I haven't done it yet.

If the perl script isn't very long, may be you should rewrite it in C and forget perl.

Otherwise, i would use a system call. If portability is a concern, you can compile the perl script with perlcc in order to get an exe file which will be fully portable and will save you from installing perl in every machine you want to run this program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM