The semantics of $SIG{__DIE__} are fairly annoying.

The way it steps over previous signals, the way you can't do 'local $SIG{__DIE__}' at the top of your module and get what you want, etc.

After a brief discussion of this on the Minneapolis PM list, I mulled over a comment by Ken Williams. I can't remember what the comment was but it prompted me to write some weird code.

I'm not posting the code here. Rather I'll describe it.

If you want it, write to me at autarch@urth.org. If people actually find it useful I'll CPAN-ize it.

I myself can't figure out if its a neat hack or a useless neat hack.

There are two modules that I've written. First of all, I know the names are wrong. If this does go on CPAN then better names are welcome.

The first, Sig::Lexical, makes it possible to (sort of) have signal handlers that only apply to code in a given package. The problem with this code is that it requires all code that wants to set $SIG{__DIE__} or $SIG{__WARN__} to go through this module.

If some code just does a $SIG{__DIE__} = sub { foo } then its all over.

Anyway, the code lets you do this:

  Sig::Lexical::my_sig( __DIE__ => \&some_sub );

Now, if code dies in the package that made the call, then &some_sub is called. Otherwise it dies normally.

To turn this off you can call:

  Sig::Lexical::unmy_sig( __DIE__ => 1 );

Ok, that function name sucks, I know.

Now for the really weird stuff. The other module is called Sig::Lexical::Paranoid.

If you use this module before any others, than _all_ assignments to $SIG{__DIE__} and $SIG{__WARN__} are treated as if they were done via Sig::Lexical::my_sig

This means that if some rude module just blindly assigns to $SIG{__DIE__}, this doesn't overwrite existing signal handlers, except in the rude module.

One thing I could add to this would be an option to make the signals more like 'local'. This means that when a die occurs, the module would travel back the stack in caller until either:

A) it finds a handler set via Sig::Lexical::my_sig for a calling package
or
B) it runs out of stack and does a normal die/warn.

This would work with the paranoid code too.

I could also make it so that you could say that certain signal handlers _always_ took precedence (at least in the paranoid version), no matter what else was set.

So is this useful or am I just a wacko?

-dave


In reply to Weirdness with $SIG{__DIE__} (and __WARN__) by autarch

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.