Manual Pages for UNIX Darwin command on man AutoLoader
MyWebUniversity

Manual Pages for UNIX Darwin command on man AutoLoader

AutoLoader(3pm) Perl Programmers Reference Guide AutoLoader(3pm)

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 AAuuttooLLooaaddeerr module works with the AAuuttooSSpplliitt module and the "END" token to defer the loading of some subroutines until they are used rather than loading them all at once. To use AAuuttooLLooaaddeerr, the author of a module has to place the definitions

of subroutines to be autoloaded after an "END" token. (See perl-

data.) The AAuuttooSSpplliitt module can then be run manually to extract the definitions into individual files auto/funcname.al.

AAuuttooLLooaaddeerr implements an AUTOLOAD subroutine. When an undefined sub-

routine in is called in a client module of AAuuttooLLooaaddeerr, AAuuttooLLooaaddeerr'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, AAuuttooLLooaaddeerr will look for perl subrou-

tines PPOOSSIIXX in /usr/local/lib/perl5/auto/POSIX/*.al, where the ".al" file has the same name as the subroutine, sans package. If such a file exists, AUTOLOAD will read and evaluate it, thus (presumably) defining the needed subroutine. AUTOLOAD will then "goto" the newly defined subroutine. Once this process completes for a given function, it is defined, so future calls to the subroutine will bypass the AUTOLOAD mechanism. SSuubbrroouuttiinnee SSttuubbss In 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 "sub NAME;"). See "SYNOP-

SIS" in perlsub. Such forward declaration creates "subroutine stubs", which are place holders with no code. The AutoSplit and AAuuttooLLooaaddeerr 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 AAuuttooLLooaaddeerr is always "use"d and not "require"d. UUssiinngg AAuuttooLLooaaddeerr''ss AAUUTTOOLLOOAADD SSuubbrroouuttiinnee In order to use AAuuttooLLooaaddeerr's AUTOLOAD subroutine you must explicitly import it:

use AutoLoader 'AUTOLOAD';

OOvveerrrriiddiinngg AAuuttooLLooaaddeerr''ss AAUUTTOOLLOOAADD SSuubbrroouuttiinnee

Some modules, mainly extensions, provide their own AUTOLOAD subrou-

tines. They typically need to check for some special cases (such as constants) and then fallback to AAuuttooLLooaaddeerr's AUTOLOAD for the rest. Such modules should not import AAuuttooLLooaaddeerr'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 AAuuttooLLooaaddeerr at all. PPaacckkaaggee LLeexxiiccaallss Package lexicals declared with "my" in the main block of a package

using AAuuttooLLooaaddeerr will not be visible to auto-loaded subroutines, 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 AAuuttooLLooaaddeerr.

The "vars" pragma (see "vars" in perlmod) may be used in such situa-

tions 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). NNoott UUssiinngg AAuuttooLLooaaddeerr

You can stop using AutoLoader by simply

no AutoLoader;

AAuuttooLLooaaddeerr vvss.. SSeellffLLooaaddeerr The AAuuttooLLooaaddeerr is similar in purpose to SSeellffLLooaaddeerr: both delay the loading of subroutines. SSeellffLLooaaddeerr uses 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, SSeellffLLooaaddeerr suffers a startup speed

disadvantage in the one-time parsing of the lines after "DATA",

after which routines are cached. SSeellffLLooaaddeerr can also handle multiple packages in a file. AAuuttooLLooaaddeerr only reads code as it is requested, and in many cases should be faster, but requires a mechanism like AAuuttooSSpplliitt be used to create

the individual files. ExtUtils::MakeMaker will invoke AAuuttooSSpplliitt auto-

matically if AAuuttooLLooaaddeerr is used in a module source file. CCAAVVEEAATTSS

AutoLoaders prior to Perl 5.002 had a slightly different interface.

Any old modules which use AAuuttooLLooaaddeerr should be changed to the new call-

ing style. Typically this just means changing a require to a use, adding the explicit 'AUTOLOAD' import if needed, and removing AAuuttooLLooaaddeerr from @ISA.

On systems with restrictions on file name length, the file correspond-

ing 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 @INC contains relative paths, aanndd the program does "chdir".

SEE ALSO

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

perl v5.8.8 2001-09-21 AutoLoader(3pm)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™