LunaticLeo has asked for the wisdom of the Perl Monks concerning the following question:
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Other Options
by chromatic (Archbishop) on Mar 30, 2000 at 08:03 UTC | |
|
Re: AUTOLOAD functions & inheritence
by chromatic (Archbishop) on Mar 30, 2000 at 01:51 UTC | |
by Anonymous Monk on Mar 30, 2000 at 02:55 UTC | |
by Anonymous Monk on Mar 30, 2000 at 03:05 UTC | |
|
Re: AUTOLOAD functions & inheritence
by btrott (Parson) on Mar 30, 2000 at 02:12 UTC | |
by Anonymous Monk on Mar 30, 2000 at 02:43 UTC | |
by Anonymous Monk on Mar 30, 2000 at 04:02 UTC |