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":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.