Graham has asked for the wisdom of the Perl Monks concerning the following question:
I have an example where having a LATER line in the code of the form: Xxxx::foobar makes AUTOLOAD trigger correctly for the prior line of Xxxx->new. Commenting out this line makes the earlier line fail. This seems bizarre behavour. Does this mean something is occuring at compile time?
This behavour occurs on perl 5.8.0 on IRIX and Linux platforms but does not occur on perl 5.6.1 on Win32 and Linux platforms or perl 5.005_03 on IRIX and Win32 platforms
This is creating a big problem for us as we have hundreds of calls of this form most of which rely on AUTOLOAD to load the class and method(s) on demand to keep the memory footprint down.
Here is an example that demonstrates the problem, note the lines to be uncommented near the bottom:
Thanks,#!/usr/local/bin/perl -w package UNIVERSAL; sub AUTOLOAD { print "UNIVERSAL::AUTOLOAD=$AUTOLOAD\n" unless $AUTOLOAD =~ /DEST +ROY/; if ($AUTOLOAD =~ /^(.*)::(.*)$/ ) { my ($class,$sub) = ($1,$2); if ($sub eq 'new') { my $self = {xxx => 1}; bless $self, $class; return $self; } else { return "$AUTOLOAD **results**"; } } } #test forms of AUTOLOAD package main; my $n; #the following succeeeds but gets message: # Use of inherited AUTOLOAD for non-method foo::anysub() # is deprecated... my $xx=foo::anysub(); print ref $xx ? "object class:$xx\n" : "sub:$xx\n"; #the following succeeeds and returns an object of class foo my $xx=foo->new(); print ref $xx ? "object class:$xx\n" : "sub:$xx\n"; # *** IF THE LINES FURTHER ON ARE COMMENTED THIS FAILS WITH MESSAGE: # Can't locate object method "new" via package "bar" (perhaps # you forgot to load "bar"?)... # *** OTHERWISE IT WORKS WHY?? *** my $xx=bar->new(); print ref $xx ? "object class:$xx\n" : "sub:$xx\n"; # *** IF YOU UNCOMMENT THESE 2 LINES THE ABOVE WORKS *** #my $xx=bar::new(); #print ref $xx ? "object class:$xx\n" : "sub:$xx\n"; print "Finished\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: AUTOLOAD fails in perl 5.8.0
by PodMaster (Abbot) on Oct 16, 2002 at 10:04 UTC | |
|
Re: AUTOLOAD fails in perl 5.8.0
by bart (Canon) on Oct 16, 2002 at 12:29 UTC | |
by demerphq (Chancellor) on Oct 16, 2002 at 14:51 UTC | |
by Graham (Deacon) on Oct 17, 2002 at 09:37 UTC | |
by demerphq (Chancellor) on Oct 17, 2002 at 12:44 UTC |