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
$SIG{HUP} = sub { print "Framework handler!\n"; };
but the application FooApp has already set
$SIG{HUP} = sub { print "FooApp handler!\n"; };
then loading module FooFramework would actually disrupt the application by nuking its signal handler.
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:
my $old_handler = $SIG{HUP};
$SIG{HUP} = sub {
print "Framework handler!\n";
$old_handler->(@_) if defined $old_handler;
}
which works as long as FooApp doesn't set $SIG{HUP} to 'DEFAULT' or 'IGNORE'.
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.