4/27/2007

Embed Perl into C/C++

1. Compile the interpreter:

gcc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts`

`perl -MExtUtils::Embed -e ccopts -e ldopts` is used to set the options fro gcc

interp.c:
#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)
{
 PERL_SYS_INIT3(&argc,&argv,&env);
 my_perl = perl_alloc();
 perl_construct(my_perl);
 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
 perl_parse(my_perl, NULL, argc, argv, (char **)NULL);
 perl_run(my_perl);
 perl_destruct(my_perl);
 perl_free(my_perl);
 PERL_SYS_TERM();
}

2. Run the interpreter:

$interp
print "Pretty Good Perl \n";
print "10890 - 9801 is ", 10890 - 9801;

Pretty Good Perl
10890 - 9801 is 1089
$

No comments: