El 31/05/2010 15:23, Tim Bunce escribió: > > I've occasionally used code like this: > > # exit with specific code when dieing from an uncaught exception > $SIG{__DIE__} = sub { > return if $^S; # we're inside an eval so ignore this one > warn @_; > exit 42; > }; > > Perhaps something like that is worth adding to the exit() docs. Tim, I've been caught with more unexpectedness doing that: $^S can be undef. BEGIN { eval { die 'X' } } If the __DIE__ handler is installed: $^S will be undef, and you will exit 42, even if the die was surrounded in eval {} !!! Modules like Math::BigInt, or the perl debugger seem to do initializations in this way so your __DIE__ exits on their CONTROLLED exceptions In retrospective, I see that documentation from die mislead me a bit (maybe it deserves a docpatch), and I didn'nt realize (although there is a link to the doc on $^S) that I could be bitten. http://perldoc.perl.org/functions/die.html says: [QUOTE] Although this feature was to be run only right before your program was to exit, this is not currently so: the $SIG{__DIE__} hook is currently called even inside eval()ed blocks/strings! If one wants the hook to do nothing in such situations, put 1. die @_ if $^S; as the first line of the handler (see $^S in perlvar). [/QUOTE] That statement is not quite true, as you won't propagate an exception in a BEGIN block. You need an extra "die if (not defined $^S)". Note: this all came from development of Nagios::Plugin::DieNicely. There's a bit of background here: http://www.pplusdomain.net/cgi-bin/blosxom.cgi/2010/05/28 My two cents, Jose Luis Martinez jlmartinez@capside.comThread Previous | Thread Next