bbfu has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
(tye)Re4: Tied %SIG
by tye (Sage) on May 01, 2001 at 02:21 UTC | |
(tye)Re: Tied %SIG
by tye (Sage) on May 01, 2001 at 01:12 UTC | |
by bbfu (Curate) on May 01, 2001 at 01:22 UTC | |
by tye (Sage) on May 01, 2001 at 01:41 UTC | |
by bbfu (Curate) on May 01, 2001 at 01:50 UTC | |
by tye (Sage) on May 01, 2001 at 01:58 UTC | |
| |
Re: Tied %SIG
by merlyn (Sage) on May 01, 2001 at 02:43 UTC | |
Re: Tied %SIG
by traveler (Parson) on May 01, 2001 at 00:45 UTC | |
by bbfu (Curate) on May 01, 2001 at 00:48 UTC |