Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: The correct way to redefine a routine

by jsegal (Friar)
on Apr 02, 2008 at 22:27 UTC ( [id://678057]=note: print w/replies, xml ) Need Help??


in reply to The correct way to redefine a routine

Like many things in Perl, there is more than one way to do it. Others in this thread have suggesting subclassing, which sometimes works, sometimes doesn't. For instance, suppose you are using class A which instantiates class B, and it is the method in class B that you need to override, even for your use of class A. Though you could in theory override class A to have the method in question instantiate your overriding class B, perl does let you do change class B's method directly.

Be warned that doing this sort of thing may be fraught with danger, especially if you upgrade the class whose method you are overriding, or if other classes you use call that method and depend on the old behavior. But if you go in with your eyes open and are aware of these issues, here are a couple of other ways to meet this goal:

package main; sub new_foobar_magic { my ($self,@otherargs) = @_; ... } *Foo::Bar::do_magic = \&new_foobar_magic; # or in one swoop: *Foo::Bar::do_magic = sub { my ($self,@otherargs) = @_; ... } # if you want to call the old routine: my $old_foobar_magic = \&Foo::Bar::do_magic; *Foo::Bar::do_magic = sub { my ($self,@otherargs) = @_; ... adjust things or log things ... $old_foobar_magic->($self,@args); };


--JAS

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://678057]
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-20 16:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found