This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
more basic support for building modules under Windows 95/98
[perl5.git] / lib / Exporter.pm
CommitLineData
8990e307
LW
1package Exporter;
2
748a9306 3require 5.001;
8990e307 4
a0d0e21e 5$ExportLevel = 0;
4af1b167 6$Verbose ||= 0;
2b5b2650 7
4af1b167
IZ
8sub export_to_level {
9 require Exporter::Heavy;
10 goto &heavy_export_to_level;
84902520
TB
11}
12
4af1b167
IZ
13sub export {
14 require Exporter::Heavy;
15 goto &heavy_export;
748a9306
LW
16}
17
4af1b167
IZ
18sub export_tags {
19 require Exporter::Heavy;
20 _push_tags((caller)[0], "EXPORT", \@_);
2b5b2650 21}
22
4af1b167
IZ
23sub export_ok_tags {
24 require Exporter::Heavy;
25 _push_tags((caller)[0], "EXPORT_OK", \@_);
a0d0e21e
LW
26}
27
4af1b167
IZ
28sub import {
29 my $pkg = shift;
30 my $callpkg = caller($ExportLevel);
31 *exports = *{"$pkg\::EXPORT"};
32 # We *need* to treat @{"$pkg\::EXPORT_FAIL"} since Carp uses it :-(
33 *fail = *{"$pkg\::EXPORT_FAIL"};
34 return export $pkg, $callpkg, @_
35 if $Verbose or $Debug or @fail > 1;
36 my $args = @_ or @_ = @exports;
37
38 if ($args and not %exports) {
39 foreach my $sym (@exports, @{"$pkg\::EXPORT_OK"}) {
40 $sym =~ s/^&//;
41 $exports{$sym} = 1;
3221d3b0 42 }
4af1b167
IZ
43 }
44 if ($Verbose or $Debug
45 or grep {/\W/ or $args and not exists $exports{$_}
46 or @fail and $_ eq $fail[0]
47 or (@{"$pkg\::EXPORT_OK"}
48 and $_ eq ${"$pkg\::EXPORT_OK"}[0])} @_) {
49 return export $pkg, $callpkg, ($args ? @_ : ());
50 }
51 #local $SIG{__WARN__} = sub {require Carp; goto &Carp::carp};
52 local $SIG{__WARN__} =
53 sub {require Carp; local $Carp::CarpLevel = 1; &Carp::carp};
54 foreach $sym (@_) {
55 # shortcut for the common case of no type character
56 *{"$callpkg\::$sym"} = \&{"$pkg\::$sym"};
57 }
e50aee73
AD
58}
59
8990e307 601;
2b5b2650 61
62# A simple self test harness. Change 'require Carp' to 'use Carp ()' for testing.
63# package main; eval(join('',<DATA>)) or die $@ unless caller;
64__END__
65package Test;
66$INC{'Exporter.pm'} = 1;
67@ISA = qw(Exporter);
68@EXPORT = qw(A1 A2 A3 A4 A5);
69@EXPORT_OK = qw(B1 B2 B3 B4 B5);
70%EXPORT_TAGS = (T1=>[qw(A1 A2 B1 B2)], T2=>[qw(A1 A2 B3 B4)], T3=>[qw(X3)]);
71@EXPORT_FAIL = qw(B4);
72Exporter::export_ok_tags('T3', 'unknown_tag');
73sub export_fail {
74 map { "Test::$_" } @_ # edit symbols just as an example
75}
76
77package main;
78$Exporter::Verbose = 1;
79#import Test;
80#import Test qw(X3); # export ok via export_ok_tags()
81#import Test qw(:T1 !A2 /5/ !/3/ B5);
82import Test qw(:T2 !B4);
83import Test qw(:T2); # should fail
841;
85
86=head1 NAME
87
88Exporter - Implements default import method for modules
89
90=head1 SYNOPSIS
91
92In module ModuleName.pm:
93
94 package ModuleName;
95 require Exporter;
96 @ISA = qw(Exporter);
97
98 @EXPORT = qw(...); # symbols to export by default
99 @EXPORT_OK = qw(...); # symbols to export on request
100 %EXPORT_TAGS = tag => [...]; # define names for sets of symbols
101
102In other files which wish to use ModuleName:
103
104 use ModuleName; # import default symbols into my package
105
106 use ModuleName qw(...); # import listed symbols into my package
107
108 use ModuleName (); # do not import any symbols
109
110=head1 DESCRIPTION
111
112The Exporter module implements a default C<import> method which
68dc0745 113many modules choose to inherit rather than implement their own.
2b5b2650 114
115Perl automatically calls the C<import> method when processing a
116C<use> statement for a module. Modules and C<use> are documented
117in L<perlfunc> and L<perlmod>. Understanding the concept of
118modules and how the C<use> statement operates is important to
119understanding the Exporter.
120
121=head2 Selecting What To Export
122
123Do B<not> export method names!
124
125Do B<not> export anything else by default without a good reason!
126
127Exports pollute the namespace of the module user. If you must export
128try to use @EXPORT_OK in preference to @EXPORT and avoid short or
129common symbol names to reduce the risk of name clashes.
130
131Generally anything not exported is still accessible from outside the
1fef88e7 132module using the ModuleName::item_name (or $blessed_ref-E<gt>method)
2b5b2650 133syntax. By convention you can use a leading underscore on names to
134informally indicate that they are 'internal' and not for public use.
135
136(It is actually possible to get private functions by saying:
137
138 my $subref = sub { ... };
139 &$subref;
140
141But there's no way to call that directly as a method, since a method
142must have a name in the symbol table.)
143
144As a general rule, if the module is trying to be object oriented
145then export nothing. If it's just a collection of functions then
146@EXPORT_OK anything but use @EXPORT with caution.
147
148Other module design guidelines can be found in L<perlmod>.
149
150=head2 Specialised Import Lists
151
152If the first entry in an import list begins with !, : or / then the
153list is treated as a series of specifications which either add to or
154delete from the list of names to import. They are processed left to
155right. Specifications are in the form:
156
157 [!]name This name only
158 [!]:DEFAULT All names in @EXPORT
159 [!]:tag All names in $EXPORT_TAGS{tag} anonymous list
160 [!]/pattern/ All names in @EXPORT and @EXPORT_OK which match
161
162A leading ! indicates that matching names should be deleted from the
163list of names to import. If the first specification is a deletion it
164is treated as though preceded by :DEFAULT. If you just want to import
165extra names in addition to the default set you will still need to
166include :DEFAULT explicitly.
167
168e.g., Module.pm defines:
169
170 @EXPORT = qw(A1 A2 A3 A4 A5);
171 @EXPORT_OK = qw(B1 B2 B3 B4 B5);
172 %EXPORT_TAGS = (T1 => [qw(A1 A2 B1 B2)], T2 => [qw(A1 A2 B3 B4)]);
173
174 Note that you cannot use tags in @EXPORT or @EXPORT_OK.
175 Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK.
176
177An application using Module can say something like:
178
179 use Module qw(:DEFAULT :T2 !B3 A3);
180
181Other examples include:
182
183 use Socket qw(!/^[AP]F_/ !SOMAXCONN !SOL_SOCKET);
184 use POSIX qw(:errno_h :termios_h !TCSADRAIN !/^EXIT/);
185
186Remember that most patterns (using //) will need to be anchored
187with a leading ^, e.g., C</^EXIT/> rather than C</EXIT/>.
188
189You can say C<BEGIN { $Exporter::Verbose=1 }> to see how the
190specifications are being processed and what is actually being imported
191into modules.
192
84902520
TB
193=head2 Exporting without using Export's import method
194
195Exporter has a special method, 'export_to_level' which is used in situations
196where you can't directly call Export's import method. The export_to_level
197method looks like:
198
ba5725f8 199MyPackage->export_to_level($where_to_export, $package, @what_to_export);
84902520
TB
200
201where $where_to_export is an integer telling how far up the calling stack
202to export your symbols, and @what_to_export is an array telling what
ba5725f8
GS
203symbols *to* export (usually this is @_). The $package argument is
204currently unused.
84902520
TB
205
206For example, suppose that you have a module, A, which already has an
207import function:
208
209package A;
210
211@ISA = qw(Exporter);
212@EXPORT_OK = qw ($b);
213
214sub import
215{
216 $A::b = 1; # not a very useful import method
217}
218
219and you want to Export symbol $A::b back to the module that called
220package A. Since Exporter relies on the import method to work, via
221inheritance, as it stands Exporter::import() will never get called.
222Instead, say the following:
223
224package A;
225@ISA = qw(Exporter);
226@EXPORT_OK = qw ($b);
227
228sub import
229{
230 $A::b = 1;
231 A->export_to_level(1, @_);
232}
233
234This will export the symbols one level 'above' the current package - ie: to
235the program or module that used package A.
236
237Note: Be careful not to modify '@_' at all before you call export_to_level
238- or people using your package will get very unexplained results!
239
240
2b5b2650 241=head2 Module Version Checking
242
243The Exporter module will convert an attempt to import a number from a
1fef88e7 244module into a call to $module_name-E<gt>require_version($value). This can
2b5b2650 245be used to validate that the version of the module being used is
246greater than or equal to the required version.
247
248The Exporter module supplies a default require_version method which
249checks the value of $VERSION in the exporting module.
250
251Since the default require_version method treats the $VERSION number as
d5e40bcc 252a simple numeric value it will regard version 1.10 as lower than
2531.9. For this reason it is strongly recommended that you use numbers
254with at least two decimal places, e.g., 1.09.
2b5b2650 255
256=head2 Managing Unknown Symbols
257
258In some situations you may want to prevent certain symbols from being
259exported. Typically this applies to extensions which have functions
260or constants that may not exist on some systems.
261
262The names of any symbols that cannot be exported should be listed
263in the C<@EXPORT_FAIL> array.
264
7a2e2cd6 265If a module attempts to import any of these symbols the Exporter
2b5b2650 266will give the module an opportunity to handle the situation before
267generating an error. The Exporter will call an export_fail method
268with a list of the failed symbols:
269
270 @failed_symbols = $module_name->export_fail(@failed_symbols);
271
272If the export_fail method returns an empty list then no error is
273recorded and all the requested symbols are exported. If the returned
274list is not empty then an error is generated for each symbol and the
275export fails. The Exporter provides a default export_fail method which
276simply returns the list unchanged.
277
278Uses for the export_fail method include giving better error messages
279for some symbols and performing lazy architectural checks (put more
280symbols into @EXPORT_FAIL by default and then take them out if someone
281actually tries to use them and an expensive check shows that they are
282usable on that platform).
283
284=head2 Tag Handling Utility Functions
285
286Since the symbols listed within %EXPORT_TAGS must also appear in either
287@EXPORT or @EXPORT_OK, two utility functions are provided which allow
288you to easily add tagged sets of symbols to @EXPORT or @EXPORT_OK:
289
290 %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]);
291
292 Exporter::export_tags('foo'); # add aa, bb and cc to @EXPORT
293 Exporter::export_ok_tags('bar'); # add aa, cc and dd to @EXPORT_OK
294
295Any names which are not tags are added to @EXPORT or @EXPORT_OK
d5e40bcc 296unchanged but will trigger a warning (with C<-w>) to avoid misspelt tags
2b5b2650 297names being silently added to @EXPORT or @EXPORT_OK. Future versions
298may make this a fatal error.
299
300=cut