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


In reply to How bad is $SIG{__DIE__} really? by haukex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.