⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<p>You can say <tt class="literal">BEGIN { $Exporter::Verbose=1 }</tt> to seehow the specifications are being processed and what is actually beingimported into your package.</p><p>The <tt class="literal">Exporter</tt> is itself a Perl module, and if you'recurious you can see the typeglob trickery it uses to export symbolsfrom one package into another.  Inside the <tt class="literal">Export</tt>module, the key function is named <tt class="literal">import</tt>, whichperforms the necessary aliasing to make a symbol in one package appearto be in another.  In fact, a <tt class="literal">use Bestiary</tt><em class="replaceable">LIST</em> statement is exactly equivalent to:<blockquote><pre class="programlisting">BEGIN {    require Bestiary;    import Bestiary <em class="replaceable">LIST</em>;}</pre></blockquote>This means that your modules don't have to use the<tt class="literal">Exporter</tt>.  A module can do anything it jolly wellpleases when it's used, since <tt class="literal">use</tt> just calls theordinary <tt class="literal">import</tt> method for the module, and you candefine that method to do anything you like.</p><h3 class="sect3">11.2.1.1. Exporting without using Exporter's import method</h3><p><a name="INDEX-2314"></a><a name="INDEX-2315"></a><a name="INDEX-2316"></a>The <tt class="literal">Exporter</tt> defines a method called<tt class="literal">export_to_level</tt>, used for situations where (forsome reason) you can't directly call <tt class="literal">Exporter</tt>'s<tt class="literal">import</tt> method. The<tt class="literal">export_to_level</tt> method is invoked like this:<blockquote><pre class="programlisting"><em class="replaceable">MODULE</em>-&gt;export_to_level($where_to_export, @what_to_export);</pre></blockquote>where <tt class="literal">$where_to_export</tt> is an integer indicating howfar up the calling stack to export your symbols, and<tt class="literal">@what_to_export</tt> is an array listing the symbols toexport (usually <tt class="literal">@_</tt>).</p><p>For example, suppose our <tt class="literal">Bestiary</tt> had an<tt class="literal">import</tt> function of its own:<blockquote><pre class="programlisting">package Bestiary;@ISA = qw(Exporter);@EXPORT_OK = qw ($zoo);sub import {    $Bestiary::zoo = "menagerie";}</pre></blockquote>The presence of this <tt class="literal">import</tt> function prevents<tt class="literal">Exporter</tt>'s <tt class="literal">import</tt> function frombeing inherited.  If you want <tt class="literal">Bestiary</tt>'s<tt class="literal">import</tt> function to behave just like<tt class="literal">Exporter</tt>'s <tt class="literal">import</tt> function onceit sets <tt class="literal">$Bestiary::zoo</tt>, you'd define it as follows:<blockquote><pre class="programlisting">sub import {    $Bestiary::zoo = "menagerie";    Bestiary-&gt;export_to_level(1, @_);}</pre></blockquote>This exports symbols to the package one level "above" the currentpackage.  That is, to whatever program or module is using the<tt class="literal">Bestiary</tt>.</p><h3 class="sect3">11.2.1.2. Version checking</h3><p><a name="INDEX-2317"></a><a name="INDEX-2318"></a>If your module defines a <tt class="literal">$VERSION</tt> variable, aprogram using your module can ensure that the module is sufficientlyrecent.  For example:<blockquote><pre class="programlisting">use Bestiary 3.14;   # The Bestiary must be version 3.14 or lateruse Bestiary v1.0.4; # The Bestiary must be version 1.0.4 or later</pre></blockquote>These are converted into calls to<tt class="literal">Bestiary-&gt;require_version</tt>, which your modulethen inherits.</p><h3 class="sect3">11.2.1.3. Managing unknown symbols</h3><p><a name="INDEX-2319"></a><a name="INDEX-2320"></a>In some situations, you may want to <em class="emphasis">prevent</em>certain symbols from being exported. Typically, this applies tomodules that have functions or constants that might not make sense onsome systems.  You can prevent the <tt class="literal">Exporter</tt> fromexporting those symbols by placing them in the<tt class="literal">@EXPORT_FAIL</tt> array.</p><p><a name="INDEX-2321"></a><a name="INDEX-2322"></a>If a program attempts to import any of these symbols, the<tt class="literal">Exporter</tt> gives the module an opportunity to handlethe situation before generating an error.  It does this by calling an<tt class="literal">export_fail</tt> method with a list of the failedsymbols, which you might define as follows (assuming your module usesthe <tt class="literal">Carp</tt> module):<blockquote><pre class="programlisting">sub export_fail {    my $class = shift;    carp "Sorry, these symbols are unavailable: @_";    return @_;}</pre></blockquote>The <tt class="literal">Exporter</tt> provides a default<tt class="literal">export_fail</tt> method, which simply returns the listunchanged and makes the <tt class="literal">use</tt> fail with an exceptionraised for each symbol.  If <tt class="literal">export_fail</tt> returns anempty list, no error is recorded and all the requested symbols areexported.</p><h3 class="sect3">11.2.1.4. Tag-handling utility functions</h3><p><a name="INDEX-2323"></a>Since the symbols listed within <tt class="literal">%EXPORT_TAGS</tt> mustalso appear in either <tt class="literal">@EXPORT</tt> or<tt class="literal">@EXPORT_OK</tt>, the <tt class="literal">Exporter</tt>provides two functions to let you add those tagged sets of symbols:<blockquote><pre class="programlisting">%EXPORT_TAGS = (foo =&gt; [qw(aa bb cc)], bar =&gt; [qw(aa cc dd)]);Exporter::export_tags('foo');     # add aa, bb and cc to @EXPORTExporter::export_ok_tags('bar');  # add aa, cc and dd to @EXPORT_OK</pre></blockquote>Specifying names that are not tags is erroneous.</p><a name="INDEX-2324"></a><a name="INDEX-2325"></a><a name="INDEX-2326"></a><a name="INDEX-2327"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch11_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch11_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">11.1. Using Modules</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">11.3. Overriding Built-in Functions</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -