Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by ZZamboni (Curate)
on Apr 20, 2000 at 01:39 UTC ( [id://8081]=note: print w/replies, xml ) Need Help??


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

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) { }
  • Comment on RE: RE: Setting up signal handlers for an object with access to $self
  • Download Code

Replies are listed 'Best First'.
RE: RE: RE: Setting up signal handlers for an object with access to $self
by chromatic (Archbishop) on Apr 20, 2000 at 02:41 UTC
    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://8081]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-18 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found