develooper Front page | perl.perl5.porters | Postings from June 2010

Re: Inconsistent exit code between architectures?

Thread Previous | Thread Next
Jose Luis Martinez
June 1, 2010 01:05
Re: Inconsistent exit code between architectures?
Message ID:
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. says:

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).

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:

My two cents,

Jose Luis Martinez

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About