1AutoLoader(3pm)        Perl Programmers Reference Guide        AutoLoader(3pm)
2
3
4

NAME

6       AutoLoader - load subroutines only on demand
7

SYNOPSIS

9           package Foo;
10           use AutoLoader 'AUTOLOAD';   # import the default AUTOLOAD subroutine
11
12           package Bar;
13           use AutoLoader;              # don't import AUTOLOAD, define our own
14           sub AUTOLOAD {
15               ...
16               $AutoLoader::AUTOLOAD = "...";
17               goto &AutoLoader::AUTOLOAD;
18           }
19

DESCRIPTION

21       The AutoLoader module works with the AutoSplit module and the "__END__"
22       token to defer the loading of some subroutines until they are used
23       rather than loading them all at once.
24
25       To use AutoLoader, the author of a module has to place the definitions
26       of subroutines to be autoloaded after an "__END__" token.  (See
27       perldata.)  The AutoSplit module can then be run manually to extract
28       the definitions into individual files auto/funcname.al.
29
30       AutoLoader implements an AUTOLOAD subroutine.  When an undefined
31       subroutine in is called in a client module of AutoLoader, AutoLoader's
32       AUTOLOAD subroutine attempts to locate the subroutine in a file with a
33       name related to the location of the file from which the client module
34       was read.  As an example, if POSIX.pm is located in
35       /usr/local/lib/perl5/POSIX.pm, AutoLoader will look for perl
36       subroutines POSIX in /usr/local/lib/perl5/auto/POSIX/*.al, where the
37       ".al" file has the same name as the subroutine, sans package.  If such
38       a file exists, AUTOLOAD will read and evaluate it, thus (presumably)
39       defining the needed subroutine.  AUTOLOAD will then "goto" the newly
40       defined subroutine.
41
42       Once this process completes for a given function, it is defined, so
43       future calls to the subroutine will bypass the AUTOLOAD mechanism.
44
45   Subroutine Stubs
46       In order for object method lookup and/or prototype checking to operate
47       correctly even when methods have not yet been defined it is necessary
48       to "forward declare" each subroutine (as in "sub NAME;").  See
49       "SYNOPSIS" in perlsub.  Such forward declaration creates "subroutine
50       stubs", which are place holders with no code.
51
52       The AutoSplit and AutoLoader modules automate the creation of forward
53       declarations.  The AutoSplit module creates an 'index' file containing
54       forward declarations of all the AutoSplit subroutines.  When the
55       AutoLoader module is 'use'd it loads these declarations into its
56       callers package.
57
58       Because of this mechanism it is important that AutoLoader is always
59       "use"d and not "require"d.
60
61   Using AutoLoader's AUTOLOAD Subroutine
62       In order to use AutoLoader's AUTOLOAD subroutine you must explicitly
63       import it:
64
65           use AutoLoader 'AUTOLOAD';
66
67   Overriding AutoLoader's AUTOLOAD Subroutine
68       Some modules, mainly extensions, provide their own AUTOLOAD
69       subroutines.  They typically need to check for some special cases (such
70       as constants) and then fallback to AutoLoader's AUTOLOAD for the rest.
71
72       Such modules should not import AutoLoader's AUTOLOAD subroutine.
73       Instead, they should define their own AUTOLOAD subroutines along these
74       lines:
75
76           use AutoLoader;
77           use Carp;
78
79           sub AUTOLOAD {
80               my $sub = $AUTOLOAD;
81               (my $constname = $sub) =~ s/.*:://;
82               my $val = constant($constname, @_ ? $_[0] : 0);
83               if ($! != 0) {
84                   if ($! =~ /Invalid/ || $!{EINVAL}) {
85                       $AutoLoader::AUTOLOAD = $sub;
86                       goto &AutoLoader::AUTOLOAD;
87                   }
88                   else {
89                       croak "Your vendor has not defined constant $constname";
90                   }
91               }
92               *$sub = sub { $val }; # same as: eval "sub $sub { $val }";
93               goto &$sub;
94           }
95
96       If any module's own AUTOLOAD subroutine has no need to fallback to the
97       AutoLoader's AUTOLOAD subroutine (because it doesn't have any AutoSplit
98       subroutines), then that module should not use AutoLoader at all.
99
100   Package Lexicals
101       Package lexicals declared with "my" in the main block of a package
102       using AutoLoader will not be visible to auto-loaded subroutines, due to
103       the fact that the given scope ends at the "__END__" marker.  A module
104       using such variables as package globals will not work properly under
105       the AutoLoader.
106
107       The "vars" pragma (see "vars" in perlmod) may be used in such
108       situations as an alternative to explicitly qualifying all globals with
109       the package namespace.  Variables pre-declared with this pragma will be
110       visible to any autoloaded routines (but will not be invisible outside
111       the package, unfortunately).
112
113   Not Using AutoLoader
114       You can stop using AutoLoader by simply
115
116               no AutoLoader;
117
118   AutoLoader vs. SelfLoader
119       The AutoLoader is similar in purpose to SelfLoader: both delay the
120       loading of subroutines.
121
122       SelfLoader uses the "__DATA__" marker rather than "__END__".  While
123       this avoids the use of a hierarchy of disk files and the associated
124       open/close for each routine loaded, SelfLoader suffers a startup speed
125       disadvantage in the one-time parsing of the lines after "__DATA__",
126       after which routines are cached.  SelfLoader can also handle multiple
127       packages in a file.
128
129       AutoLoader only reads code as it is requested, and in many cases should
130       be faster, but requires a mechanism like AutoSplit be used to create
131       the individual files.  ExtUtils::MakeMaker will invoke AutoSplit
132       automatically if AutoLoader is used in a module source file.
133
134   Forcing AutoLoader to Load a Function
135       Sometimes, it can be necessary or useful to make sure that a certain
136       function is fully loaded by AutoLoader. This is the case, for example,
137       when you need to wrap a function to inject debugging code. It is also
138       helpful to force early loading of code before forking to make use of
139       copy-on-write as much as possible.
140
141       Starting with AutoLoader 5.73, you can call the
142       "AutoLoader::autoload_sub" function with the fully-qualified name of
143       the function to load from its .al file. The behaviour is exactly the
144       same as if you called the function, triggering the regular "AUTOLOAD"
145       mechanism, but it does not actually execute the autoloaded function.
146

CAVEATS

148       AutoLoaders prior to Perl 5.002 had a slightly different interface.
149       Any old modules which use AutoLoader should be changed to the new
150       calling style.  Typically this just means changing a require to a use,
151       adding the explicit 'AUTOLOAD' import if needed, and removing
152       AutoLoader from @ISA.
153
154       On systems with restrictions on file name length, the file
155       corresponding to a subroutine may have a shorter name that the routine
156       itself.  This can lead to conflicting file names.  The AutoSplit
157       package warns of these potential conflicts when used to split a module.
158
159       AutoLoader may fail to find the autosplit files (or even find the wrong
160       ones) in cases where @INC contains relative paths, and the program does
161       "chdir".
162

SEE ALSO

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

AUTHOR

167       "AutoLoader" is maintained by the perl5-porters. Please direct any
168       questions to the canonical mailing list. Anything that is applicable to
169       the CPAN release can be sent to its maintainer, though.
170
171       Author and Maintainer: The Perl5-Porters <perl5-porters@perl.org>
172
173       Maintainer of the CPAN release: Steffen Mueller <smueller@cpan.org>
174
176       This package has been part of the perl core since the first release of
177       perl5. It has been released separately to CPAN so older installations
178       can benefit from bug fixes.
179
180       This package has the same copyright and license as the perl core:
181
182                    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
183               2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
184               2011, 2012, 2013
185               by Larry Wall and others
186
187                                   All rights reserved.
188
189           This program is free software; you can redistribute it and/or modify
190           it under the terms of either:
191
192               a) the GNU General Public License as published by the Free
193               Software Foundation; either version 1, or (at your option) any
194               later version, or
195
196               b) the "Artistic License" which comes with this Kit.
197
198           This program is distributed in the hope that it will be useful,
199           but WITHOUT ANY WARRANTY; without even the implied warranty of
200           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
201           the GNU General Public License or the Artistic License for more details.
202
203           You should have received a copy of the Artistic License with this
204           Kit, in the file named "Artistic".  If not, I'll be glad to provide one.
205
206           You should also have received a copy of the GNU General Public License
207           along with this program in the file named "Copying". If not, write to the
208           Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
209           MA 02110-1301, USA or visit their web page on the internet at
210           http://www.gnu.org/copyleft/gpl.html.
211
212           For those of you that choose to use the GNU General Public License,
213           my interpretation of the GNU General Public License is that no Perl
214           script falls under the terms of the GPL unless you explicitly put
215           said script under the terms of the GPL yourself.  Furthermore, any
216           object code linked with perl does not automatically fall under the
217           terms of the GPL, provided such object code only adds definitions
218           of subroutines and variables, and does not otherwise impair the
219           resulting interpreter from executing any standard Perl script.  I
220           consider linking in C subroutines in this manner to be the moral
221           equivalent of defining subroutines in the Perl language itself.  You
222           may sell such an object file as proprietary provided that you provide
223           or offer to provide the Perl source, as specified by the GNU General
224           Public License.  (This is merely an alternate way of specifying input
225           to the program.)  You may also sell a binary produced by the dumping of
226           a running Perl script that belongs to you, provided that you provide or
227           offer to provide the Perl source as specified by the GPL.  (The
228           fact that a Perl interpreter and your code are in the same binary file
229           is, in this case, a form of mere aggregation.)  This is my interpretation
230           of the GPL.  If you still have concerns or difficulties understanding
231           my intent, feel free to contact me.  Of course, the Artistic License
232           spells all this out for your protection, so you may prefer to use that.
233
234
235
236perl v5.26.3                      2018-03-01                   AutoLoader(3pm)
Impressum