Ok, well in that case you could have a Handler::Colors that has returns those 80 objects from its new() method (though I would probably call it something else, then, like init()):
package 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});
}
}
updated: added return @handlers line
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
push @event_handlers,"Handler::$_";
line needs to be replaced by
push @event_handlers,"Handler::$_"->init();
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.