SEARCH
NEW RPMS
DIRECTORIES
ABOUT
FAQ
VARIOUS
BLOG

BotDetect - Real-Time Bot Detection API
 
 

MAN page from Old RedHat 5.X perl-5.004-4.i386.rpm

AutoLoader

Section: Perl Programmers Reference Guide (3)
Updated: perl 5.004, patch 04
Index 

NAME

AutoLoader - load subroutines only on demand 

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;    }
 

DESCRIPTION

The AutoLoader module works with the AutoSplit module and the__END__ token to defer the loading of some subroutines until they areused rather than loading them all at once.

To use AutoLoader, the author of a module has to place thedefinitions of subroutines to be autoloaded after an __END__ token.(See the perldata manpage.) The AutoSplit module can then be run manually toextract the definitions into individual files auto/funcname.al.

AutoLoader implements an AUTOLOAD subroutine. When an undefinedsubroutine in is called in a client module of AutoLoader,AutoLoader's AUTOLOAD subroutine attempts to locate the subroutine in afile with a name related to the location of the file from which theclient module was read. As an example, if POSIX.pm is located in/usr/local/lib/perl5/POSIX.pm, AutoLoader will look for perlsubroutines POSIX in /usr/local/lib/perl5/auto/POSIX/*.al, wherethe .al file has the same name as the subroutine, sans package. Ifsuch a file exists, AUTOLOAD will read and evaluate it,thus (presumably) defining the needed subroutine. AUTOLOAD will thengoto the newly defined subroutine.

Once this process completes for a given funtion, it is defined, sofuture calls to the subroutine will bypass the AUTOLOAD mechanism. 

Subroutine Stubs

In order for object method lookup and/or prototype checking to operatecorrectly even when methods have not yet been defined it is necessary to``forward declare'' each subroutine (as in sub NAME;). Seethe section on SYNOPSIS in the perlsub manpage. Such forward declaration creates ``subroutinestubs'', which are place holders with no code.

The AutoSplit and AutoLoader modules automate the creation of forwarddeclarations. The AutoSplit module creates an `index' file containingforward declarations of all the AutoSplit subroutines. When theAutoLoader module is `use'd it loads these declarations into its callerspackage.

Because of this mechanism it is important that AutoLoader is alwaysused and not required. 

Using AutoLoader's AUTOLOAD Subroutine

In order to use AutoLoader's AUTOLOAD subroutine you mustexplicitly import it:

    use AutoLoader 'AUTOLOAD';
 

Overriding AutoLoader's AUTOLOAD Subroutine

Some 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 theselines:

    use AutoLoader;    use Carp;
    sub AUTOLOAD {        my $constname;        ($constname = $AUTOLOAD) =~ s/.*:://;        my $val = constant($constname, @_ ? $_[0] : 0);        if ($! != 0) {            if ($! =~ /Invalid/) {                $AutoLoader::AUTOLOAD = $AUTOLOAD;                goto &AutoLoader::AUTOLOAD;            }            else {                croak "Your vendor has not defined constant $constname";            }        }        *$AUTOLOAD = sub { $val }; # same as: eval "sub $AUTOLOAD { $val }";        goto &$AUTOLOAD;    }
If any module's own AUTOLOAD subroutine has no need to fallback to theAutoLoader's AUTOLOAD subroutine (because it doesn't have any AutoSplitsubroutines), then that module should not use AutoLoader at all. 

Package Lexicals

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

The vars pragma (see the section on vars in the perlmod manpage) may be used in suchsituations as an alternative to explicitly qualifying all globals withthe package namespace. Variables pre-declared with this pragma will bevisible to any autoloaded routines (but will not be invisible outsidethe package, unfortunately). 

AutoLoader vs. SelfLoader

The AutoLoader is similar in purpose to SelfLoader: both delay theloading of subroutines.

SelfLoader uses the __DATA__ marker rather than __END__.While this avoids the use of a hierarchy of disk files and theassociated open/close for each routine loaded, SelfLoader suffers astartup speed disadvantage in the one-time parsing of the lines after__DATA__, after which routines are cached. SelfLoader can alsohandle multiple packages in a file.

AutoLoader only reads code as it is requested, and in many casesshould be faster, but requires a machanism like AutoSplit be used tocreate the individual files. the ExtUtils::MakeMaker manpage will invokeAutoSplit automatically if AutoLoader is used in a module sourcefile. 

CAVEATS

AutoLoaders prior to Perl 5.002 had a slightly different interface. Anyold modules which use AutoLoader should be changed to the new callingstyle. Typically this just means changing a require to a use, addingthe explicit 'AUTOLOAD' import if needed, and removing AutoLoaderfrom @ISA.

On systems with restrictions on file name length, the file correspondingto a subroutine may have a shorter name that the routine itself. Thiscan lead to conflicting file names. The AutoSplit package warns ofthese potential conflicts when used to split a module. 

SEE ALSO

the SelfLoader manpage - an autoloader that doesn't use external files.


 

Index

NAME
SYNOPSIS
DESCRIPTION
Subroutine Stubs
Using AutoLoader's AUTOLOAD Subroutine
Overriding AutoLoader's AUTOLOAD Subroutine
Package Lexicals
AutoLoader vs. SelfLoader
CAVEATS
SEE ALSO

This document was created byman2html,using the manual pages.
 
ICM Bot detect detector