ActivePerl Documentation
|
NAMEAutoLoader - load subroutines only on demand
SUPPORTED PLATFORMS
SYNOPSIS
package Foo;
use AutoLoader 'AUTOLOAD'; # import the default AUTOLOAD subroutine
package Bar;
use AutoLoader; # don't import AUTOLOAD, define our own
sub AUTOLOAD {
...
$AutoLoader::AUTOLOAD = "...";
goto &AutoLoader::AUTOLOAD;
}
DESCRIPTIONThe AutoLoader module works with the AutoSplit module and the
To use AutoLoader, the author of a module has to place the
definitions of subroutines to be autoloaded after an AutoLoader implements an AUTOLOAD subroutine. When an undefined
subroutine in is called in a client module of AutoLoader,
AutoLoader's AUTOLOAD subroutine attempts to locate the subroutine in a
file with a name related to the location of the file from which the
client module was read. As an example, if POSIX.pm is located in
/usr/local/lib/perl5/POSIX.pm, AutoLoader will look for perl
subroutines POSIX in /usr/local/lib/perl5/auto/POSIX/*.al, where
the Once this process completes for a given function, it is defined, so future calls to the subroutine will bypass the AUTOLOAD mechanism.
Subroutine StubsIn order for object method lookup and/or prototype checking to operate
correctly even when methods have not yet been defined it is necessary to
``forward declare'' each subroutine (as in The AutoSplit and AutoLoader modules automate the creation of forward declarations. The AutoSplit module creates an 'index' file containing forward declarations of all the AutoSplit subroutines. When the AutoLoader module is 'use'd it loads these declarations into its callers package. Because of this mechanism it is important that AutoLoader is always
Using AutoLoader's AUTOLOAD SubroutineIn order to use AutoLoader's AUTOLOAD subroutine you must explicitly import it:
use AutoLoader 'AUTOLOAD';
Overriding AutoLoader's AUTOLOAD SubroutineSome modules, mainly extensions, provide their own AUTOLOAD subroutines. They typically need to check for some special cases (such as constants) and then fallback to AutoLoader's AUTOLOAD for the rest. Such modules should not import AutoLoader's AUTOLOAD subroutine. Instead, they should define their own AUTOLOAD subroutines along these lines:
use AutoLoader;
use Carp;
sub AUTOLOAD {
my $sub = $AUTOLOAD;
(my $constname = $sub) =~ s/.*:://;
my $val = constant($constname, @_ ? $_[0] : 0);
if ($! != 0) {
if ($! =~ /Invalid/ || $!{EINVAL}) {
$AutoLoader::AUTOLOAD = $sub;
goto &AutoLoader::AUTOLOAD;
}
else {
croak "Your vendor has not defined constant $constname";
}
}
*$sub = sub { $val }; # same as: eval "sub $sub { $val }";
goto &$sub;
}
If any module's own AUTOLOAD subroutine has no need to fallback to the AutoLoader's AUTOLOAD subroutine (because it doesn't have any AutoSplit subroutines), then that module should not use AutoLoader at all.
Package LexicalsPackage lexicals declared with The
AutoLoader vs. SelfLoaderThe AutoLoader is similar in purpose to SelfLoader: both delay the loading of subroutines. SelfLoader uses the AutoLoader only reads code as it is requested, and in many cases should be faster, but requires a mechanism like AutoSplit be used to create the individual files. the ExtUtils::MakeMaker manpage will invoke AutoSplit automatically if AutoLoader is used in a module source file.
CAVEATSAutoLoaders prior to Perl 5.002 had a slightly different interface. Any
old modules which use AutoLoader should be changed to the new calling
style. Typically this just means changing a require to a use, adding
the explicit On systems with restrictions on file name length, the file corresponding to a subroutine may have a shorter name that the routine itself. This can lead to conflicting file names. The AutoSplit package warns of these potential conflicts when used to split a module. AutoLoader may fail to find the autosplit files (or even find the wrong
ones) in cases where
SEE ALSOthe SelfLoader manpage - an autoloader that doesn't use external files.
|