Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: BEGIN, END, eval and die.

by autark (Friar)
on Nov 19, 2000 at 03:00 UTC ( #42379=note: print w/ replies, xml ) Need Help??


in reply to BEGIN, END, eval and die.

The $@ variable is only set by the eval code if it somehow fails. In your last example, you don't use eval to "catch" this die, so $@ will not be set.

However, even if you used eval, this will not work for your END-block. You might want to try to install a $SIG{__DIE__} handler instead. Using that to set a variable or something similar, so that you can access the information within the END-block.

$SIG{__DIE__} = sub { our @reason = @_ }; eval { die "Let's Try to die." }; END { our @reason; if (@reason) { print "We caught a call to die: @reason\n"; } }

Autark.


Comment on Re: BEGIN, END, eval and die.
Select or Download Code
Replies are listed 'Best First'.
Re: Re: BEGIN, END, eval and die.
by zzspectrez (Hermit) on Nov 19, 2000 at 12:30 UTC

    I did not realize the $@ variable is tied just to the eval code. This makes sense.
    Using a $SIG{__DIE__} handler seems to be exactly what I was looking for. Will this work on the windows platform as well?

    THANKS!!
    zzspectrez

      Yes -- but that's only because it's a Perl-generated signal that never touches the OS. So Windows' lack of signal support doesn't even get brought into play.

      perl -e 'print "Just use $^X$\"$]!$/"'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://42379]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2015-07-30 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls