shemp has asked for the wisdom of the Perl Monks concerning the following question:
I have 2 classes, one inherits from the other, call them Parent and Child (Child inherits from Parent), and Parent has an AUTOLOAD method, and so does Child. The problem is that an instance of Child will never call Parent::AUTOLOAD, because of the call heirarchy. For instance:
So, when i have an instance of Child, and call parent_method() on it, there is no parent_method() found in Child, then its not found in Parent, then its not found in UNIVERSAL. But then, Child::AUTOLOAD() is found, and is invoked. But that AUTOLOAD doesnt know about parent_method(), so does nothing, and Parent::AUTOLOAD() is not invoked. To get around this, im thinking i should add a line to the end of Child::AUTOLOAD() like this:package Parent; ... sub AUTOLOAD { my $self = shift @_; (my $method = $AUTOLOAD) =~ s/^.*:://; return if $method eq "DESTROY"; if ( $method eq 'parent_method' ) { # do something } } ... package Child; ... sub AUTOLOAD { my $self = shift @_; (my $method = $AUTOLOAD) =~ s/^.*:://; return if $method eq "DESTROY"; if ( $method eq 'child_method' ) { # do something } }
Is this the proper way to do this, or am i missing something obvious?package Child; ... sub AUTOLOAD { my $self = shift @_; (my $method = $AUTOLOAD) =~ s/^.*:://; return if $method eq "DESTROY"; if ( $method eq 'child_method' ) { # do something } ... else { $self->SUPER::AUTOLOAD($method, @_); } }
Thanks much, as always.
Update: I found that i need to modify Parent::AUTOLOAD() too, so im thinking there is a better way than this, but heres the parent modification:
This had to be done because $Parent::AUTOLOAD is not set like for a normal call to AUTOLOAD, so i had to do something to get Parent::AUTOLOAD() to know what method to use.package Parent; ... sub AUTOLOAD { my $self = shift @_; my $method; if ( defined $AUTOLOAD ) { ($method = $AUTOLOAD) =~ s/.*:://; } else { $method = shift @_; } ... }
Update 2:
I came up with a better way to do this, which doesnt require modifying Parent::AUTOLOAD():
The only thing about this one is that Perl will search through Parent and its parents, before calling Parent::AUTOLOAD(), which is a little more time consuming, but i am going to have the AUTOLOADS screw with the symbol tables and install AUTOLOAD'ed methods after their first invocation, which will speed things up.# in Child::AUTOLOAD() # instead of $self->SUPER::AUTOLOAD($method, @_); my $super_method = 'SUPER::' . $method; $self->$super_method(@_);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: AUTOLOAD cascade
by blokhead (Monsignor) on Oct 13, 2004 at 16:38 UTC | |
|
Re: AUTOLOAD cascade
by Ven'Tatsu (Deacon) on Oct 13, 2004 at 16:51 UTC | |
|
Re: AUTOLOAD cascade
by JediWizard (Deacon) on Oct 13, 2004 at 16:28 UTC | |
|
Re: AUTOLOAD cascade
by diotalevi (Canon) on Oct 13, 2004 at 17:14 UTC | |
by BrowserUk (Patriarch) on Oct 13, 2004 at 17:33 UTC | |
by dragonchild (Archbishop) on Oct 13, 2004 at 17:39 UTC | |
by ysth (Canon) on Oct 13, 2004 at 18:43 UTC | |
by BrowserUk (Patriarch) on Oct 13, 2004 at 18:12 UTC | |
by dragonchild (Archbishop) on Oct 13, 2004 at 18:16 UTC |