Just create a subclass, that's what I'd do too. As for the custom method: provide an extra field in the original object (that is indeed the most dangerous aspect of it), and assign the sub ref to it. The method itself, as per
robartes, needn't do anything other than fetching that sub ref and calling it.
Now, looking at the source of Apache::FakeRequest, it looks like it is a blessed hash ref, with your named parameters as hash items. So you can just call new() with this additional named parameter, to have it inserted.
$req=Apache::FakeRequest::ViceRaid->new( method::lookup_uri => sub {
# ... insert what you want it to do
});
That was easy.
n.b. "
method::lookup_uri" is still a bareword as far as Perl is concerned. I just used an outstanding value to reduce the risk of field name clashes.
The source for the subclass can be pretty much as in robartes note. You just need to fill in the method call, which could look something like this:
sub lookup_uri {
$_[0]->{method::lookup_uri}->(@_);
}
and now you have quite a generic subclass, all objects having their own custom method. You could generalize it, adding more methods, and calling SUPER::
method if the necessary sub ref is missing. For example:
sub lookup_uri {
($_[0]->{method::lookup_uri} || $_[0]->can(SUPER::lookup_uri))->(@
+_);
}
p.s. Caveat: I haven't tested this, as I don't have a mod_perl installation handy. Heh.
Update: I have. It seems to work well. Who needs mod_perl for this kind of test, anyway? :)
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.