1Exporter(3pm)          Perl Programmers Reference Guide          Exporter(3pm)
2
3
4

NAME

6       Exporter - Implements default import method for modules
7

SYNOPSIS

9       In module YourModule.pm:
10
11         package YourModule;
12         require Exporter;
13         @ISA = qw(Exporter);
14         @EXPORT_OK = qw(munge frobnicate);  # symbols to export on request
15
16       or
17
18         package YourModule;
19         use Exporter 'import'; # gives you Exporter's import() method directly
20         @EXPORT_OK = qw(munge frobnicate);  # symbols to export on request
21
22       In other files which wish to use YourModule:
23
24         use ModuleName qw(frobnicate);      # import listed symbols
25         frobnicate ($left, $right)          # calls YourModule::frobnicate
26

DESCRIPTION

28       The Exporter module implements an "import" method which allows a module
29       to export functions and variables to its users' namespaces. Many mod‐
30       ules use Exporter rather than implementing their own "import" method
31       because Exporter provides a highly flexible interface, with an imple‐
32       mentation optimised for the common case.
33
34       Perl automatically calls the "import" method when processing a "use"
35       statement for a module. Modules and "use" are documented in perlfunc
36       and perlmod. Understanding the concept of modules and how the "use"
37       statement operates is important to understanding the Exporter.
38
39       How to Export
40
41       The arrays @EXPORT and @EXPORT_OK in a module hold lists of symbols
42       that are going to be exported into the users name space by default, or
43       which they can request to be exported, respectively.  The symbols can
44       represent functions, scalars, arrays, hashes, or typeglobs.  The sym‐
45       bols must be given by full name with the exception that the ampersand
46       in front of a function is optional, e.g.
47
48           @EXPORT    = qw(afunc $scalar @array);   # afunc is a function
49           @EXPORT_OK = qw(&bfunc %hash *typeglob); # explicit prefix on &bfunc
50
51       If you are only exporting function names it is recommended to omit the
52       ampersand, as the implementation is faster this way.
53
54       Selecting What To Export
55
56       Do not export method names!
57
58       Do not export anything else by default without a good reason!
59
60       Exports pollute the namespace of the module user.  If you must export
61       try to use @EXPORT_OK in preference to @EXPORT and avoid short or com‐
62       mon symbol names to reduce the risk of name clashes.
63
64       Generally anything not exported is still accessible from outside the
65       module using the ModuleName::item_name (or $blessed_ref->method) syn‐
66       tax.  By convention you can use a leading underscore on names to infor‐
67       mally indicate that they are 'internal' and not for public use.
68
69       (It is actually possible to get private functions by saying:
70
71         my $subref = sub { ... };
72         $subref->(@args);            # Call it as a function
73         $obj->$subref(@args);        # Use it as a method
74
75       However if you use them for methods it is up to you to figure out how
76       to make inheritance work.)
77
78       As a general rule, if the module is trying to be object oriented then
79       export nothing. If it's just a collection of functions then @EXPORT_OK
80       anything but use @EXPORT with caution. For function and method names
81       use barewords in preference to names prefixed with ampersands for the
82       export lists.
83
84       Other module design guidelines can be found in perlmod.
85
86       How to Import
87
88       In other files which wish to use your module there are three basic ways
89       for them to load your module and import its symbols:
90
91       "use ModuleName;"
92           This imports all the symbols from ModuleName's @EXPORT into the
93           namespace of the "use" statement.
94
95       "use ModuleName ();"
96           This causes perl to load your module but does not import any sym‐
97           bols.
98
99       "use ModuleName qw(...);"
100           This imports only the symbols listed by the caller into their
101           namespace.  All listed symbols must be in your @EXPORT or
102           @EXPORT_OK, else an error occurs. The advanced export features of
103           Exporter are accessed like this, but with list entries that are
104           syntactically distinct from symbol names.
105
106       Unless you want to use its advanced features, this is probably all you
107       need to know to use Exporter.
108

Advanced features

110       Specialised Import Lists
111
112       If any of the entries in an import list begins with !, : or / then the
113       list is treated as a series of specifications which either add to or
114       delete from the list of names to import. They are processed left to
115       right. Specifications are in the form:
116
117           [!]name         This name only
118           [!]:DEFAULT     All names in @EXPORT
119           [!]:tag         All names in $EXPORT_TAGS{tag} anonymous list
120           [!]/pattern/    All names in @EXPORT and @EXPORT_OK which match
121
122       A leading ! indicates that matching names should be deleted from the
123       list of names to import.  If the first specification is a deletion it
124       is treated as though preceded by :DEFAULT. If you just want to import
125       extra names in addition to the default set you will still need to
126       include :DEFAULT explicitly.
127
128       e.g., Module.pm defines:
129
130           @EXPORT      = qw(A1 A2 A3 A4 A5);
131           @EXPORT_OK   = qw(B1 B2 B3 B4 B5);
132           %EXPORT_TAGS = (T1 => [qw(A1 A2 B1 B2)], T2 => [qw(A1 A2 B3 B4)]);
133
134           Note that you cannot use tags in @EXPORT or @EXPORT_OK.
135           Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK.
136
137       An application using Module can say something like:
138
139           use Module qw(:DEFAULT :T2 !B3 A3);
140
141       Other examples include:
142
143           use Socket qw(!/^[AP]F_/ !SOMAXCONN !SOL_SOCKET);
144           use POSIX  qw(:errno_h :termios_h !TCSADRAIN !/^EXIT/);
145
146       Remember that most patterns (using //) will need to be anchored with a
147       leading ^, e.g., "/^EXIT/" rather than "/EXIT/".
148
149       You can say "BEGIN { $Exporter::Verbose=1 }" to see how the specifica‐
150       tions are being processed and what is actually being imported into mod‐
151       ules.
152
153       Exporting without using Exporter's import method
154
155       Exporter has a special method, 'export_to_level' which is used in situ‐
156       ations where you can't directly call Exporter's import method. The
157       export_to_level method looks like:
158
159           MyPackage->export_to_level($where_to_export, $package, @what_to_export);
160
161       where $where_to_export is an integer telling how far up the calling
162       stack to export your symbols, and @what_to_export is an array telling
163       what symbols *to* export (usually this is @_).  The $package argument
164       is currently unused.
165
166       For example, suppose that you have a module, A, which already has an
167       import function:
168
169           package A;
170
171           @ISA = qw(Exporter);
172           @EXPORT_OK = qw ($b);
173
174           sub import
175           {
176               $A::b = 1;     # not a very useful import method
177           }
178
179       and you want to Export symbol $A::b back to the module that called
180       package A. Since Exporter relies on the import method to work, via
181       inheritance, as it stands Exporter::import() will never get called.
182       Instead, say the following:
183
184           package A;
185           @ISA = qw(Exporter);
186           @EXPORT_OK = qw ($b);
187
188           sub import
189           {
190               $A::b = 1;
191               A->export_to_level(1, @_);
192           }
193
194       This will export the symbols one level 'above' the current package -
195       ie: to the program or module that used package A.
196
197       Note: Be careful not to modify @_ at all before you call
198       export_to_level - or people using your package will get very unex‐
199       plained results!
200
201       Exporting without inheriting from Exporter
202
203       By including Exporter in your @ISA you inherit an Exporter's import()
204       method but you also inherit several other helper methods which you
205       probably don't want. To avoid this you can do
206
207         package YourModule;
208         use Exporter qw( import );
209
210       which will export Exporter's own import() method into YourModule.
211       Everything will work as before but you won't need to include Exporter
212       in @YourModule::ISA.
213
214       Module Version Checking
215
216       The Exporter module will convert an attempt to import a number from a
217       module into a call to $module_name->require_version($value). This can
218       be used to validate that the version of the module being used is
219       greater than or equal to the required version.
220
221       The Exporter module supplies a default require_version method which
222       checks the value of $VERSION in the exporting module.
223
224       Since the default require_version method treats the $VERSION number as
225       a simple numeric value it will regard version 1.10 as lower than 1.9.
226       For this reason it is strongly recommended that you use numbers with at
227       least two decimal places, e.g., 1.09.
228
229       Managing Unknown Symbols
230
231       In some situations you may want to prevent certain symbols from being
232       exported. Typically this applies to extensions which have functions or
233       constants that may not exist on some systems.
234
235       The names of any symbols that cannot be exported should be listed in
236       the @EXPORT_FAIL array.
237
238       If a module attempts to import any of these symbols the Exporter will
239       give the module an opportunity to handle the situation before generat‐
240       ing an error. The Exporter will call an export_fail method with a list
241       of the failed symbols:
242
243         @failed_symbols = $module_name->export_fail(@failed_symbols);
244
245       If the export_fail method returns an empty list then no error is
246       recorded and all the requested symbols are exported. If the returned
247       list is not empty then an error is generated for each symbol and the
248       export fails. The Exporter provides a default export_fail method which
249       simply returns the list unchanged.
250
251       Uses for the export_fail method include giving better error messages
252       for some symbols and performing lazy architectural checks (put more
253       symbols into @EXPORT_FAIL by default and then take them out if someone
254       actually tries to use them and an expensive check shows that they are
255       usable on that platform).
256
257       Tag Handling Utility Functions
258
259       Since the symbols listed within %EXPORT_TAGS must also appear in either
260       @EXPORT or @EXPORT_OK, two utility functions are provided which allow
261       you to easily add tagged sets of symbols to @EXPORT or @EXPORT_OK:
262
263         %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]);
264
265         Exporter::export_tags('foo');     # add aa, bb and cc to @EXPORT
266         Exporter::export_ok_tags('bar');  # add aa, cc and dd to @EXPORT_OK
267
268       Any names which are not tags are added to @EXPORT or @EXPORT_OK
269       unchanged but will trigger a warning (with "-w") to avoid misspelt tags
270       names being silently added to @EXPORT or @EXPORT_OK. Future versions
271       may make this a fatal error.
272
273       Generating combined tags
274
275       If several symbol categories exist in %EXPORT_TAGS, it's usually useful
276       to create the utility ":all" to simplify "use" statements.
277
278       The simplest way to do this is:
279
280         %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]);
281
282         # add all the other ":class" tags to the ":all" class,
283         # deleting duplicates
284         {
285           my %seen;
286
287           push @{$EXPORT_TAGS{all}},
288             grep {!$seen{$_}++} @{$EXPORT_TAGS{$_}} foreach keys %EXPORT_TAGS;
289         }
290
291       CGI.pm creates an ":all" tag which contains some (but not really all)
292       of its categories.  That could be done with one small change:
293
294         # add some of the other ":class" tags to the ":all" class,
295         # deleting duplicates
296         {
297           my %seen;
298
299           push @{$EXPORT_TAGS{all}},
300             grep {!$seen{$_}++} @{$EXPORT_TAGS{$_}}
301               foreach qw/html2 html3 netscape form cgi internal/;
302         }
303
304       Note that the tag names in %EXPORT_TAGS don't have the leading ':'.
305
306       "AUTOLOAD"ed Constants
307
308       Many modules make use of "AUTOLOAD"ing for constant subroutines to
309       avoid having to compile and waste memory on rarely used values (see
310       perlsub for details on constant subroutines).  Calls to such constant
311       subroutines are not optimized away at compile time because they can't
312       be checked at compile time for constancy.
313
314       Even if a prototype is available at compile time, the body of the sub‐
315       routine is not (it hasn't been "AUTOLOAD"ed yet). perl needs to examine
316       both the "()" prototype and the body of a subroutine at compile time to
317       detect that it can safely replace calls to that subroutine with the
318       constant value.
319
320       A workaround for this is to call the constants once in a "BEGIN" block:
321
322          package My ;
323
324          use Socket ;
325
326          foo( SO_LINGER );     ## SO_LINGER NOT optimized away; called at runtime
327          BEGIN { SO_LINGER }
328          foo( SO_LINGER );     ## SO_LINGER optimized away at compile time.
329
330       This forces the "AUTOLOAD" for "SO_LINGER" to take place before
331       SO_LINGER is encountered later in "My" package.
332
333       If you are writing a package that "AUTOLOAD"s, consider forcing an
334       "AUTOLOAD" for any constants explicitly imported by other packages or
335       which are usually used when your package is "use"d.
336
337
338
339perl v5.8.8                       2001-09-21                     Exporter(3pm)
Impressum