haukex has asked for the wisdom of the Perl Monks concerning the following question:
Fellow Monks,
The thread Getting the Behavior of the "file open or die" Pragma but with a Program Pause got me thinking about this. The documentation of $SIG{__DIE__} says this:
Having to even think about the $^S variable in your exception handlers is simply wrong. $SIG{__DIE__} as currently implemented invites grievous and difficult to track down errors. Avoid it and use an END{} or CORE::GLOBAL::die override instead.
That exact paragraph has been present in perlvar since 1999, and although it sounds like a pretty dire warning, currently it's at the bottom of that section of documentation. I've seen $SIG{__DIE__} used in lots of places, and I've used it myself a few times, mostly to rewrite the die message and add additional information, and sometimes to send error messages to syslog as well. Although the code seems to work fine, now I'm wondering about that. Is $SIG{__DIE__} really as bad as the documentation says and should I be avoiding it for these uses? As always, your wisdom would be appreciated.
Update: Some examples of how I've used $SIG{__DIE__}:
local $SIG{__DIE__} = sub { die "[".gmtime." UTC] (PID $$) FATAL ".shi +ft }; # - OR - # for my $i (0..10) { local $SIG{__DIE__} = sub { my $e = shift; $e=~s/\.?\n?$//; die "$ +e (item $i)\n" }; } # - OR - # local $SIG{__DIE__} = sub { chomp(my $m = shift); syslog('err','Error: + %s',$m) };
What say you, have I sinned?
P.S. I should say that I am aware of the issues of how $SIG{__DIE__} interacts with eval and that specifically hasn't been a problem for me so far. I'm asking mostly about the other, more general, warning in the documentation I quoted above.
Thanks,
-- Hauke D
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How bad is $SIG{__DIE__} really?
by Corion (Patriarch) on Oct 11, 2016 at 10:34 UTC | |
by haukex (Archbishop) on Oct 26, 2016 at 08:47 UTC | |
|
Re: How bad is $SIG{__DIE__} really?
by ateague (Monk) on Oct 11, 2016 at 13:59 UTC | |
|
Re: How bad is $SIG{__DIE__} really?
by Discipulus (Canon) on Oct 11, 2016 at 10:51 UTC | |
|
Re: How bad is $SIG{__DIE__} really?
by stevieb (Canon) on Oct 11, 2016 at 13:47 UTC | |
by tye (Sage) on Oct 12, 2016 at 03:36 UTC | |
by stevieb (Canon) on Oct 12, 2016 at 12:36 UTC | |
|
Re: How bad is $SIG{__DIE__} really?
by haukex (Archbishop) on Oct 26, 2016 at 08:31 UTC |