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