954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Embeding perl in C?

I have a question about embedding Perl scripts in C. I wrote most of the program in C and a portion of it in Perl. As of right now, to call the Perl script I'm using pipe(), fork() and dup2() to create another process and have the Perl application write its stdout to the pipe. This works fine, but I don't want to have to lug around the Perl script whenever I want to run my program. I understand that I can add a Perl interpreter to my C code, but as far as I can see this leaves me I the same boat as I was previously unless I rewrite my Perl code so that it fits the perlapi.

I was wondering if it is possible to just compile my Perl script into the executable binary and use the embedded Perl interpreter to handle it? Thanks!

dcarrawa
Newbie Poster
3 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Google suggested http://docstore.mik.ua/orelly/perl/prog3/ch21_04.htm

There's a chapter about this topic in Programming Perl IIRC, but my copy is in another state, so I can't look it up atm.

Trentacle
Junior Poster in Training
72 posts since Dec 2010
Reputation Points: 110
Solved Threads: 20
 

Thanks for the reply,
The page Google suggests doesn't quite answer my question though. I would prefer to not have to rewrite any of my Perl script yet still have it within the executable binary.

dcarrawa
Newbie Poster
3 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

I know Perl is famous for "batteries included", but C is famous for rolling them little batteries right along, as well. ;)

If you're going to have to re-write it, consider just using C.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Perhaps you should read it more carefully, as there are definitely examples of running Perl code from within C on that page.

That said, it is a bit unusual to call Perl from C and not the other way around. If it's reasonable to do so, another option is to compile your C code into a library and use it from Perl -- of course, this raises some more compatibility issues.

I think you can also compile Perl code into to a native library by linking it with the Perl interpreter somehow, then use that library directly... but I don't know the process.

The "simple way" is to call upon the Perl interpreter directly to do the hard stuff. Since you wish to avoid that approach, everything else is going to be quite a bit more involved.

Trentacle
Junior Poster in Training
72 posts since Dec 2010
Reputation Points: 110
Solved Threads: 20
 

Sorry, I think you're misinterpreting what I want to do, or maybe I wasn't very clear. I know there are examples of Perl code running from C on that page.
Regardless, I've figured it out, thanks for the help though.

dcarrawa
Newbie Poster
3 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: