📄 ch07_02.htm
字号:
<html><head><title>Modules (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch07_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch07_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">7.2. Modules</h2><p><a name="INDEX-1248" />A<em class="emphasis">module</em> is a package defined in a file with aname that is the same as the package's. Perl locatesmodules by searching the <tt class="literal">@INC</tt> array, whichcontains a list of library directories. Perl's useof <tt class="literal">@INC</tt> is roughly comparable to the Unixshell's use of the PATH environment variable tolocate executable programs. <tt class="literal">@INC</tt> is defined whenPerl is built and can be supplemented with the<em class="emphasis">-I</em> command-line option to Perl or with<tt class="literal">use lib</tt> within a program.</p><p>When you refer to <tt class="literal">ModuleName</tt> in your program, Perlsearches in the directories listed in <tt class="literal">@INC</tt> for themodule file <em class="emphasis">ModuleName.pm</em> and uses the first oneit finds. When you refer to a module embedded in another package,such as <tt class="literal">ParentPackage::ModuleName</tt>, Perl looks fora <em class="emphasis">ParentPackage/</em> subdirectory in the<tt class="literal">@INC</tt> path, and for a<em class="emphasis">ModuleName.pm</em> file in that subdirectory.</p><p>Every Perl installation includes a central <em class="emphasis">lib</em>directory. The actual pathname of this directory varies from systemto system, but it's commonly<em class="emphasis">/usr/lib/perl</em> or<em class="emphasis">/usr/local/lib/perl</em>. Looking at the central<em class="emphasis">lib</em> directory for your Perl distribution,you'll see something like this:</p><blockquote><pre class="code">% ls -aF /usr/local/lib/perl./ I18N/ bigfloat.pl less.pm../ IO/ bigint.pl lib.pmAnyDBM_File.pm IPC/ bigrat.pl locale.pmAutoLoader.pm Math/ blib.pm look.plAutoSplit.pm Net/ cacheout.pl man/Benchmark.pm Pod/ chat2.pl newgetopt.plBundle/ Search/ complete.pl open2.plCGI/ SelectSaver.pm constant.pm open3.plCGI.pm SelfLoader.pm ctime.pl perl5db.plCPAN/ Shell.pm diagnostics.pm pod/CPAN.pm Symbol.pm dotsh.pl pwd.plCarp.pm Sys/ dumpvar.pl shellwords.pl ...</pre></blockquote><p>When you request the <tt class="literal">AnyDBM_File</tt> module, it uses<em class="emphasis">AnyDBM_File.pm</em>. When you request the<tt class="literal">Math::Complex</tt> module, it looks for<em class="emphasis">Math/Complex.pm</em>.</p><p><a name="INDEX-1249" /><a name="INDEX-1250" />A module can be included in yourprogram with <tt class="literal">use</tt> or <tt class="literal">require</tt>.Both <tt class="literal">use</tt> and <tt class="literal">require</tt> read in amodule file for use with your program.</p><blockquote><pre class="code">require Module;</pre></blockquote><p>or: </p><blockquote><pre class="code">use Module;</pre></blockquote><p><tt class="literal">use</tt> can also take a list of strings namingentities that you want to import from the module. The list only hasto include entities that are not automatically exported by themodule. You don't have to provide this list if themodule automatically exports all the entities you need.</p><blockquote><pre class="code">use Module qw(<em class="replaceable"><tt>const1 const2 func1 func2 func3</tt></em>);</pre></blockquote><p>The difference between <tt class="literal">use</tt> and<tt class="literal">require</tt> is that <tt class="literal">use</tt> pulls inthe module at compile time. This means that functions such as<em class="replaceable"><tt>func1</em> or <em class="replaceable">func2</tt></em>can be used as predeclared list operators throughout the file. The<tt class="literal">require</tt> call does not necessarily load the moduleduring compilation, so you must explicitly qualify its routines withthe package name<a name="INDEX-1251" />. </p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch07_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch07_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">7. Packages, Modules, and Objects</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">7.3. Object-Oriented Perl</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -