Overriding object methods (sometimes)

Bradley Dean bjdean at bjdean.id.au
Mon Feb 25 17:25:33 GMT 2008

Greetings folks,

I've recently had cause to wrap LWP::UserAgent in a child class
which needs to override most public methods. In doing so I've hit up
against a problem - these requests call each other, but I only want my
child-class methods to be called when I call them.

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

sub request {
  my ($self, @params) = @_;
  my ($package, $filename, $line) = caller();
  if ( $package eq 'LWP::UserAgent' ) {
    return $self->SUPER::request(@params);
  else {
    return $self->_wrapper(\&LWP::UserAgent::request, @params)

Where &_wrapper contains my local logic (it's taking a reference
to &LWP::UserAgent::request because it's going to call it later on).

This works fine, and isn't overly ugly, but I'm wondering if I've missed or
forgotten a nicer way to do it. :)

Any thoughts?



Bradley Dean
Software Engineer - http://bjdean.id.au/
Email: bjdean at bjdean.id.au Skype: skype at bjdean.id.au
Mobile(Aus): +61-413014395 Mobile(UK): +44-7846895073

More information about the london.pm mailing list