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

Hi All,

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

#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() ; }
(PS: Here's my environment info:) perl, version 5.005_03 built for MSWin32-x86 I'm building on Win2k

Many thanks in advance

  • Comment on Embedding Perl in a C application - syntax errors cause program crash
  • Download Code

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
    Building using Visual Studio 6, on Windows 2000 perl being "This is perl, v5.6.1 built for MSWin32-x86-multi-thread" the code runs exactly how you might think it would. It's ActiveState's binary distribution.

    Bad libaries print me outThe use of non-existant libraries in a 'use' statement in the evaluated code in the C program print the following error at execution time":
    ERROR: Can't locate Pooey.pm in @INC (@INC contains: C:/Perl/lib C:/Pe +rl/site/lib .) at (eval 1) line 1. BEGIN failed--compilation aborted at (eval 1) line 1.
    Using a statement in the C program, such as "die('Mommie!');" prints out:
    ERROR: Mommie! at (eval 1) line 1.
    What compiler? Perhaps upgrade perl/ActiveState if possible to the latest?

        --jb

    Update: Attempted to clarify statements per AM's request
      JB, You could please elaborate ?. Your cryptic response has left my head spinning... What's with the "Bad libaries print me out:" line ? Thanks
        Those errors were the results of the C program that I ran with the embedded perl code suggested in the line. Perhaps it should read "The use of non-existant libraries in a 'use' statement in the evaluated code in the C program print the following error."

        Likewise, the other should probably read "using a die statement in the embedded code in the C program prints":

        Sorry, was a tad tired at the time ;)

            --jb