Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: The correct way to redefine a routine by jsegal
in thread The correct way to redefine a routine by punkish

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-18 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found