rocketboy has asked for the wisdom of the Perl Monks concerning the following question:

hi everybody.

i have a slight problem here.

my task involve calling c from perl which inturn has perl embedded in it.

this is my c code which i call 1.c

#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; int main(int argc, char **argv, char **env) { char *myargv[] ={"","Random.pl"}; 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,myargv,(char**)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }

this is my random.pl

package cryzone; print "Hello"; print " i am in random"; print $cryzone::a; 1;

this is my file test.pl from which i call 1.exe

use cryzone; $cryzone::a=1; print $cryzone::a; system("1.exe"); $cryzone::a=2; print $cryzone::a;

and this is my package cryzone

package cryzone; $a=0; 1;

on running i am never able to access $cryzone::a inside random.pl even though ihave declared it as belonging to cryzone.

how can this be solved.

the output is

1Hello i am in random2

whereas the desired output is

1Hello i am in random12

please help me.

Edited by planetscape - added code tags around output

Replies are listed 'Best First'.
Re: embedding perl
by Tanktalus (Canon) on Apr 02, 2006 at 17:28 UTC

    I'm not exactly a Perl <--> C expert, but this doesn't look like one of those situations. What you're doing is running a program from inside your perl script via system. That launches a new process, with a new perl interpreter, with no shared memory between them. Running a script via system is not anywhere near the same thing as running perl code via do, use, or require. It starts of brand new and fresh.

    What you probably want to do is embed cryzone in your application (via use), or possibly share some memory (IPC::Shareable, perhaps), or fork (but not exec). Not really sure what is suitable for your situation from your original post.