| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "AutoLoader 3perl" |
| 58 |
.TH AutoLoader 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
AutoLoader \- load subroutines only on demand |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 2 |
| 68 |
\& package Foo; |
| 69 |
\& use AutoLoader \*(AqAUTOLOAD\*(Aq; # import the default AUTOLOAD subroutine |
| 70 |
\& |
| 71 |
\& package Bar; |
| 72 |
\& use AutoLoader; # don\*(Aqt import AUTOLOAD, define our own |
| 73 |
\& sub AUTOLOAD { |
| 74 |
\& ... |
| 75 |
\& $AutoLoader::AUTOLOAD = "..."; |
| 76 |
\& goto &AutoLoader::AUTOLOAD; |
| 77 |
\& } |
| 78 |
.Ve |
| 79 |
.SH DESCRIPTION |
| 80 |
.IX Header "DESCRIPTION" |
| 81 |
The \fBAutoLoader\fR module works with the \fBAutoSplit\fR module and the |
| 82 |
\&\f(CW\*(C`_\|_END_\|_\*(C'\fR token to defer the loading of some subroutines until they are |
| 83 |
used rather than loading them all at once. |
| 84 |
.PP |
| 85 |
To use \fBAutoLoader\fR, the author of a module has to place the |
| 86 |
definitions of subroutines to be autoloaded after an \f(CW\*(C`_\|_END_\|_\*(C'\fR token. |
| 87 |
(See perldata.) The \fBAutoSplit\fR module can then be run manually to |
| 88 |
extract the definitions into individual files \fIauto/funcname.al\fR. |
| 89 |
.PP |
| 90 |
\&\fBAutoLoader\fR implements an AUTOLOAD subroutine. When an undefined |
| 91 |
subroutine in is called in a client module of \fBAutoLoader\fR, |
| 92 |
\&\fBAutoLoader\fR's AUTOLOAD subroutine attempts to locate the subroutine in a |
| 93 |
file with a name related to the location of the file from which the |
| 94 |
client module was read. As an example, if \fIPOSIX.pm\fR is located in |
| 95 |
\&\fI/usr/local/lib/perl5/POSIX.pm\fR, \fBAutoLoader\fR will look for perl |
| 96 |
subroutines \fBPOSIX\fR in \fI/usr/local/lib/perl5/auto/POSIX/*.al\fR, where |
| 97 |
the \f(CW\*(C`.al\*(C'\fR file has the same name as the subroutine, sans package. If |
| 98 |
such a file exists, AUTOLOAD will read and evaluate it, |
| 99 |
thus (presumably) defining the needed subroutine. AUTOLOAD will then |
| 100 |
\&\f(CW\*(C`goto\*(C'\fR the newly defined subroutine. |
| 101 |
.PP |
| 102 |
Once this process completes for a given function, it is defined, so |
| 103 |
future calls to the subroutine will bypass the AUTOLOAD mechanism. |
| 104 |
.SS "Subroutine Stubs" |
| 105 |
.IX Subsection "Subroutine Stubs" |
| 106 |
In order for object method lookup and/or prototype checking to operate |
| 107 |
correctly even when methods have not yet been defined it is necessary to |
| 108 |
"forward declare" each subroutine (as in \f(CW\*(C`sub NAME;\*(C'\fR). See |
| 109 |
"SYNOPSIS" in perlsub. Such forward declaration creates "subroutine |
| 110 |
stubs", which are place holders with no code. |
| 111 |
.PP |
| 112 |
The AutoSplit and \fBAutoLoader\fR modules automate the creation of forward |
| 113 |
declarations. The AutoSplit module creates an 'index' file containing |
| 114 |
forward declarations of all the AutoSplit subroutines. When the |
| 115 |
AutoLoader module is 'use'd it loads these declarations into its callers |
| 116 |
package. |
| 117 |
.PP |
| 118 |
Because of this mechanism it is important that \fBAutoLoader\fR is always |
| 119 |
\&\f(CW\*(C`use\*(C'\fRd and not \f(CW\*(C`require\*(C'\fRd. |
| 120 |
.SS "Using \fBAutoLoader\fP's AUTOLOAD Subroutine" |
| 121 |
.IX Subsection "Using AutoLoader's AUTOLOAD Subroutine" |
| 122 |
In order to use \fBAutoLoader\fR's AUTOLOAD subroutine you \fImust\fR |
| 123 |
explicitly import it: |
| 124 |
.PP |
| 125 |
.Vb 1 |
| 126 |
\& use AutoLoader \*(AqAUTOLOAD\*(Aq; |
| 127 |
.Ve |
| 128 |
.SS "Overriding \fBAutoLoader\fP's AUTOLOAD Subroutine" |
| 129 |
.IX Subsection "Overriding AutoLoader's AUTOLOAD Subroutine" |
| 130 |
Some modules, mainly extensions, provide their own AUTOLOAD subroutines. |
| 131 |
They typically need to check for some special cases (such as constants) |
| 132 |
and then fallback to \fBAutoLoader\fR's AUTOLOAD for the rest. |
| 133 |
.PP |
| 134 |
Such modules should \fInot\fR import \fBAutoLoader\fR's AUTOLOAD subroutine. |
| 135 |
Instead, they should define their own AUTOLOAD subroutines along these |
| 136 |
lines: |
| 137 |
.PP |
| 138 |
.Vb 2 |
| 139 |
\& use AutoLoader; |
| 140 |
\& use Carp; |
| 141 |
\& |
| 142 |
\& sub AUTOLOAD { |
| 143 |
\& my $sub = $AUTOLOAD; |
| 144 |
\& (my $constname = $sub) =~ s/.*:://; |
| 145 |
\& my $val = constant($constname, @_ ? $_[0] : 0); |
| 146 |
\& if ($! != 0) { |
| 147 |
\& if ($! =~ /Invalid/ || $!{EINVAL}) { |
| 148 |
\& $AutoLoader::AUTOLOAD = $sub; |
| 149 |
\& goto &AutoLoader::AUTOLOAD; |
| 150 |
\& } |
| 151 |
\& else { |
| 152 |
\& croak "Your vendor has not defined constant $constname"; |
| 153 |
\& } |
| 154 |
\& } |
| 155 |
\& *$sub = sub { $val }; # same as: eval "sub $sub { $val }"; |
| 156 |
\& goto &$sub; |
| 157 |
\& } |
| 158 |
.Ve |
| 159 |
.PP |
| 160 |
If any module's own AUTOLOAD subroutine has no need to fallback to the |
| 161 |
AutoLoader's AUTOLOAD subroutine (because it doesn't have any AutoSplit |
| 162 |
subroutines), then that module should not use \fBAutoLoader\fR at all. |
| 163 |
.SS "Package Lexicals" |
| 164 |
.IX Subsection "Package Lexicals" |
| 165 |
Package lexicals declared with \f(CW\*(C`my\*(C'\fR in the main block of a package |
| 166 |
using \fBAutoLoader\fR will not be visible to auto-loaded subroutines, due to |
| 167 |
the fact that the given scope ends at the \f(CW\*(C`_\|_END_\|_\*(C'\fR marker. A module |
| 168 |
using such variables as package globals will not work properly under the |
| 169 |
\&\fBAutoLoader\fR. |
| 170 |
.PP |
| 171 |
The \f(CW\*(C`vars\*(C'\fR pragma (see "vars" in perlmod) may be used in such |
| 172 |
situations as an alternative to explicitly qualifying all globals with |
| 173 |
the package namespace. Variables pre-declared with this pragma will be |
| 174 |
visible to any autoloaded routines (but will not be invisible outside |
| 175 |
the package, unfortunately). |
| 176 |
.SS "Not Using AutoLoader" |
| 177 |
.IX Subsection "Not Using AutoLoader" |
| 178 |
You can stop using AutoLoader by simply |
| 179 |
.PP |
| 180 |
.Vb 1 |
| 181 |
\& no AutoLoader; |
| 182 |
.Ve |
| 183 |
.SS "\fBAutoLoader\fP vs. \fBSelfLoader\fP" |
| 184 |
.IX Subsection "AutoLoader vs. SelfLoader" |
| 185 |
The \fBAutoLoader\fR is similar in purpose to \fBSelfLoader\fR: both delay the |
| 186 |
loading of subroutines. |
| 187 |
.PP |
| 188 |
\&\fBSelfLoader\fR uses the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR marker rather than \f(CW\*(C`_\|_END_\|_\*(C'\fR. |
| 189 |
While this avoids the use of a hierarchy of disk files and the |
| 190 |
associated open/close for each routine loaded, \fBSelfLoader\fR suffers a |
| 191 |
startup speed disadvantage in the one-time parsing of the lines after |
| 192 |
\&\f(CW\*(C`_\|_DATA_\|_\*(C'\fR, after which routines are cached. \fBSelfLoader\fR can also |
| 193 |
handle multiple packages in a file. |
| 194 |
.PP |
| 195 |
\&\fBAutoLoader\fR only reads code as it is requested, and in many cases |
| 196 |
should be faster, but requires a mechanism like \fBAutoSplit\fR be used to |
| 197 |
create the individual files. ExtUtils::MakeMaker will invoke |
| 198 |
\&\fBAutoSplit\fR automatically if \fBAutoLoader\fR is used in a module source |
| 199 |
file. |
| 200 |
.SS "Forcing AutoLoader to Load a Function" |
| 201 |
.IX Subsection "Forcing AutoLoader to Load a Function" |
| 202 |
Sometimes, it can be necessary or useful to make sure that a certain |
| 203 |
function is fully loaded by AutoLoader. This is the case, for example, |
| 204 |
when you need to wrap a function to inject debugging code. It is also |
| 205 |
helpful to force early loading of code before forking to make use of |
| 206 |
copy-on-write as much as possible. |
| 207 |
.PP |
| 208 |
Starting with AutoLoader 5.73, you can call the |
| 209 |
\&\f(CW\*(C`AutoLoader::autoload_sub\*(C'\fR function with the fully-qualified name of |
| 210 |
the function to load from its \fI.al\fR file. The behaviour is exactly |
| 211 |
the same as if you called the function, triggering the regular |
| 212 |
\&\f(CW\*(C`AUTOLOAD\*(C'\fR mechanism, but it does not actually execute the |
| 213 |
autoloaded function. |
| 214 |
.SH CAVEATS |
| 215 |
.IX Header "CAVEATS" |
| 216 |
AutoLoaders prior to Perl 5.002 had a slightly different interface. Any |
| 217 |
old modules which use \fBAutoLoader\fR should be changed to the new calling |
| 218 |
style. Typically this just means changing a require to a use, adding |
| 219 |
the explicit \f(CW\*(AqAUTOLOAD\*(Aq\fR import if needed, and removing \fBAutoLoader\fR |
| 220 |
from \f(CW@ISA\fR. |
| 221 |
.PP |
| 222 |
On systems with restrictions on file name length, the file corresponding |
| 223 |
to a subroutine may have a shorter name that the routine itself. This |
| 224 |
can lead to conflicting file names. The \fIAutoSplit\fR package warns of |
| 225 |
these potential conflicts when used to split a module. |
| 226 |
.PP |
| 227 |
AutoLoader may fail to find the autosplit files (or even find the wrong |
| 228 |
ones) in cases where \f(CW@INC\fR contains relative paths, \fBand\fR the program |
| 229 |
does \f(CW\*(C`chdir\*(C'\fR. |
| 230 |
.SH "SEE ALSO" |
| 231 |
.IX Header "SEE ALSO" |
| 232 |
SelfLoader \- an autoloader that doesn't use external files. |
| 233 |
.SH AUTHOR |
| 234 |
.IX Header "AUTHOR" |
| 235 |
\&\f(CW\*(C`AutoLoader\*(C'\fR is maintained by the perl5\-porters. Please direct |
| 236 |
any questions to the canonical mailing list. Anything that |
| 237 |
is applicable to the CPAN release can be sent to its maintainer, |
| 238 |
though. |
| 239 |
.PP |
| 240 |
Author and Maintainer: The Perl5\-Porters <perl5\[email protected]> |
| 241 |
.PP |
| 242 |
Maintainer of the CPAN release: Steffen Mueller <[email protected]> |
| 243 |
.SH "COPYRIGHT AND LICENSE" |
| 244 |
.IX Header "COPYRIGHT AND LICENSE" |
| 245 |
This package has been part of the perl core since the first release |
| 246 |
of perl5. It has been released separately to CPAN so older installations |
| 247 |
can benefit from bug fixes. |
| 248 |
.PP |
| 249 |
This package has the same copyright and license as the perl core: |
| 250 |
.PP |
| 251 |
.Vb 4 |
| 252 |
\& Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
| 253 |
\& 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, |
| 254 |
\& 2011, 2012, 2013 |
| 255 |
\& by Larry Wall and others |
| 256 |
\& |
| 257 |
\& All rights reserved. |
| 258 |
\& |
| 259 |
\& This program is free software; you can redistribute it and/or modify |
| 260 |
\& it under the terms of either: |
| 261 |
\& |
| 262 |
\& a) the GNU General Public License as published by the Free |
| 263 |
\& Software Foundation; either version 1, or (at your option) any |
| 264 |
\& later version, or |
| 265 |
\& |
| 266 |
\& b) the "Artistic License" which comes with this Kit. |
| 267 |
\& |
| 268 |
\& This program is distributed in the hope that it will be useful, |
| 269 |
\& but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 270 |
\& MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either |
| 271 |
\& the GNU General Public License or the Artistic License for more details. |
| 272 |
\& |
| 273 |
\& You should have received a copy of the Artistic License with this |
| 274 |
\& Kit, in the file named "Artistic". If not, I\*(Aqll be glad to provide one. |
| 275 |
\& |
| 276 |
\& You should also have received a copy of the GNU General Public License |
| 277 |
\& along with this program in the file named "Copying". If not, write to the |
| 278 |
\& Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 279 |
\& MA 02110\-1301, USA or visit their web page on the internet at |
| 280 |
\& http://www.gnu.org/copyleft/gpl.html. |
| 281 |
\& |
| 282 |
\& For those of you that choose to use the GNU General Public License, |
| 283 |
\& my interpretation of the GNU General Public License is that no Perl |
| 284 |
\& script falls under the terms of the GPL unless you explicitly put |
| 285 |
\& said script under the terms of the GPL yourself. Furthermore, any |
| 286 |
\& object code linked with perl does not automatically fall under the |
| 287 |
\& terms of the GPL, provided such object code only adds definitions |
| 288 |
\& of subroutines and variables, and does not otherwise impair the |
| 289 |
\& resulting interpreter from executing any standard Perl script. I |
| 290 |
\& consider linking in C subroutines in this manner to be the moral |
| 291 |
\& equivalent of defining subroutines in the Perl language itself. You |
| 292 |
\& may sell such an object file as proprietary provided that you provide |
| 293 |
\& or offer to provide the Perl source, as specified by the GNU General |
| 294 |
\& Public License. (This is merely an alternate way of specifying input |
| 295 |
\& to the program.) You may also sell a binary produced by the dumping of |
| 296 |
\& a running Perl script that belongs to you, provided that you provide or |
| 297 |
\& offer to provide the Perl source as specified by the GPL. (The |
| 298 |
\& fact that a Perl interpreter and your code are in the same binary file |
| 299 |
\& is, in this case, a form of mere aggregation.) This is my interpretation |
| 300 |
\& of the GPL. If you still have concerns or difficulties understanding |
| 301 |
\& my intent, feel free to contact me. Of course, the Artistic License |
| 302 |
\& spells all this out for your protection, so you may prefer to use that. |
| 303 |
.Ve |