package GoodStuff; use Exporter; use AutoLoader; @ISA = qw(Exporter AutoLoader);
The AutoLoader module provides a standard mechanism for
delayed loading of functions stored in separate files on disk.
Each file has the same name as the function (plus a .al), and
comes from a directory named after the package (with the auto/ directory).
For example, the function named GoodStuff::whatever()
would be loaded from the file
auto/GoodStuff/whatever.al.
A module using the AutoLoader should have the special marker __END__
prior to the actual subroutine declarations. All code before this
marker is loaded and compiled when the module is used. At the marker,
Perl stops parsing the file.
When a subroutine not yet in memory is called, the AUTOLOAD
function attempts to
locate it in a directory relative to the location of the module file itself. As
an example, assume POSIX.pm is located in
/usr/local/lib/perl5/POSIX.pm. The AutoLoader will look for
the corresponding subroutines for this package in
/usr/ local/lib/perl5/auto/POSIX/*.al.
Lexicals declared with my in the
main block of a package using the AutoLoader will not be visible to autoloaded
functions, because the given lexical scope ends at the
__END__
marker. A module using such
variables as file-scoped globals will not work properly under the AutoLoader.
Package globals must be used instead. When running under use
strict
, the use vars
pragma may be employed in such
situations as an alternative to explicitly qualifying all globals with the
package name. Package variables predeclared with this pragma will be accessible
to any autoloaded routines, but of course will not be invisible outside the
module file.
The AutoLoader is a counterpart to the SelfLoader module. Both delay the loading
of subroutines, but the SelfLoader accomplishes this by storing the subroutines
right there in the module file rather than in separate files elsewhere. While
this avoids the use of a hierarchy of disk files and the associated I/O for each
routine loaded, the SelfLoader suffers a disadvantage in the one-time parsing of
the lines after __DATA__
, after which
routines are cached. The SelfLoader can also handle multiple packages in a
file.
AutoLoader, on the other hand, only reads code as it is requested, and in many cases should be faster. But it requires a mechanism like AutoSplit to be used to create the individual files.
On systems with restrictions on file name length, the file corresponding to a subroutine may have a shorter name than the routine itself. This can lead to conflicting filenames. The AutoSplit module will warn of these potential conflicts when used to split a module.
See the discussion of autoloading in Chapter 5. Also see the AutoSplit module, a utility that automatically splits a module into a collection of files for autoloading.