Overriding object methods (sometimes)

Ovid publiustemp-londonpm at yahoo.com
Tue Feb 26 15:16:03 GMT 2008


--- Bradley Dean <bjdean at bjdean.id.au> wrote:

> But I only want my child-class methods to be called when I call them.

In other words, if LWP::Request calls a method, it should be calling
the method that it really expects?

> My solution was to check the caller and respond appropriately, for
> instance:

Untested, but this feels a touch cleaner to me:

  sub request {
      my ( $caller ) = caller();

      # preserve context and don't update the call stack
      goto &LWP::UserAgent::request unless $caller->isa(__PACKAGE__);
      if ( my $code = $caller->can('request') ) {
          return shift->_wrapper( $code, @_ );
      }
      else {
          # squeal like a pig 'cuz our base class shouldn't change
      }
  }

Still, without seeing what you're really trying to do, I would think
this is better:

  sub request {
     my $self = shift;
      my ( $caller ) = caller();
      if ( $caller->isa(__PACKAGE__) ) {
          # handle local logic
      }
      $self->SUPER::request(@_);
  }

However, even then I'd step back and try to look at the bigger picture.
 This seems like a code smell.

Cheers,
Ovid

--
Buy the book  - http://www.oreilly.com/catalog/perlhks/
Perl and CGI  - http://users.easystreet.com/ovid/cgi_course/
Personal blog - http://publius-ovidius.livejournal.com/
Tech blog     - http://use.perl.org/~Ovid/journal/


More information about the london.pm mailing list