Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I want to add Perl scripting to a C application I wrote. I have managed to embed the Perl intepreter (see code below), and implemented some rudimentary error checking. For e.g. If a non-existing (or mis-spelled) module is specified in the Perl script, it prints an error message and gracefully exits. However, when a Perl syntax error occurs, it crashes my entire application.
The code follows below
(PS: Here's my environment info:) perl, version 5.005_03 built for MSWin32-x86 I'm building on Win2k#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl = NULL ; void load_perl(){ char *embedding[] = { "", "-e", "0" }; if (my_perl == NULL){ my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 3, embedding, NULL); perl_run(my_perl); } } char *eval_perl(char *user_code, char **error){ char *code = (char *)calloc(strlen(user_code) + 512, sizeof(char)) + ; SV *ret = NULL ; SV *err = NULL ; strcat(code, user_code) ; ret = eval_pv(user_code, FALSE) ; free(code) ; err = get_sv("@", TRUE) ; if (SvTRUE(err)){ *error = SvPV_nolen(err) ; return NULL ; } return SvPV_nolen(ret) ; } void unload_perl(){ if (my_perl != NULL){ perl_destruct(my_perl); perl_free(my_perl); my_perl = NULL ; } } main (int argc, char **argv){ char code[1024] ; char *error = NULL ; char *ret = NULL ; sprintf(code, "%s", "die('!!') ;") ; load_perl() ; ret = eval_perl(code, &error) ; if (error != NULL){ printf("ERROR: %s\n", error) ; } else{ printf("SUCCESS: %s\n", ret) ; } unload_perl() ; }
Many thanks in advance
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Embedding Perl in a C application - syntax errors cause program crash
by JayBonci (Curate) on May 06, 2002 at 08:55 UTC | |
by Anonymous Monk on May 09, 2002 at 21:01 UTC | |
by JayBonci (Curate) on May 09, 2002 at 21:07 UTC |