#!/usr/local/bin/perl -w package UNIVERSAL; sub AUTOLOAD { print "UNIVERSAL::AUTOLOAD=$AUTOLOAD\n" unless $AUTOLOAD =~ /DESTROY/; 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";