Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / man / man3 / Exporter.3
CommitLineData
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"
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
146or
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
154In 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"
162The Exporter module implements an \f(CW\*(C`import\*(C'\fR method which allows a module
163to export functions and variables to its users' namespaces. Many modules
164use Exporter rather than implementing their own \f(CW\*(C`import\*(C'\fR method because
165Exporter provides a highly flexible interface, with an implementation optimised
166for the common case.
167.PP
168Perl 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
170in perlfunc and perlmod. Understanding the concept of
171modules and how the \f(CW\*(C`use\*(C'\fR statement operates is important to
172understanding the Exporter.
173.Sh "How to Export"
174.IX Subsection "How to Export"
175The arrays \f(CW@EXPORT\fR and \f(CW@EXPORT_OK\fR in a module hold lists of
176symbols that are going to be exported into the users name space by
177default, or which they can request to be exported, respectively. The
178symbols can represent functions, scalars, arrays, hashes, or typeglobs.
179The symbols must be given by full name with the exception that the
180ampersand 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
187If you are only exporting function names it is recommended to omit the
188ampersand, as the implementation is faster this way.
189.Sh "Selecting What To Export"
190.IX Subsection "Selecting What To Export"
191Do \fBnot\fR export method names!
192.PP
193Do \fBnot\fR export anything else by default without a good reason!
194.PP
195Exports pollute the namespace of the module user. If you must export
196try to use \f(CW@EXPORT_OK\fR in preference to \f(CW@EXPORT\fR and avoid short or
197common symbol names to reduce the risk of name clashes.
198.PP
199Generally anything not exported is still accessible from outside the
200module using the ModuleName::item_name (or \f(CW$blessed_ref\fR\->method)
201syntax. By convention you can use a leading underscore on names to
202informally 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
212However if you use them for methods it is up to you to figure out
213how to make inheritance work.)
214.PP
215As a general rule, if the module is trying to be object oriented
216then 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
218method names use barewords in preference to names prefixed with
219ampersands for the export lists.
220.PP
221Other module design guidelines can be found in perlmod.
222.Sh "How to Import"
223.IX Subsection "How to Import"
224In other files which wish to use your module there are three basic ways for
225them 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;"
229This imports all the symbols from ModuleName's \f(CW@EXPORT\fR into the namespace
230of 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 ();"
234This 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(...);"
238This imports only the symbols listed by the caller into their namespace.
239All listed symbols must be in your \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR, else an error
240occurs. The advanced export features of Exporter are accessed like this,
241but with list entries that are syntactically distinct from symbol names.
242.PP
243Unless you want to use its advanced features, this is probably all you
244need 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"
249If any of the entries in an import list begins with !, : or / then
250the list is treated as a series of specifications which either add to
251or delete from the list of names to import. They are processed left to
252right. 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
261A leading ! indicates that matching names should be deleted from the
262list of names to import. If the first specification is a deletion it
263is treated as though preceded by :DEFAULT. If you just want to import
264extra names in addition to the default set you will still need to
265include :DEFAULT explicitly.
266.PP
267e.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
280An application using Module can say something like:
281.PP
282.Vb 1
283\& use Module qw(:DEFAULT :T2 !B3 A3);
284.Ve
285.PP
286Other 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
293Remember that most patterns (using //) will need to be anchored
294with a leading ^, e.g., \f(CW\*(C`/^EXIT/\*(C'\fR rather than \f(CW\*(C`/EXIT/\*(C'\fR.
295.PP
296You can say \f(CW\*(C`BEGIN { $Exporter::Verbose=1 }\*(C'\fR to see how the
297specifications are being processed and what is actually being imported
298into modules.
299.Sh "Exporting without using Exporter's import method"
300.IX Subsection "Exporting without using Exporter's import method"
301Exporter has a special method, 'export_to_level' which is used in situations
302where you can't directly call Exporter's import method. The export_to_level
303method looks like:
304.PP
305.Vb 1
306\& MyPackage->export_to_level($where_to_export, $package, @what_to_export);
307.Ve
308.PP
309where \f(CW$where_to_export\fR is an integer telling how far up the calling stack
310to export your symbols, and \f(CW@what_to_export\fR is an array telling what
311symbols *to* export (usually this is \f(CW@_\fR). The \f(CW$package\fR argument is
312currently unused.
313.PP
314For example, suppose that you have a module, A, which already has an
315import 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
333and you want to Export symbol \f(CW$A::b\fR back to the module that called
334package A. Since Exporter relies on the import method to work, via
335inheritance, as it stands \fIExporter::import()\fR will never get called.
336Instead, 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
352This will export the symbols one level 'above' the current package \- ie: to
353the program or module that used package A.
354.PP
355Note: 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"
359By including Exporter in your \f(CW@ISA\fR you inherit an Exporter's \fIimport()\fR method
360but you also inherit several other helper methods which you probably don't
361want. To avoid this you can do
362.PP
363.Vb 2
364\& package YourModule;
365\& use Exporter qw( import );
366.Ve
367.PP
368which will export Exporter's own \fIimport()\fR method into YourModule.
369Everything 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"
373The Exporter module will convert an attempt to import a number from a
374module into a call to \f(CW$module_name\fR\->require_version($value). This can
375be used to validate that the version of the module being used is
376greater than or equal to the required version.
377.PP
378The Exporter module supplies a default require_version method which
379checks the value of \f(CW$VERSION\fR in the exporting module.
380.PP
381Since the default require_version method treats the \f(CW$VERSION\fR number as
382a simple numeric value it will regard version 1.10 as lower than
3831.9. For this reason it is strongly recommended that you use numbers
384with at least two decimal places, e.g., 1.09.
385.Sh "Managing Unknown Symbols"
386.IX Subsection "Managing Unknown Symbols"
387In some situations you may want to prevent certain symbols from being
388exported. Typically this applies to extensions which have functions
389or constants that may not exist on some systems.
390.PP
391The names of any symbols that cannot be exported should be listed
392in the \f(CW@EXPORT_FAIL\fR array.
393.PP
394If a module attempts to import any of these symbols the Exporter
395will give the module an opportunity to handle the situation before
396generating an error. The Exporter will call an export_fail method
397with a list of the failed symbols:
398.PP
399.Vb 1
400\& @failed_symbols = $module_name->export_fail(@failed_symbols);
401.Ve
402.PP
403If the export_fail method returns an empty list then no error is
404recorded and all the requested symbols are exported. If the returned
405list is not empty then an error is generated for each symbol and the
406export fails. The Exporter provides a default export_fail method which
407simply returns the list unchanged.
408.PP
409Uses for the export_fail method include giving better error messages
410for some symbols and performing lazy architectural checks (put more
411symbols into \f(CW@EXPORT_FAIL\fR by default and then take them out if someone
412actually tries to use them and an expensive check shows that they are
413usable on that platform).
414.Sh "Tag Handling Utility Functions"
415.IX Subsection "Tag Handling Utility Functions"
416Since 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
418you 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
429Any names which are not tags are added to \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR
430unchanged but will trigger a warning (with \f(CW\*(C`\-w\*(C'\fR) to avoid misspelt tags
431names being silently added to \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR. Future versions
432may make this a fatal error.
433.Sh "Generating combined tags"
434.IX Subsection "Generating combined tags"
435If several symbol categories exist in \f(CW%EXPORT_TAGS\fR, it's usually
436useful to create the utility \*(L":all\*(R" to simplify \*(L"use\*(R" statements.
437.PP
438The 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
458all) of its categories. That could be done with one small
459change:
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
475Note 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"
479Many modules make use of \f(CW\*(C`AUTOLOAD\*(C'\fRing for constant subroutines to
480avoid having to compile and waste memory on rarely used values (see
481perlsub for details on constant subroutines). Calls to such
482constant subroutines are not optimized away at compile time because
483they can't be checked at compile time for constancy.
484.PP
485Even if a prototype is available at compile time, the body of the
486subroutine is not (it hasn't been \f(CW\*(C`AUTOLOAD\*(C'\fRed yet). perl needs to
487examine both the \f(CW\*(C`()\*(C'\fR prototype and the body of a subroutine at
488compile time to detect that it can safely replace calls to that
489subroutine with the constant value.
490.PP
491A 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
507This 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
510If you are writing a package that \f(CW\*(C`AUTOLOAD\*(C'\fRs, consider forcing
511an \f(CW\*(C`AUTOLOAD\*(C'\fR for any constants explicitly imported by other packages
512or which are usually used when your package is \f(CW\*(C`use\*(C'\fRd.