Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: changing object's methods at runtime

by bart (Canon)
on May 22, 2003 at 02:25 UTC ( [id://259965]=note: print w/replies, xml ) Need Help??


in reply to Re: changing object's methods at runtime
in thread changing object's methods at runtime

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? :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://259965]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-19 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found