Hi,
there are examples available on how to allocate a Perl_Interpreter
in a C program and work with it, e.g. let it execute a script.
In these examples, the Perl_Interpreter is always called
my_perl and i noticed that compiling fails when it is given
a different name.
Is it possible to give it a different name?
Yes.
$ cat interp.c
#include <EXTERN.h>
#include <perl.h>
int
main(int argc, char **argv, char **env)
{
PerlInterpreter *your_perl;
int exitstatus;
your_perl = perl_alloc();
if (!your_perl)
exit(1);
perl_construct(your_perl);
exitstatus = perl_parse(your_perl, NULL, argc, argv, (char **)NULL);
if (!exitstatus) {
exitstatus = perl_run(your_perl);
}
perl_destruct(your_perl);
perl_free(your_perl);
return exitstatus;
}
$ gcc -c `/opt/perl-5.8.2/bin/perl -MExtUtils::Embed -e ccopts` interp.c
$ gcc interp.o `/opt/perl-5.8.2/bin/perl -MExtUtils::Embed -e ldopts`
$ ./a.out -e 'print "Hello World!\n"'
Hello World!
$
The above also works when you make your_perl a static variable, declared
outside of main, or a global variable declared outside of main.
Is it possible to allovate TWO Perl_Interpreters at a time
and let them execute code?
$ cat interp.c
#include <EXTERN.h>
#include <perl.h>
#define MAX_PERLS 4
int
main(int argc, char **argv, char **env)
{
PerlInterpreter *your_perl[MAX_PERLS];
int i;
for (i = 0; i < MAX_PERLS; i++)
{
your_perl
= perl_alloc();
if (!your_perl)
exit(1);
perl_construct(your_perl);
}
for (i = 0; i < MAX_PERLS; i++)
{
perl_parse(your_perl, NULL, argc, argv, (char **)NULL);
perl_run(your_perl);
}
for (i = 0; i < MAX_PERLS; i++)
{
perl_destruct(your_perl);
perl_free(your_perl);
}
return 0;
}
$ gcc -c `/opt/perl-5.8.2/bin/perl -MExtUtils::Embed -e ccopts` interp.c
$ gcc interp.o `/opt/perl-5.8.2/bin/perl -MExtUtils::Embed -e ldopts`
$ ./a.out -e 'print "Hello World!\n"'
Hello World!
Hello World!
Hello World!
Hello World!
$
Martien