Debian Codegraph @

upstream/opensuse-tumbleweed/man3/SelfLoader.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 "SelfLoader 3pm"
61 .TH SelfLoader 3pm 2026-03-09 "perl v5.42.1" "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 SelfLoader \- load functions only on demand
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 2
71 \& package FOOBAR;
72 \& use SelfLoader;
73 \&
74 \& ... (initializing code)
75 \&
76 \& _\|_DATA_\|_
77 \& sub {....
78 .Ve
79 .SH DESCRIPTION
80 .IX Header "DESCRIPTION"
81 This module tells its users that functions in the FOOBAR package are to be
82 autoloaded from after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token. See also
83 "Autoloading" in perlsub.
84 .SS "The _\|_DATA_\|_ token"
85 .IX Subsection "The __DATA__ token"
86 The \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token tells the perl compiler that the perl code
87 for compilation is finished. Everything after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token
88 is available for reading via the filehandle FOOBAR::DATA,
89 where FOOBAR is the name of the current package when the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR
90 token is reached. This works just the same as \f(CW\*(C`_\|_END_\|_\*(C'\fR does in
91 package \*(Aqmain\*(Aq, but for other modules data after \f(CW\*(C`_\|_END_\|_\*(C'\fR is not
92 automatically retrievable, whereas data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR is.
93 The \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token is not recognized in versions of perl prior to
94 5.001m.
95 .PP
96 Note that it is possible to have \f(CW\*(C`_\|_DATA_\|_\*(C'\fR tokens in the same package
97 in multiple files, and that the last \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token in a given
98 package that is encountered by the compiler is the one accessible
99 by the filehandle. This also applies to \f(CW\*(C`_\|_END_\|_\*(C'\fR and main, i.e. if
100 the \*(Aqmain\*(Aq program has an \f(CW\*(C`_\|_END_\|_\*(C'\fR, but a module \*(Aqrequire\*(Aqd (_not_ \*(Aquse\*(Aqd)
101 by that program has a \*(Aqpackage main;\*(Aq declaration followed by an \*(Aq\f(CW\*(C`_\|_DATA_\|_\*(C'\fR\*(Aq,
102 then the \f(CW\*(C`DATA\*(C'\fR filehandle is set to access the data after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR
103 in the module, _not_ the data after the \f(CW\*(C`_\|_END_\|_\*(C'\fR token in the \*(Aqmain\*(Aq
104 program, since the compiler encounters the \*(Aqrequire\*(Aqd file later.
105 .SS "SelfLoader autoloading"
106 .IX Subsection "SelfLoader autoloading"
107 The \fBSelfLoader\fR works by the user placing the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR
108 token \fIafter\fR perl code which needs to be compiled and
109 run at \*(Aqrequire\*(Aq time, but \fIbefore\fR subroutine declarations
110 that can be loaded in later \- usually because they may never
111 be called.
112 .PP
113 The \fBSelfLoader\fR will read from the FOOBAR::DATA filehandle to
114 load in the data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR, and load in any subroutine
115 when it is called. The costs are the one\-time parsing of the
116 data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR, and a load delay for the _first_
117 call of any autoloaded function. The benefits (hopefully)
118 are a speeded up compilation phase, with no need to load
119 functions which are never used.
120 .PP
121 The \fBSelfLoader\fR will stop reading from \f(CW\*(C`_\|_DATA_\|_\*(C'\fR if
122 it encounters the \f(CW\*(C`_\|_END_\|_\*(C'\fR token \- just as you would expect.
123 If the \f(CW\*(C`_\|_END_\|_\*(C'\fR token is present, and is followed by the
124 token DATA, then the \fBSelfLoader\fR leaves the FOOBAR::DATA
125 filehandle open on the line after that token.
126 .PP
127 The \fBSelfLoader\fR exports the \f(CW\*(C`AUTOLOAD\*(C'\fR subroutine to the
128 package using the \fBSelfLoader\fR, and this loads the called
129 subroutine when it is first called.
130 .PP
131 There is no advantage to putting subroutines which will _always_
132 be called after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token.
133 .SS "Autoloading and package lexicals"
134 .IX Subsection "Autoloading and package lexicals"
135 A \*(Aqmy \f(CW$pack_lexical\fR\*(Aq statement makes the variable \f(CW$pack_lexical\fR
136 local _only_ to the file up to the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token. Subroutines
137 declared elsewhere _cannot_ see these types of variables,
138 just as if you declared subroutines in the package but in another
139 file, they cannot see these variables.
140 .PP
141 So specifically, autoloaded functions cannot see package
142 lexicals (this applies to both the \fBSelfLoader\fR and the Autoloader).
143 The \f(CW\*(C`vars\*(C'\fR pragma provides an alternative to defining package\-level
144 globals that will be visible to autoloaded routines. See the documentation
145 on \fBvars\fR in the pragma section of perlmod.
146 .SS "SelfLoader and AutoLoader"
147 .IX Subsection "SelfLoader and AutoLoader"
148 The \fBSelfLoader\fR can replace the AutoLoader \- just change \*(Aquse AutoLoader\*(Aq
149 to \*(Aquse SelfLoader\*(Aq (though note that the \fBSelfLoader\fR exports
150 the AUTOLOAD function \- but if you have your own AUTOLOAD and
151 are using the AutoLoader too, you probably know what you\*(Aqre doing),
152 and the \f(CW\*(C`_\|_END_\|_\*(C'\fR token to \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. You will need perl version 5.001m
153 or later to use this (version 5.001 with all patches up to patch m).
154 .PP
155 There is no need to inherit from the \fBSelfLoader\fR.
156 .PP
157 The \fBSelfLoader\fR works similarly to the AutoLoader, but picks up the
158 subs from after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR instead of in the \*(Aqlib/auto\*(Aq directory.
159 There is a maintenance gain in not needing to run AutoSplit on the module
160 at installation, and a runtime gain in not needing to keep opening and
161 closing files to load subs. There is a runtime loss in needing
162 to parse the code after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. Details of the \fBAutoLoader\fR and
163 another view of these distinctions can be found in that module\*(Aqs
164 documentation.
165 .SS "_\|_DATA_\|_, _\|_END_\|_, and the FOOBAR::DATA filehandle"
166 .IX Subsection "__DATA__, __END__, and the FOOBAR::DATA filehandle"
167 This section is only relevant if you want to use
168 the \f(CW\*(C`FOOBAR::DATA\*(C'\fR together with the \fBSelfLoader\fR.
169 .PP
170 Data after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token in a module is read using the
171 FOOBAR::DATA filehandle. \f(CW\*(C`_\|_END_\|_\*(C'\fR can still be used to denote the end
172 of the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR section if followed by the token DATA \- this is supported
173 by the \fBSelfLoader\fR. The \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle is left open if an
174 \&\f(CW\*(C`_\|_END_\|_\*(C'\fR followed by a DATA is found, with the filehandle positioned at
175 the start of the line after the \f(CW\*(C`_\|_END_\|_\*(C'\fR token. If no \f(CW\*(C`_\|_END_\|_\*(C'\fR token is
176 present, or an \f(CW\*(C`_\|_END_\|_\*(C'\fR token with no DATA token on the same line, then
177 the filehandle is closed.
178 .PP
179 The \fBSelfLoader\fR reads from wherever the current
180 position of the \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle is, until the
181 EOF or \f(CW\*(C`_\|_END_\|_\*(C'\fR. This means that if you want to use
182 that filehandle (and ONLY if you want to), you should either
183 .PP
184 1. Put all your subroutine declarations immediately after
185 the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token and put your own data after those
186 declarations, using the \f(CW\*(C`_\|_END_\|_\*(C'\fR token to mark the end
187 of subroutine declarations. You must also ensure that the \fBSelfLoader\fR
188 reads first by calling \*(AqSelfLoader\->\fBload_stubs()\fR;\*(Aq, or by using a
189 function which is selfloaded;
190 .PP
191 or
192 .PP
193 2. You should read the \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle first, leaving
194 the handle open and positioned at the first line of subroutine
195 declarations.
196 .PP
197 You could conceivably do both.
198 .SS "Classes and inherited methods"
199 .IX Subsection "Classes and inherited methods"
200 For modules which are not classes, this section is not relevant.
201 This section is only relevant if you have methods which could
202 be inherited.
203 .PP
204 A subroutine stub (or forward declaration) looks like
205 .PP
206 .Vb 1
207 \& sub stub;
208 .Ve
209 .PP
210 i.e. it is a subroutine declaration without the body of the
211 subroutine. For modules which are not classes, there is no real
212 need for stubs as far as autoloading is concerned.
213 .PP
214 For modules which ARE classes, and need to handle inherited methods,
215 stubs are needed to ensure that the method inheritance mechanism works
216 properly. You can load the stubs into the module at \*(Aqrequire\*(Aq time, by
217 adding the statement \*(AqSelfLoader\->\fBload_stubs()\fR;\*(Aq to the module to do
218 this.
219 .PP
220 The alternative is to put the stubs in before the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token BEFORE
221 releasing the module, and for this purpose the \f(CW\*(C`Devel::SelfStubber\*(C'\fR
222 module is available. However this does require the extra step of ensuring
223 that the stubs are in the module. If this is done I strongly recommend
224 that this is done BEFORE releasing the module \- it should NOT be done
225 at install time in general.
226 .SH "Multiple packages and fully qualified subroutine names"
227 .IX Header "Multiple packages and fully qualified subroutine names"
228 Subroutines in multiple packages within the same file are supported \- but you
229 should note that this requires exporting the \f(CW\*(C`SelfLoader::AUTOLOAD\*(C'\fR to
230 every package which requires it. This is done automatically by the
231 \&\fBSelfLoader\fR when it first loads the subs into the cache, but you should
232 really specify it in the initialization before the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR by putting
233 a \*(Aquse SelfLoader\*(Aq statement in each package.
234 .PP
235 Fully qualified subroutine names are also supported. For example,
236 .PP
237 .Vb 4
238 \& _\|_DATA_\|_
239 \& sub foo::bar {23}
240 \& package baz;
241 \& sub dob {32}
242 .Ve
243 .PP
244 will all be loaded correctly by the \fBSelfLoader\fR, and the \fBSelfLoader\fR
245 will ensure that the packages \*(Aqfoo\*(Aq and \*(Aqbaz\*(Aq correctly have the
246 \&\fBSelfLoader\fR \f(CW\*(C`AUTOLOAD\*(C'\fR method when the data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR is first
247 parsed.
248 .SH AUTHOR
249 .IX Header "AUTHOR"
250 \&\f(CW\*(C`SelfLoader\*(C'\fR is maintained by the perl5\-porters. Please direct
251 any questions to the canonical mailing list. Anything that
252 is applicable to the CPAN release can be sent to its maintainer,
253 though.
254 .PP
255 Author and Maintainer: The Perl5\-Porters <perl5\[email protected]>
256 .PP
257 Maintainer of the CPAN release: Steffen Mueller <[email protected]>
258 .SH "COPYRIGHT AND LICENSE"
259 .IX Header "COPYRIGHT AND LICENSE"
260 This package has been part of the perl core since the first release
261 of perl5. It has been released separately to CPAN so older installations
262 can benefit from bug fixes.
263 .PP
264 This package has the same copyright and license as the perl core:
265 .PP
266 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
267 2000, 2001, 2002, 2003, 2004, 2005, 2006 by Larry Wall and others
268 .PP
269 All rights reserved.
270 .PP
271 This program is free software; you can redistribute it and/or modify
272 it under the terms of either:
273 .IP a) 4
274 .IX Item "a)"
275 the GNU General Public License as published by the Free Software Foundation;
276 either version 1, or (at your option) any later version, or
277 .IP b) 4
278 .IX Item "b)"
279 the "Artistic License" which comes with this Kit.
280 .PP
281 This program is distributed in the hope that it will be useful,
282 but WITHOUT ANY WARRANTY; without even the implied warranty of
283 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either
284 the GNU General Public License or the Artistic License for more details.
285 .PP
286 You should have received a copy of the Artistic License with this
287 Kit, in the file named "Artistic". If not, I\*(Aqll be glad to provide one.
288 .PP
289 You should also have received a copy of the GNU General Public License
290 along with this program in the file named "Copying". If not, see
291 <https://www.gnu.org/licenses/>.
292 .PP
293 For those of you that choose to use the GNU General Public License,
294 my interpretation of the GNU General Public License is that no Perl
295 script falls under the terms of the GPL unless you explicitly put
296 said script under the terms of the GPL yourself. Furthermore, any
297 object code linked with perl does not automatically fall under the
298 terms of the GPL, provided such object code only adds definitions
299 of subroutines and variables, and does not otherwise impair the
300 resulting interpreter from executing any standard Perl script. I
301 consider linking in C subroutines in this manner to be the moral
302 equivalent of defining subroutines in the Perl language itself. You
303 may sell such an object file as proprietary provided that you provide
304 or offer to provide the Perl source, as specified by the GNU General
305 Public License. (This is merely an alternate way of specifying input
306 to the program.) You may also sell a binary produced by the dumping of
307 a running Perl script that belongs to you, provided that you provide or
308 offer to provide the Perl source as specified by the GPL. (The
309 fact that a Perl interpreter and your code are in the same binary file
310 is, in this case, a form of mere aggregation.) This is my interpretation
311 of the GPL. If you still have concerns or difficulties understanding
312 my intent, feel free to contact me. Of course, the Artistic License
313 spells all this out for your protection, so you may prefer to use that.