Graham has asked for the wisdom of the Perl Monks concerning the following question:

AUTOLOAD is failing to get called for sub calls of the form Xxxx->new when the class Xxxx has never been seen before.
Calls of Xxxx::new invoke AUTOLOAD correctly.

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:

#!/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";
Thanks,
Graham

Replies are listed 'Best First'.
Re: AUTOLOAD fails in perl 5.8.0
by PodMaster (Abbot) on Oct 16, 2002 at 10:04 UTC
    same here on win32, broken in both 5.8 and 5.7.3, but works on 5.6

    I don't know where in the perl source this taken care of, or where it's tested, but apparently this scenario is not being tested, so some serious source-diving/patching needs to be done.

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: AUTOLOAD fails in perl 5.8.0
by bart (Canon) on Oct 16, 2002 at 12:29 UTC
    I think you should bring this up on P5P . Only those people know if this is a bug or a feature (AKA a fixed bug), and if it's indeed a bug, only they can do something about it.

    p.s. Sorry I couldn't give you a more detailed link, the server is currently not working well. I think the email address is <p5p@perl.org>, but don't take my word for it.

      Ive been meaning to get bleadperl up and running and test this code but its been too busy today. If and or when I do, if it still exists then I will file a bug report. (although maybe a bug report is needed either way. It looks like there a variety of reasons to release a 5.8.1 (ie: horrible memory leak when using sockets under default conditions)

      However the p5p list is actually perl5-porters@perl.org

      --- demerphq
      my friends call me, usually because I'm late....

        The problem still exists in the perl@17882 snapshot. In the meantime I have filed a bug report from my stable 5.8.0 version under Ticket#17967
        -Graham