home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _7cf85da75cc7719e5fd31932b1fc46d9 < prev    next >
Text File  |  2000-03-23  |  12KB  |  240 lines

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