📄 ch32_15.htm
字号:
<html><head><title>Exporter (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Exporter"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch32_14.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch32_01.htm">Chapter 32: Standard Modules</a></td><td align="right" valign="top" width="172"><a href="ch32_16.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">32.15. Exporter</h2><p>Inside your <em class="emphasis">MyModule.pm</em> file:<blockquote><pre class="programlisting">package MyModule;use strict;use Exporter;our $VERSION = 1.00; # Or higher...our @ISA = qw(Exporter);our @EXPORT = qw(f1 %h); # Symbols imported by default.our @EXPORT_OK = qw(f2 f3); # Symbols imported only by request.our %EXPORT_TAGS = ( # Mappings for :shortcuts. a => [qw(f1 f2 f3)], b => [qw(f2 %h)],);# Your code here.1;</pre></blockquote>From a program or another module that makes use of your module:<blockquote><pre class="programlisting">use MyModule; # Import everything in @EXPORT.use MyModule (); # Load module, no imports at all.use MyModule "f1", "f2", "%h"; # Two subs and a variable.use MyModule qw(:DEFAULT f3); # All in @EXPORT + one sub.use MyModule "f4"; # Fatal because f4 not exported.</pre></blockquote>Whenever anyone invokes a <tt class="literal">use</tt> declaration to load your module,it calls the <tt class="literal">import</tt> method from your module to fetch any symbolsit needs into the package of the invoker. Your module (the one doingthe exporting) can define the <tt class="literal">import</tt> method any way it pleases, butthe standard way is to inherit the method from the<tt class="literal">Exporter</tt> class module. That is what the code above arranges.</p><p>The <tt class="literal">Exporter</tt> module serves as a base class for modules that wish toestablish their own exports. Oddly, object-oriented modules typicallydon't use <tt class="literal">Exporter</tt>, since they don't normally export anything(method calls don't need to be exported). However, the <tt class="literal">Exporter</tt>module itself is accessed in an OO fashion because of the <tt class="literal">@ISA</tt> arrayyou installed, as in our example. When another program or module<tt class="literal">use</tt>s your module, the <tt class="literal">import</tt> method is invoked as a class methodin your module: <tt class="literal">MyModule->import(</tt><em class="replaceable">LIST</em><tt class="literal">)</tt>. However, since youdidn't define an <tt class="literal">import</tt> method in your module, you'll automaticallymake use of the <tt class="literal">Exporter::import</tt> method through inheritance.</p><p>The module's <tt class="literal">@EXPORT</tt> array contains a list of symbols (functionsand even variables) that the calling code automatically importswith an unadorned <tt class="literal">use</tt> statement. The <tt class="literal">@EXPORT_OK</tt> array holdssymbols that can be imported if specifically requested by name.The <tt class="literal">$VERSION</tt> number is consulted if the <tt class="literal">use</tt> statement requeststhat a particular version (or newer) of the module. Many, many other featuresare available. See <a href="ch11_01.htm">Chapter 11, "Modules"</a>, as well as theonline manpage for the <tt class="literal">Exporter</tt> module.</p><!-- 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="ch32_14.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="ch32_16.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.14. Errno</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">32.16. Fatal</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 © 2001</a> O'Reilly & 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 + -