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