Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

CProgramTrends
FlashNewz
DevWebPro








Perl In C Part 1

By Bryan Young
Expert Author
Article Date: 2010-07-29

Perl, which is an extremely flexible and fluid language, in itself written in C, and as such it is easy to incorporate it into your own programs. This is not always necessary for most programs, as it is easy to call a Perl program in its own process by using popen. For persistent programs, this can be a major time-saver as Perl will only be loaded into memory once.

First thing to do is to find the libraries you need from your Perl installation. This can be done from the command line.

perl -Mconfig -e 'print $Config{archlib}'


This will give you the location of the EXTERN.h and perl.h libraries. You then just need to include them in your source code. Once we have this, we simple add a perl interpreter to our code. Below, you can see an example Larry Wall included in the book "Programming Perl"


#include /* from the Perl distribution */
#include /* from the Perl distribution */

static PerlInterpreter my_perl; /*** The Perl Interpreter **/

int main(int argc, char **argv, char **env)
{
my_perl = perl_alloc();
perl_construct(my_perl);
perl_parse(my_perl, NULL, argc, argv, (char **)NULL);
perl_run(my_perl);
perl_destruct(my_perl);
perl_free(my_perl);
}


You can see that when you run this particular program, the arguments are the perl statements that are executed. These statements could be replaced with a file just as easily. It is also possible to run a perl subroutine from your code if you replace


perl_run(my_perl);


with


call_argv("subroutine", G_DISCARD | G_NOARGS, NULL);


In this example I run the subroutine "subroutine." The G_DISCARD | G_NOARGS are flags stating that we can ignore the output of of the subroutine and there are no arguments to pass.

About the Author:
Bryan Young is a staff writer for WebProNews.



Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

C Programming Trends is an iEntry, Inc. ® publication - All Rights Reserved Privacy Policy and Legal