Ilya Zakharevich

NAME

AutoLoader - load functions only on demand

SYNOPSIS

    package FOOBAR;
    use Exporter;
    use AutoLoader;
    @ISA = (Exporter, AutoLoader);

DESCRIPTION

This module tells its users that functions in the FOOBAR package are to be autoloaded from auto/$AUTOLOAD.al. See "Autoloading" in perlsub and AutoSplit.

__END__

The module using the autoloader should have the special marker __END__ prior to the actual subroutine declarations. All code that is before the marker will be loaded and compiled when the module is used. At the marker, perl will cease reading and parsing. See also the AutoSplit module, a utility that automatically splits a module into a collection of files for autoloading.

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 perl subroutines for this package in /usr/local/lib/perl5/auto/POSIX/*.al. The .al file is named using the subroutine name, sans package.

Package Lexicals

Package lexicals declared with my in the main block of a package using the AutoLoader will not be visible to auto-loaded functions, due to the fact that the given scope ends at the __END__ marker. A module using such variables as package globals will not work properly under the AutoLoader.

The vars pragma (see "vars" in perlmod) may be used in such situations as an alternative to explicitly qualifying all globals with the package namespace. Variables pre-declared with this pragma will be visible to any autoloaded routines (but will not be invisible outside the package, unfortunately).

AutoLoader vs. SelfLoader

The AutoLoader is a counterpart to the SelfLoader module. Both delay the loading of subroutines, but the SelfLoader accomplishes the goal via the __DATA__ marker rather than __END__. While this avoids the use of a hierarchy of disk files and the associated open/close for each routine loaded, the SelfLoader suffers a disadvantage in the one-time parsing of the lines after __DATA__, after which routines are cached. SelfLoader can also handle multiple packages in a file.

AutoLoader only reads code as it is requested, and in many cases should be faster, but requires a machanism like AutoSplit be used to create the individual files.

CAVEAT

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.