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

Re: Inconsistent exit code between architectures?

Thread Previous | Thread Next
From:
Jose Luis Martinez
Date:
June 1, 2010 01:05
Subject:
Re: Inconsistent exit code between architectures?
Message ID:
4C03C4E8.1060608@capside.com
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.com

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About