in reply to Checking the success of eval
My favourite gotcha with eval is if you set a die
handler then the die handler is called even inside the
eval! In fact I consider this a bug in perl - as does
the author of "perldoc die"
Eg
This prints Caught by die handler: Oops.$SIG{__DIE__} = sub { print "Caught by die handler: $_[0]"; exit }; eval { die "Oops\n"; }; print "Eval returned error: $@" if $@;
The fix goes like this :-
$SIG{__DIE__} = sub { print "Caught by die handler: $_[0]"; exit }; eval { local $SIG{__DIE__}; die "Oops\n"; }; print "Eval returned error: $@" if $@;Which returns Eval returned error: Oops as anyone would always want.
|
---|
Replies are listed 'Best First'. | |
---|---|
(tye)Re: Checking the success of eval
by tye (Sage) on Apr 21, 2001 at 02:04 UTC | |
by ncw (Friar) on Apr 21, 2001 at 02:31 UTC |
In Section
Meditations