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

  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>SelfLoader - load functions only on demand</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> SelfLoader - load functions only on demand</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="#the __data__ token">The __DATA__ token</A></LI>
  28.         <LI><A HREF="#selfloader autoloading">SelfLoader autoloading</A></LI>
  29.         <LI><A HREF="#autoloading and package lexicals">Autoloading and package lexicals</A></LI>
  30.         <LI><A HREF="#selfloader and autoloader">SelfLoader and AutoLoader</A></LI>
  31.         <LI><A HREF="#__data__, __end__, and the foobar::data filehandle.">__DATA__, __END__, and the FOOBAR::DATA filehandle.</A></LI>
  32.         <LI><A HREF="#classes and inherited methods.">Classes and inherited methods.</A></LI>
  33.     </UL>
  34.  
  35.     <LI><A HREF="#multiple packages and fully qualified subroutine names">Multiple packages and fully qualified subroutine names</A></LI>
  36. </UL>
  37. <!-- INDEX END -->
  38.  
  39. <HR>
  40. <P>
  41. <H1><A NAME="name">NAME</A></H1>
  42. <P>SelfLoader - load functions only on demand</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. <PRE>
  54.     package FOOBAR;
  55.     use SelfLoader;</PRE>
  56. <PRE>
  57.     ... (initializing code)</PRE>
  58. <PRE>
  59.     __DATA__
  60.     sub {....</PRE>
  61. <P>
  62. <HR>
  63. <H1><A NAME="description">DESCRIPTION</A></H1>
  64. <P>This module tells its users that functions in the FOOBAR package are to be
  65. autoloaded from after the <CODE>__DATA__</CODE> token.  See also
  66. <A HREF="../lib/Pod/perlsub.html#autoloading">Autoloading in the perlsub manpage</A>.</P>
  67. <P>
  68. <H2><A NAME="the __data__ token">The __DATA__ token</A></H2>
  69. <P>The <CODE>__DATA__</CODE> token tells the perl compiler that the perl code
  70. for compilation is finished. Everything after the <CODE>__DATA__</CODE> token
  71. is available for reading via the filehandle FOOBAR::DATA,
  72. where FOOBAR is the name of the current package when the <CODE>__DATA__</CODE>
  73. token is reached. This works just the same as <CODE>__END__</CODE> does in
  74. package 'main', but for other modules data after <CODE>__END__</CODE> is not
  75. automatically retrievable, whereas data after <CODE>__DATA__</CODE> is.
  76. The <CODE>__DATA__</CODE> token is not recognized in versions of perl prior to
  77. 5.001m.</P>
  78. <P>Note that it is possible to have <CODE>__DATA__</CODE> tokens in the same package
  79. in multiple files, and that the last <CODE>__DATA__</CODE> token in a given
  80. package that is encountered by the compiler is the one accessible
  81. by the filehandle. This also applies to <CODE>__END__</CODE> and main, i.e. if
  82. the 'main' program has an <CODE>__END__</CODE>, but a module 'require'd (_not_ 'use'd)
  83. by that program has a 'package main;' declaration followed by an '<CODE>__DATA__</CODE>',
  84. then the <CODE>DATA</CODE> filehandle is set to access the data after the <CODE>__DATA__</CODE>
  85. in the module, _not_ the data after the <CODE>__END__</CODE> token in the 'main'
  86. program, since the compiler encounters the 'require'd file later.</P>
  87. <P>
  88. <H2><A NAME="selfloader autoloading">SelfLoader autoloading</A></H2>
  89. <P>The <STRONG>SelfLoader</STRONG> works by the user placing the <CODE>__DATA__</CODE>
  90. token <EM>after</EM> perl code which needs to be compiled and
  91. run at 'require' time, but <EM>before</EM> subroutine declarations
  92. that can be loaded in later - usually because they may never
  93. be called.</P>
  94. <P>The <STRONG>SelfLoader</STRONG> will read from the FOOBAR::DATA filehandle to
  95. load in the data after <CODE>__DATA__</CODE>, and load in any subroutine
  96. when it is called. The costs are the one-time parsing of the
  97. data after <CODE>__DATA__</CODE>, and a load delay for the _first_
  98. call of any autoloaded function. The benefits (hopefully)
  99. are a speeded up compilation phase, with no need to load
  100. functions which are never used.</P>
  101. <P>The <STRONG>SelfLoader</STRONG> will stop reading from <CODE>__DATA__</CODE> if
  102. it encounters the <CODE>__END__</CODE> token - just as you would expect.
  103. If the <CODE>__END__</CODE> token is present, and is followed by the
  104. token DATA, then the <STRONG>SelfLoader</STRONG> leaves the FOOBAR::DATA
  105. filehandle open on the line after that token.</P>
  106. <P>The <STRONG>SelfLoader</STRONG> exports the <CODE>AUTOLOAD</CODE> subroutine to the
  107. package using the <STRONG>SelfLoader</STRONG>, and this loads the called
  108. subroutine when it is first called.</P>
  109. <P>There is no advantage to putting subroutines which will _always_
  110. be called after the <CODE>__DATA__</CODE> token.</P>
  111. <P>
  112. <H2><A NAME="autoloading and package lexicals">Autoloading and package lexicals</A></H2>
  113. <P>A 'my $pack_lexical' statement makes the variable $pack_lexical
  114. local _only_ to the file up to the <CODE>__DATA__</CODE> token. Subroutines
  115. declared elsewhere _cannot_ see these types of variables,
  116. just as if you declared subroutines in the package but in another
  117. file, they cannot see these variables.</P>
  118. <P>So specifically, autoloaded functions cannot see package
  119. lexicals (this applies to both the <STRONG>SelfLoader</STRONG> and the Autoloader).
  120. The <CODE>vars</CODE> pragma provides an alternative to defining package-level
  121. globals that will be visible to autoloaded routines. See the documentation
  122. on <STRONG>vars</STRONG> in the pragma section of <A HREF="../lib/Pod/perlmod.html">the perlmod manpage</A>.</P>
  123. <P>
  124. <H2><A NAME="selfloader and autoloader">SelfLoader and AutoLoader</A></H2>
  125. <P>The <STRONG>SelfLoader</STRONG> can replace the AutoLoader - just change 'use AutoLoader'
  126. to 'use SelfLoader' (though note that the <STRONG>SelfLoader</STRONG> exports
  127. the AUTOLOAD function - but if you have your own AUTOLOAD and
  128. are using the AutoLoader too, you probably know what you're doing),
  129. and the <CODE>__END__</CODE> token to <CODE>__DATA__</CODE>. You will need perl version 5.001m
  130. or later to use this (version 5.001 with all patches up to patch m).</P>
  131. <P>There is no need to inherit from the <STRONG>SelfLoader</STRONG>.</P>
  132. <P>The <STRONG>SelfLoader</STRONG> works similarly to the AutoLoader, but picks up the
  133. subs from after the <CODE>__DATA__</CODE> instead of in the 'lib/auto' directory.
  134. There is a maintenance gain in not needing to run AutoSplit on the module
  135. at installation, and a runtime gain in not needing to keep opening and
  136. closing files to load subs. There is a runtime loss in needing
  137. to parse the code after the <CODE>__DATA__</CODE>. Details of the <STRONG>AutoLoader</STRONG> and
  138. another view of these distinctions can be found in that module's
  139. documentation.</P>
  140. <P>
  141. <H2><A NAME="__data__, __end__, and the foobar::data filehandle.">__DATA__, __END__, and the FOOBAR::DATA filehandle.</A></H2>
  142. <P>This section is only relevant if you want to use
  143. the <CODE>FOOBAR::DATA</CODE> together with the <STRONG>SelfLoader</STRONG>.</P>
  144. <P>Data after the <CODE>__DATA__</CODE> token in a module is read using the
  145. FOOBAR::DATA filehandle. <CODE>__END__</CODE> can still be used to denote the end
  146. of the <CODE>__DATA__</CODE> section if followed by the token DATA - this is supported
  147. by the <STRONG>SelfLoader</STRONG>. The <CODE>FOOBAR::DATA</CODE> filehandle is left open if an
  148. <CODE>__END__</CODE> followed by a DATA is found, with the filehandle positioned at
  149. the start of the line after the <CODE>__END__</CODE> token. If no <CODE>__END__</CODE> token is
  150. present, or an <CODE>__END__</CODE> token with no DATA token on the same line, then
  151. the filehandle is closed.</P>
  152. <P>The <STRONG>SelfLoader</STRONG> reads from wherever the current
  153. position of the <CODE>FOOBAR::DATA</CODE> filehandle is, until the
  154. EOF or <CODE>__END__</CODE>. This means that if you want to use
  155. that filehandle (and ONLY if you want to), you should either</P>
  156. <P>1. Put all your subroutine declarations immediately after
  157. the <CODE>__DATA__</CODE> token and put your own data after those
  158. declarations, using the <CODE>__END__</CODE> token to mark the end
  159. of subroutine declarations. You must also ensure that the <STRONG>SelfLoader</STRONG>
  160. reads first by  calling 'SelfLoader->load_stubs();', or by using a
  161. function which is selfloaded;</P>
  162. <P>or</P>
  163. <P>2. You should read the <CODE>FOOBAR::DATA</CODE> filehandle first, leaving
  164. the handle open and positioned at the first line of subroutine
  165. declarations.</P>
  166. <P>You could conceivably do both.</P>
  167. <P>
  168. <H2><A NAME="classes and inherited methods.">Classes and inherited methods.</A></H2>
  169. <P>For modules which are not classes, this section is not relevant.
  170. This section is only relevant if you have methods which could
  171. be inherited.</P>
  172. <P>A subroutine stub (or forward declaration) looks like</P>
  173. <PRE>
  174.   sub stub;</PRE>
  175. <P>i.e. it is a subroutine declaration without the body of the
  176. subroutine. For modules which are not classes, there is no real
  177. need for stubs as far as autoloading is concerned.</P>
  178. <P>For modules which ARE classes, and need to handle inherited methods,
  179. stubs are needed to ensure that the method inheritance mechanism works
  180. properly. You can load the stubs into the module at 'require' time, by
  181. adding the statement 'SelfLoader->load_stubs();' to the module to do
  182. this.</P>
  183. <P>The alternative is to put the stubs in before the <CODE>__DATA__</CODE> token BEFORE
  184. releasing the module, and for this purpose the <CODE>Devel::SelfStubber</CODE>
  185. module is available.  However this does require the extra step of ensuring
  186. that the stubs are in the module. If this is done I strongly recommend
  187. that this is done BEFORE releasing the module - it should NOT be done
  188. at install time in general.</P>
  189. <P>
  190. <HR>
  191. <H1><A NAME="multiple packages and fully qualified subroutine names">Multiple packages and fully qualified subroutine names</A></H1>
  192. <P>Subroutines in multiple packages within the same file are supported - but you
  193. should note that this requires exporting the <CODE>SelfLoader::AUTOLOAD</CODE> to
  194. every package which requires it. This is done automatically by the
  195. <STRONG>SelfLoader</STRONG> when it first loads the subs into the cache, but you should
  196. really specify it in the initialization before the <CODE>__DATA__</CODE> by putting
  197. a 'use SelfLoader' statement in each package.</P>
  198. <P>Fully qualified subroutine names are also supported. For example,</P>
  199. <PRE>
  200.    __DATA__
  201.    sub foo::bar {23}
  202.    package baz;
  203.    sub dob {32}</PRE>
  204. <P>will all be loaded correctly by the <STRONG>SelfLoader</STRONG>, and the <STRONG>SelfLoader</STRONG>
  205. will ensure that the packages 'foo' and 'baz' correctly have the
  206. <STRONG>SelfLoader</STRONG> <CODE>AUTOLOAD</CODE> method when the data after <CODE>__DATA__</CODE> is first
  207. parsed.</P>
  208. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  209. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  210. <STRONG><P CLASS=block> SelfLoader - load functions only on demand</P></STRONG>
  211. </TD></TR>
  212. </TABLE>
  213.  
  214. </BODY>
  215.  
  216. </HTML>
  217.