Good point (about the signal being per-process). But with a little more work, you could make your signal handler
"intelligent" to call the handlers for all the objects that
have registered. Using your sample code as a base:
package Obj;
# Class variable
%handlers=();
sub new {
my $class = shift;
my $self = { @_ };
bless $self, $class;
$self->set_signal_handlers;
$self;
}
sub name {
shift->{'name'};
}
sub set_signal_handlers {
my $self = shift;
if (!%handlers) {
# Class signal handler, only set the first time
$SIG{USR1} = \&class_handle_USR1;
}
# You could index by $self->name, or by any other identifier
$handlers{$self} = sub { $self->handle_USR1() };
}
sub handle_USR1 {
my $self = shift;
print "handling USR1 for ", $self->name, "\n";
}
sub class_handle_USR1 {
foreach (keys %handlers) {
&{$handlers{$_}}();
}
}
package main;
my $obj1 = new Obj(name => 'obj1');
my $obj2 = new Obj(name => 'obj2');
while (1) { }