From: Perl 5 Porters Date: Mon, 30 Sep 1996 04:54:37 +0000 (-0400) Subject: perl 5.003_06: lib/AutoLoader.pm X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e14baed2d4b81bc0e4a34d42e726ea5b8c11fd7d;p=p5sagit%2Fp5-mst-13.2.git perl 5.003_06: lib/AutoLoader.pm Date: Mon Sep 9 09:29:44 1996 From: Gisle Aas Subject: Re: problem with 'die' and UserAgent > This is a patch to the AutoLoader.pm (from 5.003) that fixes the problem: This is a better patch (no need to test for /::DESTROY$/ twice): Date: Mon, 30 Sep 1996 00:54:37 -0400 From: Spider Boardman The test and patches for AutoLoader were also non-functional, since the regexp context (curpm) was still being clobbered by the filename manipulations: Date: Sun, 06 Oct 1996 16:15:07 +0200 From: Gisle Aas Subject: Re: Can't locate auto/U/autosplit.ix It would IMHO be much better if the AutoLoader exported the AUTOLOAD() function. With an exported AUTOLOAD() we would not have to inherit from AutoLoader, and we would avoid these problems. This patch tries to explain the behavior of AutoLoader instead by updating its documentation. --- diff --git a/lib/AutoLoader.pm b/lib/AutoLoader.pm index e24e139..7d781d1 100644 --- a/lib/AutoLoader.pm +++ b/lib/AutoLoader.pm @@ -11,7 +11,7 @@ AutoLoader - load functions only on demand package FOOBAR; use Exporter; use AutoLoader; - @ISA = (Exporter, AutoLoader); + @ISA = qw(Exporter AutoLoader); =head1 DESCRIPTION @@ -35,6 +35,31 @@ F. The autoloader will look for perl subroutines for this package in F. The C<.al> file is named using the subroutine name, sans package. +=head2 Loading Stubs + +The B module provide a special import() method that will +load the stubs (from F file) of the calling module. +These stubs are needed to make inheritance work correctly for class +modules. + +Modules that inherit from B should always ensure that they +override the AutoLoader->import() method. If the module inherit from +B like shown in the I section this is already taken +care of. For class methods an empty import() would do nicely: + + package MyClass; + use AutoLoader; # load stubs + @ISA=qw(AutoLoader); + sub import {} # hide AutoLoader::import + +You can also set up autoloading by importing the AUTOLOAD function +instead of inheriting from B: + + package MyClass; + use AutoLoader; # load stubs + *AUTOLOAD = \&AutoLoader::AUTOLOAD; + + =head2 Package Lexicals Package lexicals declared with C in the main block of a package using @@ -60,7 +85,8 @@ can also handle multiple packages in a file. B only reads code as it is requested, and in many cases should be faster, but requires a machanism like B be used to create the -individual files. +individual files. The B will invoke B +automatically if the B is used in a module source file. =head1 CAVEAT @@ -69,30 +95,37 @@ subroutine may have a shorter name that the routine itself. This can lead to conflicting file names. The I package warns of these potential conflicts when used to split a module. +Calling foo($1) for the autoloaded function foo() might not work as +expected, because the AUTOLOAD function of B clobbers the +regexp variables. Invoking it as foo("$1") avoids this problem. + =cut AUTOLOAD { my $name = "auto/$AUTOLOAD.al"; - $name =~ s#::#/#g; + # Braces used on the s/// below to preserve $1 et al. + {$name =~ s#::#/#g} + my $save = $@; eval {require $name}; if ($@) { - # The load might just have failed because the filename was too - # long for some old SVR3 systems which treat long names as errors. - # If we can succesfully truncate a long name then it's worth a go. - # There is a slight risk that we could pick up the wrong file here - # but autosplit should have warned about that when splitting. - if ($name =~ s/(\w{12,})\.al$/substr($1,0,11).".al"/e){ - eval {require $name}; - } - elsif ($AUTOLOAD =~ /::DESTROY$/) { - # eval "sub $AUTOLOAD {}"; + if (substr($AUTOLOAD,-9) eq '::DESTROY') { *$AUTOLOAD = sub {}; - } - if ($@){ - $@ =~ s/ at .*\n//; - croak $@; + } else { + # The load might just have failed because the filename was too + # long for some old SVR3 systems which treat long names as errors. + # If we can succesfully truncate a long name then it's worth a go. + # There is a slight risk that we could pick up the wrong file here + # but autosplit should have warned about that when splitting. + if ($name =~ s/(\w{12,})\.al$/substr($1,0,11).".al"/e){ + eval {require $name}; + } + if ($@){ + $@ =~ s/ at .*\n//; + croak $@; + } } } + $@ = $save; $DB::sub = $AUTOLOAD; # Now debugger know where we are. goto &$AUTOLOAD; }