updated: added return @handlers linepackage Handler::Colors; use YAML qw(Load); # or some other module to handle config-like files # set up a bunch of event handlers that # send email depending on colour sub init { my $colors = Load('/color/config'); my @handlers; while (my ($color,$email) = each %$colors) { @handlers = Handler::Colors->new( { color => $color, email => $email } ); } return @handlers; #update: added this line! } # create handler object sub new { my $class = shift; return bless { @_ },$class; } # check for my color and send email if found. sub handle { my ($self,$event) = @_; if ($self->{color} eq $event->color()) { send_mail($self->{email}); } }
update2: To clarify: this code plugs into the code in my post above, so you can have more of these classes for each different type of action you need to perform. All that's changed there is that the
line needs to be replaced bypush @event_handlers,"Handler::$_";
push @event_handlers,"Handler::$_"->init();
In reply to Re^3: OT: Design question
by Joost
in thread OT: Design question
by dragonchild
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |