bliako has asked for the wisdom of the Perl Monks concerning the following question:
Hello Wise Monks,
I am running a Perl script via a Perl interpreter embedded in a C program.
I can not figure out how to:
Below, there is a simplified example slightly modified from perlembed which runs a string of code via the perl -e .. thingy, as I understand it.
The first problem is with ARGV, it is not passed on. But then again the perl -e .. thingy does not pass ARGV to -e script or does it? So, my first question is there a way to pass arguments to eval_sv(). Right now I am going a round-about way of globally loading ARGV data (using an eval_pv() again). Bear in mind that the Perl code I want to run in the script is a fully-fledged Perl script, with parsing of ARGV. I want to avoid converting it into a sub.
The second problem is with the C program catching a ctrl-c and passing it on to the Perl code currently being run. If I install a sig-handler (in Perl script) which dies on receiving SIGINT, it works and prints out the message (re: commented code). But when the "ignoring-you" sig-handler is installed, I don't even get the message to print out. But the C-application is terminated.
thanks, bliako
/* harness for embedding Perl into C modified by Bliako from https://perldoc.perl.org/perlembed.html Compile: $(perl -MConfig -e 'print $Config{cc}') embedex.c $(perl -MExtUti +ls::Embed -e ccopts -e ldopts) -o embedex 30/01/2019 */ #include <stdio.h> #include <signal.h> #include <EXTERN.h> /* from the Perl distribution */ #include <perl.h> /* from the Perl distribution */ static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ void cleanup(void); void signal_handler(int signum){ printf("got signal %d\n", signum); } int main(int argc, char **argv, char **env){ PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); printf("%s : perl_alloc()\n", argv[0]); perl_construct(my_perl); printf("%s : perl_construct()\n", argv[0]); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, 2, (char *[]){"", "-e", "1"}, (char **)N +ULL); const char perlcode[] = "#$SIG{INT} = sub { print \"Caught your ctrl-c and exiting!\"; exit(0) +; };\n" "$SIG{INT} = sub { print \"Caught your ctrl-c but ignoring it!\" };\n" "print \"$0: ARGV:\"; print ' '.$_ foreach(@ARGV); print \"\\n\";\n" "print \"my pid $$\\n\";\n" "print \"now sleeping, and you ctrl-c me\\n\";\n" "sleep(10000);\n" ; printf("%s : executing :\n%s\n", argv[0], perlcode); SV *ret = eval_pv(perlcode, FALSE); if( SvTRUE(ERRSV) ){ fprintf(stderr, "%s : eval_sv() has failed wi +th:\n%s\nfor the code:\n%s\n", argv[0], SvPVx_nolen(ERRSV), perlcode) +; cleanup(); exit(1); } printf("%s : done.\n", argv[0]); exit(EXIT_SUCCESS); } void cleanup(void){ perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Pass signals and argv from C to embedded Perl
by ikegami (Patriarch) on Jan 30, 2019 at 16:18 UTC | |
by bliako (Abbot) on Jan 31, 2019 at 11:13 UTC | |
by ikegami (Patriarch) on Feb 01, 2019 at 20:41 UTC | |
by Aldebaran (Curate) on Feb 05, 2019 at 08:14 UTC | |
Re: Pass signals and argv from C to embedded Perl
by kschwab (Vicar) on Jan 31, 2019 at 13:45 UTC | |
by bliako (Abbot) on Jan 31, 2019 at 16:15 UTC | |
by Aldebaran (Curate) on Jan 31, 2019 at 18:33 UTC | |
by bliako (Abbot) on Jan 31, 2019 at 19:04 UTC |