http://qs321.pair.com?node_id=650903

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

Hello,

i want catch compiler errors from an embedded perl interpreter.

For example:

eval_pv( "unknown_print 123;", FALSE );

prints the error message directly to STDERR.

injecting "$SIG{'__DIE__'} = 'MY::die_handler';" did not work for this case.

Is there any possibility to catch this error? Thank you.

Replies are listed 'Best First'.
Re: catch errors within C
by tilly (Archbishop) on Nov 15, 2007 at 06:00 UTC
    Inject "BEGIN{$SIG{'__DIE__'} = 'MY::die_handler';}" instead.

    The difference is that just telling it to use a die handler will set that handler after it is finished compiling when it hits that statement at run time. But putting that in a BEGIN block causes the handler to be set at compile time. (However you'd better hope that MY::die_handler has been compiled when you do that or else it won't be guaranteed to work properly. Ain't bootstrapping fun?)

Re: catch errors within C
by diotalevi (Canon) on Nov 15, 2007 at 04:23 UTC

    There are many places that are going to write directly to the *STDERR handle via the C-side Perl_debug_log variable. In all places I've noticed this goes through PerlIO. I think you can add a layer to the variable. I've never actually tried this but I'd just recommend checking out perlio and PerlIO::via. It looks like it'd be fairly easy to make a wrapper that'd just write to some alternate location.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: catch errors within C
by spacepille (Acolyte) on Nov 15, 2007 at 17:59 UTC
    thank your for your help. the BEGIN block works fine.