Debian Codegraph @

upstream/mageia-cauldron/man3pm/AutoLoader.3pm

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