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

Hi ! Is there a nice way to catch compile/parse errors from a perl script in embedded perl ? the perl script is in a string, I want to see if it compiles correctly and in the case there are errors, make them available to the embedding application. I tried something similar to
// code contains the perl code (char *) // errmsg should be filled with the compile erros PerlInterpreter *myPerlCompile; /*** The Perl interpreter ***/ char *largv[] = { "", "-c", "-e", code, NULL }; int largc=4; int errFnd; initPerlInterp(); myPerlCompile = perl_alloc(); perl_construct(myPerlCompile); //PL_exit_flags |= PERL_EXIT_DESTRUCT_END; errFnd=perl_parse(myPerlCompile, xs_init, largc, largv, (char **)NULL) +; sprintf(errmsg, "%s", SvPV_nolen(ERRSV));
but this does not work (errFnd is filled in case of errors, but errmsg stays empty) I got the impression that there certainly is a better approach for this... thanx !

Replies are listed 'Best First'.
Re: embedded perl: check for compile errors without executing
by zentara (Cardinal) on Feb 05, 2010 at 16:57 UTC
    I'm just an amateur, but perlembed shows code like
    # SV* my_eval_sv(SV *sv, I32 croak_on_error) # if (croak_on_error && SvTRUE(ERRSV)) # croak(SvPVx_nolen(ERRSV));
    for errors.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku

      thanx zentara, but it seems to me that this is only available once the eval_sv has been called, i.e. the code has been executed. I would like to know about any compile errors without executing the code; similar to the command line "perl -c -e 'xxx'", but in an 'embedded way'

      I could write to file, make a system call, redirect the output to a file,.. but there is certainly a much easier more beautiful solution).

Re: embedded perl: check for compile errors without executing
by AriSoft (Sexton) on Feb 08, 2010 at 09:17 UTC

    I am experimenting my own embedded perl solution with precompile system and I really can catch syntax errors before the code is actually executed. It is also possible to run the script in a "sandbox" to catch runtime warnings.

    I think you have to split the process in two phases. Compile an execute to achieve your result. See http://www.perlmonks.org/?parent=821564;node_id=3333 for reference.

      Um, BEGIN/USE happens (is executed) before INIT/CHECK/RUN time ... some ideas Safe::World