You can use callback (I hope I haven't twisted them into
somthing else) that will do what you need. I use
them in OOP and assign a default sub via a method, that is
I create a method to return the default sub
routine (action/code) if one isn't passed into the mehtod
that uses the sub.
Here is the method that returns the default code (sub):
sub default_code {
my ($self,$extra) = @_;
my $sub = sub {
my ($extra,$optional,$optional) = @_;
# do something
};
return $sub;
}
The method that might use this sub allows for a callback
or overriding sub that is passed in via the method options.
$object->display( { my_code => sub {
my ($hashref) = shift;
foreach (keys %$hashref) {
# more code
}
};
}
);
Then in the display method
sub display {
my ($self,$args) = @_;
# $args->{'my_code'} has our "custom code"
# here is where we determine which one to use
my $action = $args->{'my_code'} || $self->default_code;
$action->($args);
}
I don't know if this good style or not, but it has been
working well for what I needed.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.