I've been thinking of writing an extension for alarm and $SIG{ALRM} that would grant super powers... er, that is, would let you queue multiple alarms (and handlers), either sequentially, or by shortest-first.

The problem comes in when using this with "legacy" code that sets $SIG{ALRM} itself. As I've got a single, custom alarm handler that should be called every time, if someone sets $SIG{ALRM} manually, it fubar's my whole system.

So, basically, what I need to do is tie the %SIG variable and catch any attempts to set $SIG{ARLM} and execute my own code instead. That's fine, I can do that. My real problem is that, even though my tied STORE method gets called, $SIG{ALRM} is changed to the new value anyway. :-(

Some example code:

#!/usr/bin/perl -w tie %SIG, Foo; alarm(2); $SIG{ALRM} = sub { print "Alarm called!\n" }; sleep 10; package Foo; sub TIEHASH { my $foo = {}; print "Hash tied.\n"; return bless $foo, shift; } sub STORE { return if $REENTRANT; my ($this, $key, $value) = @_; print "Stored $value in \$SIG{$key}.\n"; return unless($key eq 'ALRM'); # Ignore everything but ALRM's ++$REENTRANT; $SIG{ALRM} = sub { print "Ugly Hack(tm) method.\n" }; --$REENTRANT; #$_[1] = sub { print "Parameter change method.\n" }; #return sub { print "Return value method.\n" }; }

The Ugly Hack™ method does work but neither of the other methods do. I'd like to have a more elegant solution, though I'm not sure if one exists.

So, I guess my question is this: Is there another WTDI? I've tried everything I can think of, as well as everything I can find documentation on. I can go with the UH™ method if I have to.

Also, though I understand that it will probably always be relegated to the realm of Deep Magic, does anyone else think that provisions should be made to allow us to tie certain magic Perl variables, such as %SIG, in a relatively straight-forward manner? Or is it generally agreed that doing so would just encourage more madmen to do crazy things?

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.


In reply to Tied %SIG by bbfu

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.