Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Exporter.3
CommitLineData
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"
134Exporter \- Implements default import method for modules
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137In 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
146In 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"
154The Exporter module implements an \f(CW\*(C`import\*(C'\fR method which allows a module
155to export functions and variables to its users' namespaces. Many modules
156use Exporter rather than implementing their own \f(CW\*(C`import\*(C'\fR method because
157Exporter provides a highly flexible interface, with an implementation optimised
158for the common case.
159.PP
160Perl 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
162in perlfunc and perlmod. Understanding the concept of
163modules and how the \f(CW\*(C`use\*(C'\fR statement operates is important to
164understanding the Exporter.
165.Sh "How to Export"
166.IX Subsection "How to Export"
167The arrays \f(CW@EXPORT\fR and \f(CW@EXPORT_OK\fR in a module hold lists of
168symbols that are going to be exported into the users name space by
169default, or which they can request to be exported, respectively. The
170symbols can represent functions, scalars, arrays, hashes, or typeglobs.
171The symbols must be given by full name with the exception that the
172ampersand 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
179If you are only exporting function names it is recommended to omit the
180ampersand, as the implementation is faster this way.
181.Sh "Selecting What To Export"
182.IX Subsection "Selecting What To Export"
183Do \fBnot\fR export method names!
184.PP
185Do \fBnot\fR export anything else by default without a good reason!
186.PP
187Exports pollute the namespace of the module user. If you must export
188try to use \f(CW@EXPORT_OK\fR in preference to \f(CW@EXPORT\fR and avoid short or
189common symbol names to reduce the risk of name clashes.
190.PP
191Generally anything not exported is still accessible from outside the
192module using the ModuleName::item_name (or \f(CW$blessed_ref\fR\->method)
193syntax. By convention you can use a leading underscore on names to
194informally 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
204However if you use them for methods it is up to you to figure out
205how to make inheritance work.)
206.PP
207As a general rule, if the module is trying to be object oriented
208then 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
210method names use barewords in preference to names prefixed with
211ampersands for the export lists.
212.PP
213Other module design guidelines can be found in perlmod.
214.Sh "How to Import"
215.IX Subsection "How to Import"
216In other files which wish to use your module there are three basic ways for
217them 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;"
221This imports all the symbols from ModuleName's \f(CW@EXPORT\fR into the namespace
222of 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 ();"
226This 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(...);"
230This imports only the symbols listed by the caller into their namespace.
231All listed symbols must be in your \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR, else an error
232occurs. The advanced export features of Exporter are accessed like this,
233but with list entries that are syntactically distinct from symbol names.
234.PP
235Unless you want to use its advanced features, this is probably all you
236need 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"
241If the first entry in an import list begins with !, : or / then the
242list is treated as a series of specifications which either add to or
243delete from the list of names to import. They are processed left to
244right. 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
253A leading ! indicates that matching names should be deleted from the
254list of names to import. If the first specification is a deletion it
255is treated as though preceded by :DEFAULT. If you just want to import
256extra names in addition to the default set you will still need to
257include :DEFAULT explicitly.
258.PP
259e.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
272An application using Module can say something like:
273.PP
274.Vb 1
275\& use Module qw(:DEFAULT :T2 !B3 A3);
276.Ve
277.PP
278Other 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
285Remember that most patterns (using //) will need to be anchored
286with a leading ^, e.g., \f(CW\*(C`/^EXIT/\*(C'\fR rather than \f(CW\*(C`/EXIT/\*(C'\fR.
287.PP
288You can say \f(CW\*(C`BEGIN { $Exporter::Verbose=1 }\*(C'\fR to see how the
289specifications are being processed and what is actually being imported
290into modules.
291.Sh "Exporting without using Exporter's import method"
292.IX Subsection "Exporting without using Exporter's import method"
293Exporter has a special method, 'export_to_level' which is used in situations
294where you can't directly call Exporter's import method. The export_to_level
295method looks like:
296.PP
297.Vb 1
298\& MyPackage->export_to_level($where_to_export, $package, @what_to_export);
299.Ve
300.PP
301where \f(CW$where_to_export\fR is an integer telling how far up the calling stack
302to export your symbols, and \f(CW@what_to_export\fR is an array telling what
303symbols *to* export (usually this is \f(CW@_\fR). The \f(CW$package\fR argument is
304currently unused.
305.PP
306For example, suppose that you have a module, A, which already has an
307import 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
325and you want to Export symbol \f(CW$A::b\fR back to the module that called
326package A. Since Exporter relies on the import method to work, via
327inheritance, as it stands \fIExporter::import()\fR will never get called.
328Instead, 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
344This will export the symbols one level 'above' the current package \- ie: to
345the program or module that used package A.
346.PP
347Note: 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"
351The Exporter module will convert an attempt to import a number from a
352module into a call to \f(CW$module_name\fR\->require_version($value). This can
353be used to validate that the version of the module being used is
354greater than or equal to the required version.
355.PP
356The Exporter module supplies a default require_version method which
357checks the value of \f(CW$VERSION\fR in the exporting module.
358.PP
359Since the default require_version method treats the \f(CW$VERSION\fR number as
360a simple numeric value it will regard version 1.10 as lower than
3611.9. For this reason it is strongly recommended that you use numbers
362with at least two decimal places, e.g., 1.09.
363.Sh "Managing Unknown Symbols"
364.IX Subsection "Managing Unknown Symbols"
365In some situations you may want to prevent certain symbols from being
366exported. Typically this applies to extensions which have functions
367or constants that may not exist on some systems.
368.PP
369The names of any symbols that cannot be exported should be listed
370in the \f(CW@EXPORT_FAIL\fR array.
371.PP
372If a module attempts to import any of these symbols the Exporter
373will give the module an opportunity to handle the situation before
374generating an error. The Exporter will call an export_fail method
375with a list of the failed symbols:
376.PP
377.Vb 1
378\& @failed_symbols = $module_name->export_fail(@failed_symbols);
379.Ve
380.PP
381If the export_fail method returns an empty list then no error is
382recorded and all the requested symbols are exported. If the returned
383list is not empty then an error is generated for each symbol and the
384export fails. The Exporter provides a default export_fail method which
385simply returns the list unchanged.
386.PP
387Uses for the export_fail method include giving better error messages
388for some symbols and performing lazy architectural checks (put more
389symbols into \f(CW@EXPORT_FAIL\fR by default and then take them out if someone
390actually tries to use them and an expensive check shows that they are
391usable on that platform).
392.Sh "Tag Handling Utility Functions"
393.IX Subsection "Tag Handling Utility Functions"
394Since 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
396you 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
407Any names which are not tags are added to \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR
408unchanged but will trigger a warning (with \f(CW\*(C`\-w\*(C'\fR) to avoid misspelt tags
409names being silently added to \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR. Future versions
410may make this a fatal error.
411.Sh "Generating combined tags"
412.IX Subsection "Generating combined tags"
413If several symbol categories exist in \f(CW%EXPORT_TAGS\fR, it's usually
414useful to create the utility \*(L":all\*(R" to simplify \*(L"use\*(R" statements.
415.PP
416The 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
436all) of its categories. That could be done with one small
437change:
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
453Note 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"
457Many modules make use of \f(CW\*(C`AUTOLOAD\*(C'\fRing for constant subroutines to
458avoid having to compile and waste memory on rarely used values (see
459perlsub for details on constant subroutines). Calls to such
460constant subroutines are not optimized away at compile time because
461they can't be checked at compile time for constancy.
462.PP
463Even if a prototype is available at compile time, the body of the
464subroutine is not (it hasn't been \f(CW\*(C`AUTOLOAD\*(C'\fRed yet). perl needs to
465examine both the \f(CW\*(C`()\*(C'\fR prototype and the body of a subroutine at
466compile time to detect that it can safely replace calls to that
467subroutine with the constant value.
468.PP
469A 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
485This 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
488If you are writing a package that \f(CW\*(C`AUTOLOAD\*(C'\fRs, consider forcing
489an \f(CW\*(C`AUTOLOAD\*(C'\fR for any constants explicitly imported by other packages
490or which are usually used when your package is \f(CW\*(C`use\*(C'\fRd.