C
cyl
I ran into some problems when executing the sample code from perldoc
about embedding Perl in a C program. Here are my codes
---- embeddedperl.c begin ----
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
EXTERN_C void xs_init(pTHX)
{
char *file = __FILE__;
/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}
void runperl()
{
int ARGC = 2;
char *ARGV[]={"","test.pl"};
PerlInterpreter *my_perl;
PERL_SYS_INIT3(&ARGC,(char ***)&ARGV,NULL);
my_perl = perl_alloc();
perl_construct(my_perl);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_parse(my_perl, xs_init, ARGC, ARGV, (char **)NULL);
perl_run(my_perl);
perl_destruct(my_perl);
perl_free(my_perl);
PERL_SYS_TERM();
}
int main(int argc, char **argv, char **env)
{
int i=0;
for (i=0;i<2;i++)
runperl();
}
---- embeddedperl.c end ----
---- test.pl begin ----
use Cwd;
print cwd,"\n";
---- test.pl end ----
Here are the problems I got during execution
1. The loaded DLLs do not unload after the Perl interpreter is
shutdown
In my example, after perl_parse() Cwd.dll will be loaded. I
expected this dll will be unloaded after calling
perl_run() but it was not. How do I force all DLLs to be unloaded
after a script finishes?
2. perl_destruct() always throws exception. I have to comment out it
for my program to run. I suspect if it can run
without problem, maybe my 1st question can be solved.
3. After commenting out perl_destruct(), my program throws exception
after calling runperl() the 2nd time. It
actually died on perl_parse(). Since I wrap everything in a sub-
routine runperl() I thought everything starts from
scratch. However It seems not. I have no idea how come it is OK
the 1st time but not the 2nd.
Hope somebody can shed light on these. Thanks very much.
about embedding Perl in a C program. Here are my codes
---- embeddedperl.c begin ----
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
EXTERN_C void xs_init(pTHX)
{
char *file = __FILE__;
/* DynaLoader is a special case */
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
}
void runperl()
{
int ARGC = 2;
char *ARGV[]={"","test.pl"};
PerlInterpreter *my_perl;
PERL_SYS_INIT3(&ARGC,(char ***)&ARGV,NULL);
my_perl = perl_alloc();
perl_construct(my_perl);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
perl_parse(my_perl, xs_init, ARGC, ARGV, (char **)NULL);
perl_run(my_perl);
perl_destruct(my_perl);
perl_free(my_perl);
PERL_SYS_TERM();
}
int main(int argc, char **argv, char **env)
{
int i=0;
for (i=0;i<2;i++)
runperl();
}
---- embeddedperl.c end ----
---- test.pl begin ----
use Cwd;
print cwd,"\n";
---- test.pl end ----
Here are the problems I got during execution
1. The loaded DLLs do not unload after the Perl interpreter is
shutdown
In my example, after perl_parse() Cwd.dll will be loaded. I
expected this dll will be unloaded after calling
perl_run() but it was not. How do I force all DLLs to be unloaded
after a script finishes?
2. perl_destruct() always throws exception. I have to comment out it
for my program to run. I suspect if it can run
without problem, maybe my 1st question can be solved.
3. After commenting out perl_destruct(), my program throws exception
after calling runperl() the 2nd time. It
actually died on perl_parse(). Since I wrap everything in a sub-
routine runperl() I thought everything starts from
scratch. However It seems not. I have no idea how come it is OK
the 1st time but not the 2nd.
Hope somebody can shed light on these. Thanks very much.