Re: SUPER::AUTOLOAD(@_) -- will this work? With $AUTOLOAD?



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() ?
.



Relevant Pages

  • autoload with a block?
    ... I've just discovered Ruby's autoload feature. ... autoload:Foo, 'foo' ... Foo.autoload:Bar ... ... In that simplest form, it would at least centralize all my loading stuff in ...
    (comp.lang.ruby)
  • Re: initializint a CLOS class instance
    ... (defmethod frob ((obj foo)) ... (defmethod frob ((obj bar)) ...
    (comp.lang.lisp)
  • Re: methods and functions, instances and classes
    ... instance and the function (note the "(pointers to) ... ... return Method(self, obj) ... foo = Foo ... Looking up 'bar' on 'foo': ...
    (comp.lang.python)
  • Re: Objektmethoden auflisten/Frage zu SUPER
    ... Das kann man AUTOLOAD machen lassen. ... einen eigenen Wrapper drumrum möchte. ... Damit sind Funktionen lokal im Package gebunden und Aufrufe via foo() funktionieren noch, per Methoden-Lookup sind sie aber nicht mehr zu finden. ...
    (de.comp.lang.perl.misc)
  • Re: Class_eval vs. reopening class
    ... does autoloading by overriding ... If Foo wasn't available, Rails will actually try to autoload a file ...
    (comp.lang.ruby)