saintmike has asked for the wisdom of the Perl Monks concerning the following question:
When setting a signal handler in a module, the problem arises that the application using the module already might have a signal handler set up for the signal in question.
So, for example, if a module FooFramework sets
but the application FooApp has already set$SIG{HUP} = sub { print "Framework handler!\n"; };
then loading module FooFramework would actually disrupt the application by nuking its signal handler.$SIG{HUP} = sub { print "FooApp handler!\n"; };
So, if FooFramework wants to play nice, it should check if there's already a SIG handler defined and, if so, stack its handler on top of it by wrapping around FooApp's handler:
which works as long as FooApp doesn't set $SIG{HUP} to 'DEFAULT' or 'IGNORE'.my $old_handler = $SIG{HUP}; $SIG{HUP} = sub { print "Framework handler!\n"; $old_handler->(@_) if defined $old_handler; }
Now, this could be checked as well, but it feels like re-inventing the wheel. I'm wondering if there's a standard mechanism for handling these cases? Like, a %SIG slot with a ref to an array of handlers? Any help appreciated.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Stacking Signal Handlers
by Anonymous Monk on Aug 26, 2010 at 03:51 UTC | |
Re: Stacking Signal Handlers
by Marshall (Canon) on Aug 27, 2010 at 11:07 UTC | |
by saintmike (Vicar) on Aug 30, 2010 at 05:34 UTC |
Back to
Seekers of Perl Wisdom