Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "SelfLoader 3" | |
132 | .TH SelfLoader 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | SelfLoader \- load functions only on demand | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 2 | |
138 | \& package FOOBAR; | |
139 | \& use SelfLoader; | |
140 | .Ve | |
141 | .PP | |
142 | .Vb 1 | |
143 | \& ... (initializing code) | |
144 | .Ve | |
145 | .PP | |
146 | .Vb 2 | |
147 | \& __DATA__ | |
148 | \& sub {.... | |
149 | .Ve | |
150 | .SH "DESCRIPTION" | |
151 | .IX Header "DESCRIPTION" | |
152 | This module tells its users that functions in the \s-1FOOBAR\s0 package are to be | |
153 | autoloaded from after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token. See also | |
154 | \&\*(L"Autoloading\*(R" in perlsub. | |
155 | .Sh "The _\|_DATA_\|_ token" | |
156 | .IX Subsection "The __DATA__ token" | |
157 | The \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token tells the perl compiler that the perl code | |
158 | for compilation is finished. Everything after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token | |
159 | is available for reading via the filehandle \s-1FOOBAR::DATA\s0, | |
160 | where \s-1FOOBAR\s0 is the name of the current package when the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR | |
161 | token is reached. This works just the same as \f(CW\*(C`_\|_END_\|_\*(C'\fR does in | |
162 | package 'main', but for other modules data after \f(CW\*(C`_\|_END_\|_\*(C'\fR is not | |
163 | automatically retrievable, whereas data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR is. | |
164 | The \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token is not recognized in versions of perl prior to | |
165 | 5.001m. | |
166 | .PP | |
167 | Note that it is possible to have \f(CW\*(C`_\|_DATA_\|_\*(C'\fR tokens in the same package | |
168 | in multiple files, and that the last \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token in a given | |
169 | package that is encountered by the compiler is the one accessible | |
170 | by the filehandle. This also applies to \f(CW\*(C`_\|_END_\|_\*(C'\fR and main, i.e. if | |
171 | the 'main' program has an \f(CW\*(C`_\|_END_\|_\*(C'\fR, but a module 'require'd (_not_ 'use'd) | |
172 | by that program has a 'package main;' declaration followed by an '\f(CW\*(C`_\|_DATA_\|_\*(C'\fR', | |
173 | then the \f(CW\*(C`DATA\*(C'\fR filehandle is set to access the data after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR | |
174 | in the module, _not_ the data after the \f(CW\*(C`_\|_END_\|_\*(C'\fR token in the 'main' | |
175 | program, since the compiler encounters the 'require'd file later. | |
176 | .Sh "SelfLoader autoloading" | |
177 | .IX Subsection "SelfLoader autoloading" | |
178 | The \fBSelfLoader\fR works by the user placing the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR | |
179 | token \fIafter\fR perl code which needs to be compiled and | |
180 | run at 'require' time, but \fIbefore\fR subroutine declarations | |
181 | that can be loaded in later \- usually because they may never | |
182 | be called. | |
183 | .PP | |
184 | The \fBSelfLoader\fR will read from the \s-1FOOBAR::DATA\s0 filehandle to | |
185 | load in the data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR, and load in any subroutine | |
186 | when it is called. The costs are the one-time parsing of the | |
187 | data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR, and a load delay for the _first_ | |
188 | call of any autoloaded function. The benefits (hopefully) | |
189 | are a speeded up compilation phase, with no need to load | |
190 | functions which are never used. | |
191 | .PP | |
192 | The \fBSelfLoader\fR will stop reading from \f(CW\*(C`_\|_DATA_\|_\*(C'\fR if | |
193 | it encounters the \f(CW\*(C`_\|_END_\|_\*(C'\fR token \- just as you would expect. | |
194 | If the \f(CW\*(C`_\|_END_\|_\*(C'\fR token is present, and is followed by the | |
195 | token \s-1DATA\s0, then the \fBSelfLoader\fR leaves the \s-1FOOBAR::DATA\s0 | |
196 | filehandle open on the line after that token. | |
197 | .PP | |
198 | The \fBSelfLoader\fR exports the \f(CW\*(C`AUTOLOAD\*(C'\fR subroutine to the | |
199 | package using the \fBSelfLoader\fR, and this loads the called | |
200 | subroutine when it is first called. | |
201 | .PP | |
202 | There is no advantage to putting subroutines which will _always_ | |
203 | be called after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token. | |
204 | .Sh "Autoloading and package lexicals" | |
205 | .IX Subsection "Autoloading and package lexicals" | |
206 | A 'my \f(CW$pack_lexical\fR' statement makes the variable \f(CW$pack_lexical\fR | |
207 | local _only_ to the file up to the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token. Subroutines | |
208 | declared elsewhere _cannot_ see these types of variables, | |
209 | just as if you declared subroutines in the package but in another | |
210 | file, they cannot see these variables. | |
211 | .PP | |
212 | So specifically, autoloaded functions cannot see package | |
213 | lexicals (this applies to both the \fBSelfLoader\fR and the Autoloader). | |
214 | The \f(CW\*(C`vars\*(C'\fR pragma provides an alternative to defining package-level | |
215 | globals that will be visible to autoloaded routines. See the documentation | |
216 | on \fBvars\fR in the pragma section of perlmod. | |
217 | .Sh "SelfLoader and AutoLoader" | |
218 | .IX Subsection "SelfLoader and AutoLoader" | |
219 | The \fBSelfLoader\fR can replace the AutoLoader \- just change 'use AutoLoader' | |
220 | to 'use SelfLoader' (though note that the \fBSelfLoader\fR exports | |
221 | the \s-1AUTOLOAD\s0 function \- but if you have your own \s-1AUTOLOAD\s0 and | |
222 | are using the AutoLoader too, you probably know what you're doing), | |
223 | and the \f(CW\*(C`_\|_END_\|_\*(C'\fR token to \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. You will need perl version 5.001m | |
224 | or later to use this (version 5.001 with all patches up to patch m). | |
225 | .PP | |
226 | There is no need to inherit from the \fBSelfLoader\fR. | |
227 | .PP | |
228 | The \fBSelfLoader\fR works similarly to the AutoLoader, but picks up the | |
229 | subs from after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR instead of in the 'lib/auto' directory. | |
230 | There is a maintenance gain in not needing to run AutoSplit on the module | |
231 | at installation, and a runtime gain in not needing to keep opening and | |
232 | closing files to load subs. There is a runtime loss in needing | |
233 | to parse the code after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR. Details of the \fBAutoLoader\fR and | |
234 | another view of these distinctions can be found in that module's | |
235 | documentation. | |
236 | .Sh "_\|_DATA_\|_, _\|_END_\|_, and the \s-1FOOBAR::DATA\s0 filehandle." | |
237 | .IX Subsection "__DATA__, __END__, and the FOOBAR::DATA filehandle." | |
238 | This section is only relevant if you want to use | |
239 | the \f(CW\*(C`FOOBAR::DATA\*(C'\fR together with the \fBSelfLoader\fR. | |
240 | .PP | |
241 | Data after the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token in a module is read using the | |
242 | \&\s-1FOOBAR::DATA\s0 filehandle. \f(CW\*(C`_\|_END_\|_\*(C'\fR can still be used to denote the end | |
243 | of the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR section if followed by the token \s-1DATA\s0 \- this is supported | |
244 | by the \fBSelfLoader\fR. The \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle is left open if an | |
245 | \&\f(CW\*(C`_\|_END_\|_\*(C'\fR followed by a \s-1DATA\s0 is found, with the filehandle positioned at | |
246 | the start of the line after the \f(CW\*(C`_\|_END_\|_\*(C'\fR token. If no \f(CW\*(C`_\|_END_\|_\*(C'\fR token is | |
247 | present, or an \f(CW\*(C`_\|_END_\|_\*(C'\fR token with no \s-1DATA\s0 token on the same line, then | |
248 | the filehandle is closed. | |
249 | .PP | |
250 | The \fBSelfLoader\fR reads from wherever the current | |
251 | position of the \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle is, until the | |
252 | \&\s-1EOF\s0 or \f(CW\*(C`_\|_END_\|_\*(C'\fR. This means that if you want to use | |
253 | that filehandle (and \s-1ONLY\s0 if you want to), you should either | |
254 | .PP | |
255 | 1. Put all your subroutine declarations immediately after | |
256 | the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token and put your own data after those | |
257 | declarations, using the \f(CW\*(C`_\|_END_\|_\*(C'\fR token to mark the end | |
258 | of subroutine declarations. You must also ensure that the \fBSelfLoader\fR | |
259 | reads first by calling 'SelfLoader\->\fIload_stubs()\fR;', or by using a | |
260 | function which is selfloaded; | |
261 | .PP | |
262 | or | |
263 | .PP | |
264 | 2. You should read the \f(CW\*(C`FOOBAR::DATA\*(C'\fR filehandle first, leaving | |
265 | the handle open and positioned at the first line of subroutine | |
266 | declarations. | |
267 | .PP | |
268 | You could conceivably do both. | |
269 | .Sh "Classes and inherited methods." | |
270 | .IX Subsection "Classes and inherited methods." | |
271 | For modules which are not classes, this section is not relevant. | |
272 | This section is only relevant if you have methods which could | |
273 | be inherited. | |
274 | .PP | |
275 | A subroutine stub (or forward declaration) looks like | |
276 | .PP | |
277 | .Vb 1 | |
278 | \& sub stub; | |
279 | .Ve | |
280 | .PP | |
281 | i.e. it is a subroutine declaration without the body of the | |
282 | subroutine. For modules which are not classes, there is no real | |
283 | need for stubs as far as autoloading is concerned. | |
284 | .PP | |
285 | For modules which \s-1ARE\s0 classes, and need to handle inherited methods, | |
286 | stubs are needed to ensure that the method inheritance mechanism works | |
287 | properly. You can load the stubs into the module at 'require' time, by | |
288 | adding the statement 'SelfLoader\->\fIload_stubs()\fR;' to the module to do | |
289 | this. | |
290 | .PP | |
291 | The alternative is to put the stubs in before the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR token \s-1BEFORE\s0 | |
292 | releasing the module, and for this purpose the \f(CW\*(C`Devel::SelfStubber\*(C'\fR | |
293 | module is available. However this does require the extra step of ensuring | |
294 | that the stubs are in the module. If this is done I strongly recommend | |
295 | that this is done \s-1BEFORE\s0 releasing the module \- it should \s-1NOT\s0 be done | |
296 | at install time in general. | |
297 | .SH "Multiple packages and fully qualified subroutine names" | |
298 | .IX Header "Multiple packages and fully qualified subroutine names" | |
299 | Subroutines in multiple packages within the same file are supported \- but you | |
300 | should note that this requires exporting the \f(CW\*(C`SelfLoader::AUTOLOAD\*(C'\fR to | |
301 | every package which requires it. This is done automatically by the | |
302 | \&\fBSelfLoader\fR when it first loads the subs into the cache, but you should | |
303 | really specify it in the initialization before the \f(CW\*(C`_\|_DATA_\|_\*(C'\fR by putting | |
304 | a 'use SelfLoader' statement in each package. | |
305 | .PP | |
306 | Fully qualified subroutine names are also supported. For example, | |
307 | .PP | |
308 | .Vb 4 | |
309 | \& __DATA__ | |
310 | \& sub foo::bar {23} | |
311 | \& package baz; | |
312 | \& sub dob {32} | |
313 | .Ve | |
314 | .PP | |
315 | will all be loaded correctly by the \fBSelfLoader\fR, and the \fBSelfLoader\fR | |
316 | will ensure that the packages 'foo' and 'baz' correctly have the | |
317 | \&\fBSelfLoader\fR \f(CW\*(C`AUTOLOAD\*(C'\fR method when the data after \f(CW\*(C`_\|_DATA_\|_\*(C'\fR is first | |
318 | parsed. |