Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 lines
14 KiB

  1. package Exporter;
  2. require 5.001;
  3. #
  4. # We go to a lot of trouble not to 'require Carp' at file scope,
  5. # because Carp requires Exporter, and something has to give.
  6. #
  7. $ExportLevel = 0;
  8. $Verbose = 0 unless $Verbose;
  9. sub export {
  10. # First make import warnings look like they're coming from the "use".
  11. local $SIG{__WARN__} = sub {
  12. my $text = shift;
  13. if ($text =~ s/ at \S*Exporter.pm line \d+.*\n//) {
  14. require Carp;
  15. local $Carp::CarpLevel = 1; # ignore package calling us too.
  16. Carp::carp($text);
  17. }
  18. else {
  19. warn $text;
  20. }
  21. };
  22. local $SIG{__DIE__} = sub {
  23. require Carp;
  24. local $Carp::CarpLevel = 1; # ignore package calling us too.
  25. Carp::croak("$_[0]Illegal null symbol in \@${1}::EXPORT")
  26. if $_[0] =~ /^Unable to create sub named "(.*?)::"/;
  27. };
  28. my($pkg, $callpkg, @imports) = @_;
  29. my($type, $sym, $oops);
  30. *exports = *{"${pkg}::EXPORT"};
  31. if (@imports) {
  32. if (!%exports) {
  33. grep(s/^&//, @exports);
  34. @exports{@exports} = (1) x @exports;
  35. my $ok = \@{"${pkg}::EXPORT_OK"};
  36. if (@$ok) {
  37. grep(s/^&//, @$ok);
  38. @exports{@$ok} = (1) x @$ok;
  39. }
  40. }
  41. if ($imports[0] =~ m#^[/!:]#){
  42. my $tagsref = \%{"${pkg}::EXPORT_TAGS"};
  43. my $tagdata;
  44. my %imports;
  45. my($remove, $spec, @names, @allexports);
  46. # negated first item implies starting with default set:
  47. unshift @imports, ':DEFAULT' if $imports[0] =~ m/^!/;
  48. foreach $spec (@imports){
  49. $remove = $spec =~ s/^!//;
  50. if ($spec =~ s/^://){
  51. if ($spec eq 'DEFAULT'){
  52. @names = @exports;
  53. }
  54. elsif ($tagdata = $tagsref->{$spec}) {
  55. @names = @$tagdata;
  56. }
  57. else {
  58. warn qq["$spec" is not defined in %${pkg}::EXPORT_TAGS];
  59. ++$oops;
  60. next;
  61. }
  62. }
  63. elsif ($spec =~ m:^/(.*)/$:){
  64. my $patn = $1;
  65. @allexports = keys %exports unless @allexports; # only do keys once
  66. @names = grep(/$patn/, @allexports); # not anchored by default
  67. }
  68. else {
  69. @names = ($spec); # is a normal symbol name
  70. }
  71. warn "Import ".($remove ? "del":"add").": @names "
  72. if $Verbose;
  73. if ($remove) {
  74. foreach $sym (@names) { delete $imports{$sym} }
  75. }
  76. else {
  77. @imports{@names} = (1) x @names;
  78. }
  79. }
  80. @imports = keys %imports;
  81. }
  82. foreach $sym (@imports) {
  83. if (!$exports{$sym}) {
  84. if ($sym =~ m/^\d/) {
  85. $pkg->require_version($sym);
  86. # If the version number was the only thing specified
  87. # then we should act as if nothing was specified:
  88. if (@imports == 1) {
  89. @imports = @exports;
  90. last;
  91. }
  92. # We need a way to emulate 'use Foo ()' but still
  93. # allow an easy version check: "use Foo 1.23, ''";
  94. if (@imports == 2 and !$imports[1]) {
  95. @imports = ();
  96. last;
  97. }
  98. } elsif ($sym !~ s/^&// || !$exports{$sym}) {
  99. require Carp;
  100. Carp::carp(qq["$sym" is not exported by the $pkg module]);
  101. $oops++;
  102. }
  103. }
  104. }
  105. if ($oops) {
  106. require Carp;
  107. Carp::croak("Can't continue after import errors");
  108. }
  109. }
  110. else {
  111. @imports = @exports;
  112. }
  113. *fail = *{"${pkg}::EXPORT_FAIL"};
  114. if (@fail) {
  115. if (!%fail) {
  116. # Build cache of symbols. Optimise the lookup by adding
  117. # barewords twice... both with and without a leading &.
  118. # (Technique could be applied to %exports cache at cost of memory)
  119. my @expanded = map { /^\w/ ? ($_, '&'.$_) : $_ } @fail;
  120. warn "${pkg}::EXPORT_FAIL cached: @expanded" if $Verbose;
  121. @fail{@expanded} = (1) x @expanded;
  122. }
  123. my @failed;
  124. foreach $sym (@imports) { push(@failed, $sym) if $fail{$sym} }
  125. if (@failed) {
  126. @failed = $pkg->export_fail(@failed);
  127. foreach $sym (@failed) {
  128. require Carp;
  129. Carp::carp(qq["$sym" is not implemented by the $pkg module ],
  130. "on this architecture");
  131. }
  132. if (@failed) {
  133. require Carp;
  134. Carp::croak("Can't continue after import errors");
  135. }
  136. }
  137. }
  138. warn "Importing into $callpkg from $pkg: ",
  139. join(", ",sort @imports) if $Verbose;
  140. foreach $sym (@imports) {
  141. # shortcut for the common case of no type character
  142. (*{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}, next)
  143. unless $sym =~ s/^(\W)//;
  144. $type = $1;
  145. *{"${callpkg}::$sym"} =
  146. $type eq '&' ? \&{"${pkg}::$sym"} :
  147. $type eq '$' ? \${"${pkg}::$sym"} :
  148. $type eq '@' ? \@{"${pkg}::$sym"} :
  149. $type eq '%' ? \%{"${pkg}::$sym"} :
  150. $type eq '*' ? *{"${pkg}::$sym"} :
  151. do { require Carp; Carp::croak("Can't export symbol: $type$sym") };
  152. }
  153. }
  154. sub export_to_level
  155. {
  156. my $pkg = shift;
  157. my ($level, $junk) = (shift, shift); # need to get rid of first arg
  158. # we know it already.
  159. my $callpkg = caller($level);
  160. $pkg->export($callpkg, @_);
  161. }
  162. sub import {
  163. my $pkg = shift;
  164. my $callpkg = caller($ExportLevel);
  165. export $pkg, $callpkg, @_;
  166. }
  167. # Utility functions
  168. sub _push_tags {
  169. my($pkg, $var, $syms) = @_;
  170. my $nontag;
  171. *export_tags = \%{"${pkg}::EXPORT_TAGS"};
  172. push(@{"${pkg}::$var"},
  173. map { $export_tags{$_} ? @{$export_tags{$_}} : scalar(++$nontag,$_) }
  174. (@$syms) ? @$syms : keys %export_tags);
  175. if ($nontag and $^W) {
  176. # This may change to a die one day
  177. require Carp;
  178. Carp::carp("Some names are not tags");
  179. }
  180. }
  181. sub export_tags { _push_tags((caller)[0], "EXPORT", \@_) }
  182. sub export_ok_tags { _push_tags((caller)[0], "EXPORT_OK", \@_) }
  183. # Default methods
  184. sub export_fail {
  185. my $self = shift;
  186. @_;
  187. }
  188. sub require_version {
  189. my($self, $wanted) = @_;
  190. my $pkg = ref $self || $self;
  191. my $version = ${"${pkg}::VERSION"};
  192. if (!$version or $version < $wanted) {
  193. $version ||= "(undef)";
  194. my $file = $INC{"$pkg.pm"};
  195. $file &&= " ($file)";
  196. require Carp;
  197. Carp::croak("$pkg $wanted required--this is only version $version$file")
  198. }
  199. $version;
  200. }
  201. 1;
  202. # A simple self test harness. Change 'require Carp' to 'use Carp ()' for testing.
  203. # package main; eval(join('',<DATA>)) or die $@ unless caller;
  204. __END__
  205. package Test;
  206. $INC{'Exporter.pm'} = 1;
  207. @ISA = qw(Exporter);
  208. @EXPORT = qw(A1 A2 A3 A4 A5);
  209. @EXPORT_OK = qw(B1 B2 B3 B4 B5);
  210. %EXPORT_TAGS = (T1=>[qw(A1 A2 B1 B2)], T2=>[qw(A1 A2 B3 B4)], T3=>[qw(X3)]);
  211. @EXPORT_FAIL = qw(B4);
  212. Exporter::export_ok_tags('T3', 'unknown_tag');
  213. sub export_fail {
  214. map { "Test::$_" } @_ # edit symbols just as an example
  215. }
  216. package main;
  217. $Exporter::Verbose = 1;
  218. #import Test;
  219. #import Test qw(X3); # export ok via export_ok_tags()
  220. #import Test qw(:T1 !A2 /5/ !/3/ B5);
  221. import Test qw(:T2 !B4);
  222. import Test qw(:T2); # should fail
  223. 1;
  224. =head1 NAME
  225. Exporter - Implements default import method for modules
  226. =head1 SYNOPSIS
  227. In module ModuleName.pm:
  228. package ModuleName;
  229. require Exporter;
  230. @ISA = qw(Exporter);
  231. @EXPORT = qw(...); # symbols to export by default
  232. @EXPORT_OK = qw(...); # symbols to export on request
  233. %EXPORT_TAGS = tag => [...]; # define names for sets of symbols
  234. In other files which wish to use ModuleName:
  235. use ModuleName; # import default symbols into my package
  236. use ModuleName qw(...); # import listed symbols into my package
  237. use ModuleName (); # do not import any symbols
  238. =head1 DESCRIPTION
  239. The Exporter module implements a default C<import> method which
  240. many modules choose to inherit rather than implement their own.
  241. Perl automatically calls the C<import> method when processing a
  242. C<use> statement for a module. Modules and C<use> are documented
  243. in L<perlfunc> and L<perlmod>. Understanding the concept of
  244. modules and how the C<use> statement operates is important to
  245. understanding the Exporter.
  246. =head2 Selecting What To Export
  247. Do B<not> export method names!
  248. Do B<not> export anything else by default without a good reason!
  249. Exports pollute the namespace of the module user. If you must export
  250. try to use @EXPORT_OK in preference to @EXPORT and avoid short or
  251. common symbol names to reduce the risk of name clashes.
  252. Generally anything not exported is still accessible from outside the
  253. module using the ModuleName::item_name (or $blessed_ref-E<gt>method)
  254. syntax. By convention you can use a leading underscore on names to
  255. informally indicate that they are 'internal' and not for public use.
  256. (It is actually possible to get private functions by saying:
  257. my $subref = sub { ... };
  258. &$subref;
  259. But there's no way to call that directly as a method, since a method
  260. must have a name in the symbol table.)
  261. As a general rule, if the module is trying to be object oriented
  262. then export nothing. If it's just a collection of functions then
  263. @EXPORT_OK anything but use @EXPORT with caution.
  264. Other module design guidelines can be found in L<perlmod>.
  265. =head2 Specialised Import Lists
  266. If the first entry in an import list begins with !, : or / then the
  267. list is treated as a series of specifications which either add to or
  268. delete from the list of names to import. They are processed left to
  269. right. Specifications are in the form:
  270. [!]name This name only
  271. [!]:DEFAULT All names in @EXPORT
  272. [!]:tag All names in $EXPORT_TAGS{tag} anonymous list
  273. [!]/pattern/ All names in @EXPORT and @EXPORT_OK which match
  274. A leading ! indicates that matching names should be deleted from the
  275. list of names to import. If the first specification is a deletion it
  276. is treated as though preceded by :DEFAULT. If you just want to import
  277. extra names in addition to the default set you will still need to
  278. include :DEFAULT explicitly.
  279. e.g., Module.pm defines:
  280. @EXPORT = qw(A1 A2 A3 A4 A5);
  281. @EXPORT_OK = qw(B1 B2 B3 B4 B5);
  282. %EXPORT_TAGS = (T1 => [qw(A1 A2 B1 B2)], T2 => [qw(A1 A2 B3 B4)]);
  283. Note that you cannot use tags in @EXPORT or @EXPORT_OK.
  284. Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK.
  285. An application using Module can say something like:
  286. use Module qw(:DEFAULT :T2 !B3 A3);
  287. Other examples include:
  288. use Socket qw(!/^[AP]F_/ !SOMAXCONN !SOL_SOCKET);
  289. use POSIX qw(:errno_h :termios_h !TCSADRAIN !/^EXIT/);
  290. Remember that most patterns (using //) will need to be anchored
  291. with a leading ^, e.g., C</^EXIT/> rather than C</EXIT/>.
  292. You can say C<BEGIN { $Exporter::Verbose=1 }> to see how the
  293. specifications are being processed and what is actually being imported
  294. into modules.
  295. =head2 Exporting without using Export's import method
  296. Exporter has a special method, 'export_to_level' which is used in situations
  297. where you can't directly call Export's import method. The export_to_level
  298. method looks like:
  299. MyPackage->export_to_level($where_to_export, @what_to_export);
  300. where $where_to_export is an integer telling how far up the calling stack
  301. to export your symbols, and @what_to_export is an array telling what
  302. symbols *to* export (usually this is @_).
  303. For example, suppose that you have a module, A, which already has an
  304. import function:
  305. package A;
  306. @ISA = qw(Exporter);
  307. @EXPORT_OK = qw ($b);
  308. sub import
  309. {
  310. $A::b = 1; # not a very useful import method
  311. }
  312. and you want to Export symbol $A::b back to the module that called
  313. package A. Since Exporter relies on the import method to work, via
  314. inheritance, as it stands Exporter::import() will never get called.
  315. Instead, say the following:
  316. package A;
  317. @ISA = qw(Exporter);
  318. @EXPORT_OK = qw ($b);
  319. sub import
  320. {
  321. $A::b = 1;
  322. A->export_to_level(1, @_);
  323. }
  324. This will export the symbols one level 'above' the current package - ie: to
  325. the program or module that used package A.
  326. Note: Be careful not to modify '@_' at all before you call export_to_level
  327. - or people using your package will get very unexplained results!
  328. =head2 Module Version Checking
  329. The Exporter module will convert an attempt to import a number from a
  330. module into a call to $module_name-E<gt>require_version($value). This can
  331. be used to validate that the version of the module being used is
  332. greater than or equal to the required version.
  333. The Exporter module supplies a default require_version method which
  334. checks the value of $VERSION in the exporting module.
  335. Since the default require_version method treats the $VERSION number as
  336. a simple numeric value it will regard version 1.10 as lower than
  337. 1.9. For this reason it is strongly recommended that you use numbers
  338. with at least two decimal places, e.g., 1.09.
  339. =head2 Managing Unknown Symbols
  340. In some situations you may want to prevent certain symbols from being
  341. exported. Typically this applies to extensions which have functions
  342. or constants that may not exist on some systems.
  343. The names of any symbols that cannot be exported should be listed
  344. in the C<@EXPORT_FAIL> array.
  345. If a module attempts to import any of these symbols the Exporter
  346. will give the module an opportunity to handle the situation before
  347. generating an error. The Exporter will call an export_fail method
  348. with a list of the failed symbols:
  349. @failed_symbols = $module_name->export_fail(@failed_symbols);
  350. If the export_fail method returns an empty list then no error is
  351. recorded and all the requested symbols are exported. If the returned
  352. list is not empty then an error is generated for each symbol and the
  353. export fails. The Exporter provides a default export_fail method which
  354. simply returns the list unchanged.
  355. Uses for the export_fail method include giving better error messages
  356. for some symbols and performing lazy architectural checks (put more
  357. symbols into @EXPORT_FAIL by default and then take them out if someone
  358. actually tries to use them and an expensive check shows that they are
  359. usable on that platform).
  360. =head2 Tag Handling Utility Functions
  361. Since the symbols listed within %EXPORT_TAGS must also appear in either
  362. @EXPORT or @EXPORT_OK, two utility functions are provided which allow
  363. you to easily add tagged sets of symbols to @EXPORT or @EXPORT_OK:
  364. %EXPORT_TAGS = (foo => [qw(aa bb cc)], bar => [qw(aa cc dd)]);
  365. Exporter::export_tags('foo'); # add aa, bb and cc to @EXPORT
  366. Exporter::export_ok_tags('bar'); # add aa, cc and dd to @EXPORT_OK
  367. Any names which are not tags are added to @EXPORT or @EXPORT_OK
  368. unchanged but will trigger a warning (with C<-w>) to avoid misspelt tags
  369. names being silently added to @EXPORT or @EXPORT_OK. Future versions
  370. may make this a fatal error.
  371. =cut