How can I make an AUTOLOAD sub declaration in a child class not hide the AUTOLOAD sub of parent?
I use AUTOLOAD functions to automagically generate sub routines for accessing data from my objects. However, if a class which inherits from my class declares an AUTOLOAD subroutine then they will hide my AUTOLOAD.
Is there a way to do this? Am I terribly confused?
Here is some code to illustrate the problem:
Class B inherits from class A. Class A uses
AUTOLOAD to generate a baz() method. Class B
uses AUTOLOAD to generate a foo() function.
The Problem:
Class B's AUTOLOAD routine captures the baz()
call, and I can't figure out how to pass that
to Class A's AUTOLOAD function.
package A; sub bar { return "BAR"; } sub AUTOLOAD { my (@objpath) = split(/::/, $AUTOLOAD); my $fname = pop @objpath; return if $fname eq 'DESTROY'; if ( $fname eq 'baz' ) { my $subr = <<EOSUB; sub $AUTOLOAD { return "FOO"; } EOSUB eval $subr; goto &$AUTOLOAD; } return; } package B; @ISA = qw( A ); sub new { return bless {}, shift; } sub AUTOLOAD { my (@objpath) = split(/::/, $AUTOLOAD); my $fname = pop @objpath; return if $fname eq 'DESTROY'; if ( $fname eq 'foo' ) { my $subr = <<EOSUB; sub $AUTOLOAD { return "FOO"; } EOSUB eval $subr; goto &$AUTOLOAD; } return; } package main; my $b = new B; print "foo: ", $b->foo, "\n"; print "bar: ", $b->bar, "\n"; print "baz: ", $b->baz, "\n";
In reply to AUTOLOAD functions & inheritence by LunaticLeo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |