Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
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 "Exporter 3" | |
132 | .TH Exporter 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | Exporter \- Implements default import method for modules | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | In module YourModule.pm: | |
138 | .PP | |
139 | .Vb 4 | |
140 | \& package YourModule; | |
141 | \& require Exporter; | |
142 | \& @ISA = qw(Exporter); | |
143 | \& @EXPORT_OK = qw(munge frobnicate); # symbols to export on request | |
144 | .Ve | |
145 | .PP | |
146 | or | |
147 | .PP | |
148 | .Vb 3 | |
149 | \& package YourModule; | |
150 | \& use Exporter 'import'; # gives you Exporter's import() method directly | |
151 | \& @EXPORT_OK = qw(munge frobnicate); # symbols to export on request | |
152 | .Ve | |
153 | .PP | |
154 | In other files which wish to use YourModule: | |
155 | .PP | |
156 | .Vb 2 | |
157 | \& use ModuleName qw(frobnicate); # import listed symbols | |
158 | \& frobnicate ($left, $right) # calls YourModule::frobnicate | |
159 | .Ve | |
160 | .SH "DESCRIPTION" | |
161 | .IX Header "DESCRIPTION" | |
162 | The Exporter module implements an \f(CW\*(C`import\*(C'\fR method which allows a module | |
163 | to export functions and variables to its users' namespaces. Many modules | |
164 | use Exporter rather than implementing their own \f(CW\*(C`import\*(C'\fR method because | |
165 | Exporter provides a highly flexible interface, with an implementation optimised | |
166 | for the common case. | |
167 | .PP | |
168 | Perl automatically calls the \f(CW\*(C`import\*(C'\fR method when processing a | |
169 | \&\f(CW\*(C`use\*(C'\fR statement for a module. Modules and \f(CW\*(C`use\*(C'\fR are documented | |
170 | in perlfunc and perlmod. Understanding the concept of | |
171 | modules and how the \f(CW\*(C`use\*(C'\fR statement operates is important to | |
172 | understanding the Exporter. | |
173 | .Sh "How to Export" | |
174 | .IX Subsection "How to Export" | |
175 | The arrays \f(CW@EXPORT\fR and \f(CW@EXPORT_OK\fR in a module hold lists of | |
176 | symbols that are going to be exported into the users name space by | |
177 | default, or which they can request to be exported, respectively. The | |
178 | symbols can represent functions, scalars, arrays, hashes, or typeglobs. | |
179 | The symbols must be given by full name with the exception that the | |
180 | ampersand in front of a function is optional, e.g. | |
181 | .PP | |
182 | .Vb 2 | |
183 | \& @EXPORT = qw(afunc $scalar @array); # afunc is a function | |
184 | \& @EXPORT_OK = qw(&bfunc %hash *typeglob); # explicit prefix on &bfunc | |
185 | .Ve | |
186 | .PP | |
187 | If you are only exporting function names it is recommended to omit the | |
188 | ampersand, as the implementation is faster this way. | |
189 | .Sh "Selecting What To Export" | |
190 | .IX Subsection "Selecting What To Export" | |
191 | Do \fBnot\fR export method names! | |
192 | .PP | |
193 | Do \fBnot\fR export anything else by default without a good reason! | |
194 | .PP | |
195 | Exports pollute the namespace of the module user. If you must export | |
196 | try to use \f(CW@EXPORT_OK\fR in preference to \f(CW@EXPORT\fR and avoid short or | |
197 | common symbol names to reduce the risk of name clashes. | |
198 | .PP | |
199 | Generally anything not exported is still accessible from outside the | |
200 | module using the ModuleName::item_name (or \f(CW$blessed_ref\fR\->method) | |
201 | syntax. By convention you can use a leading underscore on names to | |
202 | informally indicate that they are 'internal' and not for public use. | |
203 | .PP | |
204 | (It is actually possible to get private functions by saying: | |
205 | .PP | |
206 | .Vb 3 | |
207 | \& my $subref = sub { ... }; | |
208 | \& $subref->(@args); # Call it as a function | |
209 | \& $obj->$subref(@args); # Use it as a method | |
210 | .Ve | |
211 | .PP | |
212 | However if you use them for methods it is up to you to figure out | |
213 | how to make inheritance work.) | |
214 | .PP | |
215 | As a general rule, if the module is trying to be object oriented | |
216 | then export nothing. If it's just a collection of functions then | |
217 | \&\f(CW@EXPORT_OK\fR anything but use \f(CW@EXPORT\fR with caution. For function and | |
218 | method names use barewords in preference to names prefixed with | |
219 | ampersands for the export lists. | |
220 | .PP | |
221 | Other module design guidelines can be found in perlmod. | |
222 | .Sh "How to Import" | |
223 | .IX Subsection "How to Import" | |
224 | In other files which wish to use your module there are three basic ways for | |
225 | them to load your module and import its symbols: | |
226 | .ie n .IP """use ModuleName;""" 4 | |
227 | .el .IP "\f(CWuse ModuleName;\fR" 4 | |
228 | .IX Item "use ModuleName;" | |
229 | This imports all the symbols from ModuleName's \f(CW@EXPORT\fR into the namespace | |
230 | of the \f(CW\*(C`use\*(C'\fR statement. | |
231 | .ie n .IP """use ModuleName ();""" 4 | |
232 | .el .IP "\f(CWuse ModuleName ();\fR" 4 | |
233 | .IX Item "use ModuleName ();" | |
234 | This causes perl to load your module but does not import any symbols. | |
235 | .ie n .IP """use ModuleName qw(...);""" 4 | |
236 | .el .IP "\f(CWuse ModuleName qw(...);\fR" 4 | |
237 | .IX Item "use ModuleName qw(...);" | |
238 | This imports only the symbols listed by the caller into their namespace. | |
239 | All listed symbols must be in your \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR, else an error | |
240 | occurs. The advanced export features of Exporter are accessed like this, | |
241 | but with list entries that are syntactically distinct from symbol names. | |
242 | .PP | |
243 | Unless you want to use its advanced features, this is probably all you | |
244 | need to know to use Exporter. | |
245 | .SH "Advanced features" | |
246 | .IX Header "Advanced features" | |
247 | .Sh "Specialised Import Lists" | |
248 | .IX Subsection "Specialised Import Lists" | |
249 | If any of the entries in an import list begins with !, : or / then | |
250 | the list is treated as a series of specifications which either add to | |
251 | or delete from the list of names to import. They are processed left to | |
252 | right. Specifications are in the form: | |
253 | .PP | |
254 | .Vb 4 | |
255 | \& [!]name This name only | |
256 | \& [!]:DEFAULT All names in @EXPORT | |
257 | \& [!]:tag All names in $EXPORT_TAGS{tag} anonymous list | |
258 | \& [!]/pattern/ All names in @EXPORT and @EXPORT_OK which match | |
259 | .Ve | |
260 | .PP | |
261 | A leading ! indicates that matching names should be deleted from the | |
262 | list of names to import. If the first specification is a deletion it | |
263 | is treated as though preceded by :DEFAULT. If you just want to import | |
264 | extra names in addition to the default set you will still need to | |
265 | include :DEFAULT explicitly. | |
266 | .PP | |
267 | e.g., Module.pm defines: | |
268 | .PP | |
269 | .Vb 3 | |
270 | \& @EXPORT = qw(A1 A2 A3 A4 A5); | |
271 | \& @EXPORT_OK = qw(B1 B2 B3 B4 B5); | |
272 | \& %EXPORT_TAGS = (T1 => [qw(A1 A2 B1 B2)], T2 => [qw(A1 A2 B3 B4)]); | |
273 | .Ve | |
274 | .PP | |
275 | .Vb 2 | |
276 | \& Note that you cannot use tags in @EXPORT or @EXPORT_OK. | |
277 | \& Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK. | |
278 | .Ve | |
279 | .PP | |
280 | An application using Module can say something like: | |
281 | .PP | |
282 | .Vb 1 | |
283 | \& use Module qw(:DEFAULT :T2 !B3 A3); | |
284 | .Ve | |
285 | .PP | |
286 | Other examples include: | |
287 | .PP | |
288 | .Vb 2 | |
289 | \& use Socket qw(!/^[AP]F_/ !SOMAXCONN !SOL_SOCKET); | |
290 | \& use POSIX qw(:errno_h :termios_h !TCSADRAIN !/^EXIT/); | |
291 | .Ve | |
292 | .PP | |
293 | Remember that most patterns (using //) will need to be anchored | |
294 | with a leading ^, e.g., \f(CW\*(C`/^EXIT/\*(C'\fR rather than \f(CW\*(C`/EXIT/\*(C'\fR. | |
295 | .PP | |
296 | You can say \f(CW\*(C`BEGIN { $Exporter::Verbose=1 }\*(C'\fR to see how the | |
297 | specifications are being processed and what is actually being imported | |
298 | into modules. | |
299 | .Sh "Exporting without using Exporter's import method" | |
300 | .IX Subsection "Exporting without using Exporter's import method" | |
301 | Exporter has a special method, 'export_to_level' which is used in situations | |
302 | where you can't directly call Exporter's import method. The export_to_level | |
303 | method looks like: | |
304 | .PP | |
305 | .Vb 1 | |
306 | \& MyPackage->export_to_level($where_to_export, $package, @what_to_export); | |
307 | .Ve | |
308 | .PP | |
309 | where \f(CW$where_to_export\fR is an integer telling how far up the calling stack | |
310 | to export your symbols, and \f(CW@what_to_export\fR is an array telling what | |
311 | symbols *to* export (usually this is \f(CW@_\fR). The \f(CW$package\fR argument is | |
312 | currently unused. | |
313 | .PP | |
314 | For example, suppose that you have a module, A, which already has an | |
315 | import function: | |
316 | .PP | |
317 | .Vb 1 | |
318 | \& package A; | |
319 | .Ve | |
320 | .PP | |
321 | .Vb 2 | |
322 | \& @ISA = qw(Exporter); | |
323 | \& @EXPORT_OK = qw ($b); | |
324 | .Ve | |
325 | .PP | |
326 | .Vb 4 | |
327 | \& sub import | |
328 | \& { | |
329 | \& $A::b = 1; # not a very useful import method | |
330 | \& } | |
331 | .Ve | |
332 | .PP | |
333 | and you want to Export symbol \f(CW$A::b\fR back to the module that called | |
334 | package A. Since Exporter relies on the import method to work, via | |
335 | inheritance, as it stands \fIExporter::import()\fR will never get called. | |
336 | Instead, say the following: | |
337 | .PP | |
338 | .Vb 3 | |
339 | \& package A; | |
340 | \& @ISA = qw(Exporter); | |
341 | \& @EXPORT_OK = qw ($b); | |
342 | .Ve | |
343 | .PP | |
344 | .Vb 5 | |
345 | \& sub import | |
346 | \& { | |
347 | \& $A::b = 1; | |
348 | \& A->export_to_level(1, @_); | |
349 | \& } | |
350 | .Ve | |
351 | .PP | |
352 | This will export the symbols one level 'above' the current package \- ie: to | |
353 | the program or module that used package A. | |
354 | .PP | |
355 | Note: Be careful not to modify \f(CW@_\fR at all before you call export_to_level | |
356 | \&\- or people using your package will get very unexplained results! | |
357 | .Sh "Exporting without inheriting from Exporter" | |
358 | .IX Subsection "Exporting without inheriting from Exporter" | |
359 | By including Exporter in your \f(CW@ISA\fR you inherit an Exporter's \fIimport()\fR method | |
360 | but you also inherit several other helper methods which you probably don't | |
361 | want. To avoid this you can do | |
362 | .PP | |
363 | .Vb 2 | |
364 | \& package YourModule; | |
365 | \& use Exporter qw( import ); | |
366 | .Ve | |
367 | .PP | |
368 | which will export Exporter's own \fIimport()\fR method into YourModule. | |
369 | Everything will work as before but you won't need to include Exporter in | |
370 | \&\f(CW@YourModule::ISA\fR. | |
371 | .Sh "Module Version Checking" | |
372 | .IX Subsection "Module Version Checking" | |
373 | The Exporter module will convert an attempt to import a number from a | |
374 | module into a call to \f(CW$module_name\fR\->require_version($value). This can | |
375 | be used to validate that the version of the module being used is | |
376 | greater than or equal to the required version. | |
377 | .PP | |
378 | The Exporter module supplies a default require_version method which | |
379 | checks the value of \f(CW$VERSION\fR in the exporting module. | |
380 | .PP | |
381 | Since the default require_version method treats the \f(CW$VERSION\fR number as | |
382 | a simple numeric value it will regard version 1.10 as lower than | |
383 | 1.9. For this reason it is strongly recommended that you use numbers | |
384 | with at least two decimal places, e.g., 1.09. | |
385 | .Sh "Managing Unknown Symbols" | |
386 | .IX Subsection "Managing Unknown Symbols" | |
387 | In some situations you may want to prevent certain symbols from being | |
388 | exported. Typically this applies to extensions which have functions | |
389 | or constants that may not exist on some systems. | |
390 | .PP | |
391 | The names of any symbols that cannot be exported should be listed | |
392 | in the \f(CW@EXPORT_FAIL\fR array. | |
393 | .PP | |
394 | If a module attempts to import any of these symbols the Exporter | |
395 | will give the module an opportunity to handle the situation before | |
396 | generating an error. The Exporter will call an export_fail method | |
397 | with a list of the failed symbols: | |
398 | .PP | |
399 | .Vb 1 | |
400 | \& @failed_symbols = $module_name->export_fail(@failed_symbols); | |
401 | .Ve | |
402 | .PP | |
403 | If the export_fail method returns an empty list then no error is | |
404 | recorded and all the requested symbols are exported. If the returned | |
405 | list is not empty then an error is generated for each symbol and the | |
406 | export fails. The Exporter provides a default export_fail method which | |
407 | simply returns the list unchanged. | |
408 | .PP | |
409 | Uses for the export_fail method include giving better error messages | |
410 | for some symbols and performing lazy architectural checks (put more | |
411 | symbols into \f(CW@EXPORT_FAIL\fR by default and then take them out if someone | |
412 | actually tries to use them and an expensive check shows that they are | |
413 | usable on that platform). | |
414 | .Sh "Tag Handling Utility Functions" | |
415 | .IX Subsection "Tag Handling Utility Functions" | |
416 | Since the symbols listed within \f(CW%EXPORT_TAGS\fR must also appear in either | |
417 | \&\f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR, two utility functions are provided which allow | |
418 | you to easily add tagged sets of symbols to \f(CW@EXPORT\fR or \f(CW@EXPORT_OK:\fR | |
419 | .PP | |
420 | .Vb 1 | |
421 | \& %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]); | |
422 | .Ve | |
423 | .PP | |
424 | .Vb 2 | |
425 | \& Exporter::export_tags('foo'); # add aa, bb and cc to @EXPORT | |
426 | \& Exporter::export_ok_tags('bar'); # add aa, cc and dd to @EXPORT_OK | |
427 | .Ve | |
428 | .PP | |
429 | Any names which are not tags are added to \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR | |
430 | unchanged but will trigger a warning (with \f(CW\*(C`\-w\*(C'\fR) to avoid misspelt tags | |
431 | names being silently added to \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR. Future versions | |
432 | may make this a fatal error. | |
433 | .Sh "Generating combined tags" | |
434 | .IX Subsection "Generating combined tags" | |
435 | If several symbol categories exist in \f(CW%EXPORT_TAGS\fR, it's usually | |
436 | useful to create the utility \*(L":all\*(R" to simplify \*(L"use\*(R" statements. | |
437 | .PP | |
438 | The simplest way to do this is: | |
439 | .PP | |
440 | .Vb 1 | |
441 | \& %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]); | |
442 | .Ve | |
443 | .PP | |
444 | .Vb 4 | |
445 | \& # add all the other ":class" tags to the ":all" class, | |
446 | \& # deleting duplicates | |
447 | \& { | |
448 | \& my %seen; | |
449 | .Ve | |
450 | .PP | |
451 | .Vb 3 | |
452 | \& push @{$EXPORT_TAGS{all}}, | |
453 | \& grep {!$seen{$_}++} @{$EXPORT_TAGS{$_}} foreach keys %EXPORT_TAGS; | |
454 | \& } | |
455 | .Ve | |
456 | .PP | |
457 | \&\s-1CGI\s0.pm creates an \*(L":all\*(R" tag which contains some (but not really | |
458 | all) of its categories. That could be done with one small | |
459 | change: | |
460 | .PP | |
461 | .Vb 4 | |
462 | \& # add some of the other ":class" tags to the ":all" class, | |
463 | \& # deleting duplicates | |
464 | \& { | |
465 | \& my %seen; | |
466 | .Ve | |
467 | .PP | |
468 | .Vb 4 | |
469 | \& push @{$EXPORT_TAGS{all}}, | |
470 | \& grep {!$seen{$_}++} @{$EXPORT_TAGS{$_}} | |
471 | \& foreach qw/html2 html3 netscape form cgi internal/; | |
472 | \& } | |
473 | .Ve | |
474 | .PP | |
475 | Note that the tag names in \f(CW%EXPORT_TAGS\fR don't have the leading ':'. | |
476 | .ie n .Sh """AUTOLOAD""ed Constants" | |
477 | .el .Sh "\f(CWAUTOLOAD\fPed Constants" | |
478 | .IX Subsection "AUTOLOADed Constants" | |
479 | Many modules make use of \f(CW\*(C`AUTOLOAD\*(C'\fRing for constant subroutines to | |
480 | avoid having to compile and waste memory on rarely used values (see | |
481 | perlsub for details on constant subroutines). Calls to such | |
482 | constant subroutines are not optimized away at compile time because | |
483 | they can't be checked at compile time for constancy. | |
484 | .PP | |
485 | Even if a prototype is available at compile time, the body of the | |
486 | subroutine is not (it hasn't been \f(CW\*(C`AUTOLOAD\*(C'\fRed yet). perl needs to | |
487 | examine both the \f(CW\*(C`()\*(C'\fR prototype and the body of a subroutine at | |
488 | compile time to detect that it can safely replace calls to that | |
489 | subroutine with the constant value. | |
490 | .PP | |
491 | A workaround for this is to call the constants once in a \f(CW\*(C`BEGIN\*(C'\fR block: | |
492 | .PP | |
493 | .Vb 1 | |
494 | \& package My ; | |
495 | .Ve | |
496 | .PP | |
497 | .Vb 1 | |
498 | \& use Socket ; | |
499 | .Ve | |
500 | .PP | |
501 | .Vb 3 | |
502 | \& foo( SO_LINGER ); ## SO_LINGER NOT optimized away; called at runtime | |
503 | \& BEGIN { SO_LINGER } | |
504 | \& foo( SO_LINGER ); ## SO_LINGER optimized away at compile time. | |
505 | .Ve | |
506 | .PP | |
507 | This forces the \f(CW\*(C`AUTOLOAD\*(C'\fR for \f(CW\*(C`SO_LINGER\*(C'\fR to take place before | |
508 | \&\s-1SO_LINGER\s0 is encountered later in \f(CW\*(C`My\*(C'\fR package. | |
509 | .PP | |
510 | If you are writing a package that \f(CW\*(C`AUTOLOAD\*(C'\fRs, consider forcing | |
511 | an \f(CW\*(C`AUTOLOAD\*(C'\fR for any constants explicitly imported by other packages | |
512 | or which are usually used when your package is \f(CW\*(C`use\*(C'\fRd. |