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

📄 ch11_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Modules (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 &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Modules"><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="ch10_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part2.htm">Part 2: The Gory Details</a></td><td align="right" valign="top" width="172"><a href="ch11_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h1 class="chapter">Chapter 11.  Modules</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch11_01.htm">Using Modules</a><br><a href="ch11_02.htm">Creating Modules</a><br><a href="ch11_03.htm">Overriding Built-in Functions</a><br></p></div><p><a name="INDEX-2268"></a><a name="INDEX-2269"></a> The module is thefundamental unit of code reuse in Perl.  Under the hood, it's just apackage defined in a file of the same name (with<em class="emphasis">.pm</em> on the end).  In this chapter, we'll explorehow you can use other people's modules and create your own.</p><p>Perl comes bundled with a large number of modules, which you can findin the <em class="emphasis">lib</em> directory of your Perl distribution.Many of those modules are described in <a href="ch32_01.htm">Chapter 32, "Standard Modules"</a>, and <a href="ch31_01.htm">Chapter 31, "Pragmatic Modules"</a>.  All the standard modulesalso have extensive online documentation, which (horrors) may be moreup-to-date than this book.  Try the <em class="emphasis">perldoc</em>command if your <em class="emphasis">man</em> command doesn't work.</p><p><a name="INDEX-2270"></a><a name="INDEX-2271"></a><a name="INDEX-2272"></a><a name="INDEX-2273"></a>The Comprehensive Perl Archive Network (CPAN) contains a worldwiderepository of modules contributed by the Perl community, and isdiscussed in <a href="ch22_01.htm">Chapter 22, "CPAN"</a>.  See also <a href="http://www.cpan.org">http://www.cpan.org</a>.</p><h2 class="sect1">11.1. Using Modules</h2><p><a name="INDEX-2274"></a><a name="INDEX-2275"></a><a name="INDEX-2276"></a>Modules come in two flavors: traditional and object-oriented.Traditional modules define subroutines and variables for the caller toimport and use.  Object-oriented modules function as class definitionsand are accessed through method calls, described in <a href="ch12_01.htm">Chapter 12, "Objects"</a>.  Some modules do both.</p><p>Perl modules are typically included in your program by saying:<blockquote><pre class="programlisting">use <em class="replaceable">MODULE</em> <em class="replaceable">LIST</em>;</pre></blockquote>or just:<blockquote><pre class="programlisting">use <em class="replaceable">MODULE</em>;</pre></blockquote><a name="INDEX-2277"></a></p><p><em class="replaceable">MODULE</em> must be an identifier naming themodule's package and file.  (The syntax descriptions here are meant tobe suggestive; the full syntax of the <tt class="literal">use</tt> statementis given in <a href="ch29_01.htm">Chapter 29, "Functions"</a>.)</p><p><a name="INDEX-2278"></a>The <tt class="literal">use</tt> statement does a preload of<em class="replaceable">MODULE</em> at compile time and then an importof the symbols you've requested so that they'll be available for therest of the compilation.  If you do not supply a<em class="replaceable">LIST</em> of symbols that you want, the symbolsnamed in the module's internal <tt class="literal">@EXPORT</tt> array areused--assuming you're using the <tt class="literal">Exporter</tt> module,described in "Module Privacy and the Exporter" later in this chapter.(If you do supply a <em class="replaceable">LIST</em>, all your symbolsmust be mentioned in the module's <tt class="literal">@EXPORT</tt> or<tt class="literal">@EXPORT_OK</tt> arrays, or an error will result.)</p><p>Since modules use the <tt class="literal">Exporter</tt> to import symbolsinto the current package, you can use symbols from the module withoutproviding a package qualifier:<blockquote><pre class="programlisting">use Fred;       # If Fred.pm has @EXPORT = qw(flintstone)flintstone();   # ...this calls Fred::flintstone().</pre></blockquote></p><p><a name="INDEX-2279"></a><a name="INDEX-2280"></a><a name="INDEX-2281"></a><a name="INDEX-2282"></a><a name="INDEX-2283"></a><a name="INDEX-2284"></a>All Perl module files have the extension <em class="emphasis">.pm</em>.Both <tt class="literal">use</tt> and <tt class="literal">require</tt> assume this(as well as the quotes) so that you don't have to spell out<tt class="literal">"</tt><em class="replaceable">MODULE</em><tt class="literal">.pm"</tt>.Using the bare identifier helps to differentiate new modules from<em class="emphasis">.pl</em> and <em class="emphasis">.ph</em> libraries usedin old versions of Perl.  It also introduces<em class="replaceable">MODULE</em> as an official module name, whichhelps the parser in certain ambiguous situations.  Any double colonsin the module name are translated into your system's directoryseparator, so if your module is named<tt class="literal">Red::Blue::Green</tt>, Perl might look for it as<em class="emphasis">Red/Blue/Green.pm</em>.</p><p><a name="INDEX-2285"></a><a name="INDEX-2286"></a><a name="INDEX-2287"></a><a name="INDEX-2288"></a>Perl will search for modules in each of the directories listed in the<tt class="literal">@INC</tt> array.  Since <tt class="literal">use</tt> loadsmodules at compile time, any modifications to <tt class="literal">@INC</tt>need to occur at compile time as well.  You can do this with the<tt class="literal">lib</tt> pragma described in <a href="ch31_01.htm">Chapter 31, "Pragmatic Modules"</a> or with a<tt class="literal">BEGIN</tt> block.  Once a module is included, akey/value pair will be added to the <tt class="literal">%INC</tt> hash.  Thekey will be the module filename (<tt class="literal">Red/Blue/Green.pm</tt>in our example) and the value will be the full pathname, which mightbe something like<tt class="literal">C:/perl/site/lib/Red/Blue/Green.pm</tt> for a properlyinstalled module on a Windows system.<a name="INDEX-2289"></a></p><p>Module names should be capitalized unless they're functioning aspragmas.  Pragmas are in effect compiler directives (hints for thecompiler), so we reserve the lowercase pragma names for future use.<a name="INDEX-2290"></a><a name="INDEX-2291"></a></p><p><a name="INDEX-2292"></a><a name="INDEX-2293"></a>When you <tt class="literal">use</tt> a module, any code inside the moduleis executed, just as it would be for an ordinary<tt class="literal">require</tt>.  If you really don't care whether themodule is pulled in at compile time or run time, you can just say:<blockquote><pre class="programlisting">require <em class="replaceable">MODULE</em>;</pre></blockquote>In general, however, <tt class="literal">use</tt> is preferred over <tt class="literal">require</tt> because itlooks for modules during compilation, so you learn about anymistakes sooner.</p><p><a name="INDEX-2294"></a>These two statements do almost the same thing:<blockquote><pre class="programlisting">require <em class="replaceable">MODULE</em>;require "<em class="replaceable">MODULE</em>.pm";</pre></blockquote>They differ in two ways, however.  In the first statement,<tt class="literal">require</tt> translates any double colons in the modulename into your system's directory separator, just as<tt class="literal">use</tt> does.  The second case does no translation,forcing you to specify the pathname of your module literally, which isless portable.  The other difference is that the first<tt class="literal">require</tt> tells the compiler that the expressionswith indirect object notation involving"<em class="replaceable">MODULE</em>" (such as <tt class="literal">$ob =purge</tt><em class="replaceable">MODULE</em>) are method calls,not function calls.  (Yes, this really can make a difference, ifthere's a conflicting definition of <tt class="literal">purge</tt> in yourown module.)</p><p><a name="INDEX-2295"></a>Because the <tt class="literal">use</tt> declaration and the related<tt class="literal">no</tt> declaration imply a <tt class="literal">BEGIN</tt>block, the compiler loads the module (and runs any executableinitialization code in it) as soon as it encounters that declaration,<em class="emphasis">before</em> it compiles the rest of the file.  This ishow pragmas can change the compiler's behavior, and also how modulesare able to declare subroutines that are then visible as listoperators for the remainder of compilation.  This will not work if youuse <tt class="literal">require</tt> instead of <tt class="literal">use</tt>.Just about the only reason to use <tt class="literal">require</tt> is if youhave two modules that each need a function from the other.  (And we'renot sure that's a good reason.)</p><p><a name="INDEX-2296"></a><a name="INDEX-2297"></a>Perl modules always load a <em class="emphasis">.pm</em> file, but thatfile may in turn load associated files, such as dynamically linked Cor C++ libraries or autoloaded Perl subroutine definitions.  If so,the additional shenanigans will be entirely transparent to the moduleuser.  It is the responsibility of the <em class="emphasis">.pm</em> fileto load (or arrange to autoload) any additional functionality.  The<tt class="literal">POSIX</tt> module happens to perform both dynamicloading and autoloading, but the user can say just:<blockquote><pre class="programlisting">use POSIX;</pre></blockquote>to get all the exported functions and variables.</p><a name="INDEX-2335"></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="ch10_02.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_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">10.2. Autoloading</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.2. Creating Modules</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 + -