Here's a possible way you might go about this using just two notionally singleton classes.
I haven't addressed data storage and retrieval, although I suspect a DB solution will prove the better choice. (%CFG represents the config data regardless of where and how it was read.)
I've also assumed $WID. This is expected to hold some unique reference to the widget being added.
Consider this as the basis of an approach to a solution rather than the solution itself. I've left it very skeletal as I don't know how this might fit into any existing framework you may have.
package Widget;
sub new
{
my ($class, $rh_def_cfg) = @_;
bless { rh_def_cfg => $rh_def_cfg, ro_event => Event->new } => $cl
+ass;
}
sub do_events
{
my ($self, $WID) = @_;
my $rh_event_data = $self->get_event_data($WID);
foreach my $event (@{$rh_event_data->{events}})
{
$self->{ro_event}->$event($rh_event_data->{params});
}
}
sub get_event_data
{
# Use $WID to get type & widget
# Use type to get parameters
# Use widget to get events (based on type params)
# Include other data to be used in Event class
# Return hashref with event data
}
package Event;
sub new { my $class = shift; bless {} => $class }
sub email { ... }
sub log { ... }
sub trigger { ... }
package main;
my %CFG = get_cfg(); # DB or config file
my $ro_widget = Widget->new(\%CFG);
sub add_widget
{
# Add here, then:
$ro_widget->do_events($WID);
}
You might also consider an Aspect-Oriented solution. There's an Aspect module on CPAN which may be useful. (I don't know anything further about this module - others might).
-
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.
|