Leaked source code of windows server 2003
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.

313 lines
10 KiB

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