Embedding Perl in C: problem with modules



Hello All.

I have a problem. I need persistent Perl interpreter in my C program and a function, that can run my scripts and can make addition things with data from the script. In the function I carry out set of perlapi functions: perl_construct(), perl_parse(), perl_run and perl_destruct(). But Perl interpreter doesn’t run modules (I pass it as string in perl_parse) after first using of the function.

//For instance, it is simple source that shows this problem:
#include <EXTERN.h>
#include <perl.h>
#include <stdio.h>
//it is simple fun, that run filename with module -Mstrict
void execScript (char* filename, PerlInterpreter *my_perl, int *err);

static PerlInterpreter *my_perl;
int main (int argc, char **argv, char **env)
{
int err = 0;
char *file_name = "testperl.pl";
char *command_line[] = {"","-Mstrict” ","testperl2.pl"};
int i = 0;
my_perl = perl_alloc();
//in this line will fail, because in testperl.pl we have ‘$ab’ without ‘our’
execScript (file_name, my_perl, &err);
//but these lines will be without fail, that is module ‘strict’ doesn’t run
for (i = 0; i < 2 ; i++)
{
execScript (file_name, my_perl, &err);
}
//similar as above
for (i = 0; i < 3; i++)
{
PERL_SET_CONTEXT( my_perl );
PL_perl_destruct_level = 1;
perl_construct(my_perl);
perl_parse(my_perl, NULL , 3, command_line, (char **)NULL);
perl_run(my_perl);
PL_perl_destruct_level = 1;
perl_destruct(my_perl);
}
perl_free(my_perl);
return 0;
}
void execScript (char *script, PerlInterpreter *my_perl, int* err)
{
char *command_line[] = {"","-Mstrict",script};
int i = 0;
*err = 0;
if (my_perl == NULL)
{
printf ("\nmy_perl = NULL\n");
*err = -1;
return;
}
PERL_SET_CONTEXT( my_perl );
PL_perl_destruct_level = 1;
perl_construct(my_perl);
*err = perl_parse(my_perl, NULL , 3, command_line, (char **)NULL);
if (*err != 0)
{
printf ("\nError in perl_parse\n");
PL_perl_destruct_level = 1;
perl_destruct(my_perl);
return;
}
perl_run(my_perl);
PL_perl_destruct_level = 1;
perl_destruct(my_perl);

return;
}
testperl.pl is:
$ab = 100;
print “testperl.pl. ab = $ab \n”;
testperl2.pl is:
$PQR = “Hello”;
print “$PQR from testperl2.pl \n”;

I don’t know what exactly do with my problem. I haven’t any ideas.
Any help is much appreciated.

Thanks & Regards in advance
Ivan Gromov

.



Relevant Pages

  • Re: Use perl in apache without #!/path/to/perl
    ... stud1174@di.uoa.gr (Kostas Hatzikokolakis) wrote: ... outlined there what to do to set up running scripts. ... _not_ where the Perl interpreter is. ... <spectacular error. ...
    (comp.lang.perl.misc)
  • Re: Stored Procedure Error
    ... I'm setting this up through the GUI, not scripts. ... Here is one of the offending procedures: ... @CustID int= Null, ... > Looking for a SQL Server replication book? ...
    (microsoft.public.sqlserver.replication)
  • Re: Does Linux not allow use of chmod u+s
    ... To execute scripts with root privaliges as a user you need to ... ]If Linux does not allow that, how do suid and super allow it? ... ]int main ... how buffer overflow attacks are born. ...
    (comp.os.linux.security)
  • Re: Convert CHAR YYYYMMDD to mm/dd/yyyy
    ... I know how to generate CREATE TABLE scripts but is there a fast way to ... DECLARE @dt INT; ... Whenever possible please post enough code to reproduce your problem. ... State what version of SQL Server you are using and specify the content ...
    (comp.databases.ms-sqlserver)
  • Bug in #! processing
    ... options for scripts and such. ... int main(int ac, char **av) ... root@tytus> uname -a ...
    (freebsd-current)