Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

catch errors within C

by spacepille (Acolyte)
on Nov 15, 2007 at 03:24 UTC ( #650903=perlquestion: print w/replies, xml ) Need Help??

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


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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://650903]
Approved by ikegami
Front-paged by diotalevi
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2020-12-04 12:05 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (58 votes). Check out past polls.