Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

RE: Setting up signal handlers for an object with access to $self

by btrott (Parson)
on Apr 20, 2000 at 00:26 UTC ( [id://8075]=note: print w/replies, xml ) Need Help??


in reply to Setting up signal handlers for an object with access to $self

I like this quite a lot... only problem being that signals are unique to processes, not objects, so if you try to instantiate more than 1 of your objects and attach it to a signal handler, only the last object attached will actually have the signal handler installed.

For example, if I have this code:

package Obj; 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; $SIG{USR1} = sub { $self->handle_USR1() } } sub handle_USR1 { my $self = shift; print "handling USR1 for ", $self->name, "\n"; } package main; my $obj1 = new Obj(name => 'obj1'); my $obj2 = new Obj(name => 'obj2'); while (1) { }
and I run this little script, then give it a USR1 signal, the only object that's ever going to catch that signal will be obj2. See what I'm saying?

All the same, it's a nice little method. :)

Replies are listed 'Best First'.
RE: RE: Setting up signal handlers for an object with access to $self
by ZZamboni (Curate) on Apr 20, 2000 at 01:39 UTC
    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) { }
      Because I like CODE refs way too much:
      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} = $self->handle_USR1(); } # skip a few sub handle_USR1 { my $self = shift; return sub { print "handling USR1 for ", $self->name, "\n"; } }
      That fits all of my definitions of beautiful.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://8075]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2024-04-15 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found