Re: SUPER::AUTOLOAD(@_) -- will this work? With $AUTOLOAD?
- From: Dodger <el.dodgero@xxxxxxxxx>
- Date: Wed, 19 Dec 2007 16:49:45 -0800 (PST)
On Dec 19, 4:01 pm, Dodger <el.dodg...@xxxxxxxxx> wrote a question and
then shortly thereafter answered it himself.
It appears to work. I made a small test script like so:
use strict;
my $bar = new Bar;
$bar->{thing} = 1;
print "\$bar->thing = ", $bar->thing, "\n";
print "Trying Bar AUTOLOAD specifically\n";
$bar->do_bar_AUTOLOAD(1, 2, 3);
print "\n";
print "Trying inherited AUTOLOAD\n";
$bar->do_foo_AUTOLOAD(4, 5, 6);
print "\n";
package Foo;
use strict;
our $AUTOLOAD;
sub new {
my $pkg = shift;
my $class = ref $pkg || $pkg;
my $obj = {};
bless $obj, $class;
return $obj
}
sub AUTOLOAD {
my $obj = shift;
my $method = $AUTOLOAD;
$method =~ s/^.*://;
print "AUTOLOAD of Foo\n";
print "method: $method\n";
print "args: ", join('. ', @_), "\n";
}
package Bar;
use strict;
use vars('@ISA');
BEGIN {
push @ISA,('Foo');
}
our $AUTOLOAD;
sub AUTOLOAD {
my $obj = shift;
my $method = $AUTOLOAD;
$method =~ s/.*://;
print "method: $method\n";
if ($method eq 'do_bar_AUTOLOAD') {
print "AUTOLOAD of Bar\n";
print "method: $method\n";
print "args: ", join('. ', @_), "\n";
}
elsif (exists $obj->{$method}) {
return "(Bar sez) $obj->{$method}";
}
else {
$obj->SUPER::AUTOLOAD(@_);
}
}
and my results came back as:
method: thing
$bar->thing = (Bar sez) 1
Trying Bar AUTOLOAD specifically
method: do_bar_AUTOLOAD
AUTOLOAD of Bar
method: do_bar_AUTOLOAD
args: 1. 2. 3
Trying inherited AUTOLOAD
method: do_foo_AUTOLOAD
AUTOLOAD of Foo
method:
args: 4. 5. 6
method: DESTROY
AUTOLOAD of Foo
method:
args:
So it appears that this trick will not work unless I specifically
assign to Foo::AUTOLOAD before calling it...
I tried it two ways:
else {
$SUPER::AUTOLOAD = $AUTOLOAD;
$obj->SUPER::AUTOLOAD(@_);
}
does NOT work, which seems odd...
because
else {
$Foo::AUTOLOAD = $AUTOLOAD;
$obj->SUPER::AUTOLOAD(@_);
}
DOES work...
So, I know how to do it now, but... I'm still unclear on one part...
Can anyone explain why I can't set the $AUTOLOAD in Foo with
$SUPER::AUTOLOAD instead of $Foo::AUTOLOAD if I can successfully call
SUPER::AUTOLOAD() instead of having to specify Foo::AUTOLOAD() ?
.
- References:
- Prev by Date: SUPER::AUTOLOAD(@_) -- will this work? With $AUTOLOAD?
- Next by Date: [OT] What's with the wack job that thinks James Bond is after him?
- Previous by thread: SUPER::AUTOLOAD(@_) -- will this work? With $AUTOLOAD?
- Next by thread: [OT] What's with the wack job that thinks James Bond is after him?
- Index(es):
Relevant Pages
|
|