eXile has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've got a class (say AA) that inherits from another class (say BB). I'ld like to 'proxy' all methods from AA to BB after I modified them slightly (take out 1 argument and process it, so if somebody calls AA->method , AA should catch this, and eventyally call BB->method. I've tried to use AUTOLOAD for this, but that catches undefined methods, not the methods that I want to override. Hope this makes sense (<monthy python>my brain hurts ....</monthy python>) , if not I'll add some code: test.pl:
use AA; AA->print("extrainfo","hi");
AA.pm:
package AA; use base 'BB'; sub AUTOLOAD { my ($self,$extrainfo,@args ) = @_; print "Via AA:\n"; print "extrainfo: $extrainfo \n"; $superfunk = "SUPER::$AUTOLOAD"; $self->$superfunk(@args); } 1;
BB.pm
package BB; sub print { my ($self,$arg) = @_; print "I'm BB saying: $arg\n"; } 1;
AA.pm
package AA;
if I run test.pl I'd like it to say
Via AA: extrainfo: extrainfo I'm BB saying: hi
but it says:
I'm BB saying: extrainfo

Replies are listed 'Best First'.
Re: AUTOLOAD and inheritance
by Arunbear (Prior) on Dec 04, 2004 at 00:31 UTC
    If AA is going to forward all method calls to BB (i.e. method foo in AA calls method foo in BB, and likewise for method bar e.t.c), then there's no point in subclassing BB. Just define AUTOLOAD in AA so that it does whatever preprocessing you want before calling the corresponding method in BB.

    Decoration and inheritance are orthogonal techniques.

Re: AUTOLOAD and inheritance
by revdiablo (Prior) on Dec 04, 2004 at 00:21 UTC

    I see two problems. The small problem is that $AUTOLOAD contains the name of the class, so you have to strip that off first.

    The big problem is that it appears the inheritance is working before AUTOLOAD gets a chance to run. One way around this, which is pretty ugly, is to avoid setting up the inheritence until AUTOLOAD is run. I'm not sure how workable this solution is, but at least it's something to chew on.

    You can see this by changing AA.pm to:

    package AA; require BB; sub AUTOLOAD { my ($self,$extrainfo,@args) = @_; local @ISA = 'BB'; my ($method) = (split /::/, $AUTOLOAD)[-1]; $superfunk = "SUPER::$method"; $self->$superfunk(@args); } 1;

    Update: an alternative way to accomplish a similar goal, without using AUTOLOAD, would go something like this:

    package AA; use base 'BB'; for my $meth (qw(print flop frizzle)) { *{ $meth } = sub { my ($self, $extra, @args) = @_; my $meth = "SUPER::$meth"; $self->$meth(@args); }; } 1;

    Which just goes to show that AUTOLOAD is not the only way to dynamically create methods.