📄 ch07_01.htm
字号:
<html><head><title>Packages, Modules, and Objects (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="part3.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_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 7. Packages, Modules, and Objects</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4> <p> <a href="#perlnut2-CHP-7-SECT-1">Namespaces and Packages</a><br /><a href="ch07_02.htm">Modules</a><br /><a href="ch07_03.htm">Object-Oriented Perl</a><br /><a href="ch07_04.htm">Object Syntax</a><br /></p></div><p>Over the years, Perl has evolved from a utilitarian scripting toolinto a sophisticated object-oriented programming language. Manypeople continue to use Perl just for simple scripts, and Perl willcontinue to make simple tasks easy. However, Perl can also makedifficult tasks possible by writing reusable code and usingobject-oriented programming techniques.</p><p><a name="INDEX-1236" /></a>This chapterexplains what Perl modules are and how to use them in your programs.Modules are written to accomplish tasks that eitheraren't implemented by Perl'sbuilt-in functions, or that could be done better. We say modules are"reusable" because anyone who needsto accomplish the same task can use that module instead of writingthe code from scratch. As you write more and more Perl code,you'll undoubtedly find yourself using many of themodules other Perl programmers have provided. You may also findyourself writing modules and making them available for others to use.</p><p>The remainder of this book describes a significant portion of thefunctionality that's present in publicly availablePerl modules. You'll find that a number of<em class="emphasis">standard</em> or <em class="emphasis">core</em> modulesare distributed with Perl; many of these modules are discussed in<a href="ch08_01.htm">Chapter 8, "Standard Modules"</a>. Scores of other modules are availableon CPAN, and virtually any task you'd like toaccomplish in Perl is implemented in a module found there. Forunbundled modules, you'll need to install the moduleon your system and integrate it into your program with the<tt class="literal">use</tt> function.</p><p><a name="INDEX-1237" /></a>The<tt class="literal">use</tt> function is often the key to working withmodules. For example, to bring the functionality of the popular CGImodule into your program, you need to install the<em class="emphasis">CGI.pm</em> module (the <em class="emphasis">.pm</em>stands for Perl module) and put this line near the top of yourprogram:</p><blockquote><pre class="code">use CGI;</pre></blockquote><p>Now your program can use the many functions and variables madeavailable by the CGI module.</p><p>Packages (from which modules are built) are also a mechanism by whichPerl's object-oriented features are implemented. Butobject-oriented programming isn't for everyone, andthere's nothing in packages that forces theprogrammer to work with the object-oriented paradigm.</p><div class="sect1"><a name="perlnut2-CHP-7-SECT-1" /></a><h2 class="sect1">7.1. Namespaces and Packages</h2><p><a name="INDEX-1238" /></a><a name="INDEX-1239" /></a>Anamespace does what it says: it stores names (or identifiers),including names of variables, subroutines, filehandles, and formats.Each namespace has its own <em class="emphasis">symbol table</em>, whichis basically a hash with a key for each identifier.</p><p><a name="INDEX-1240" /></a>The defaultnamespace for programs is <tt class="literal">main</tt>, but you can defineother namespaces and variables and use them in your program.Variables in different namespaces can even have the same name, butthey are completely distinct from one another.</p><p><a name="INDEX-1241" /></a>InPerl, a namespace is held in a <em class="emphasis">package</em>. Byconvention, package names start with a capital letter, and you shouldfollow that convention when you create your own packages.</p><p>Each <a name="INDEX-1242" /></a><a name="INDEX-1243" /></a>packagestarts with a <tt class="literal">package</tt> declaration. The<tt class="literal">package</tt> call takes one argument: the name of thepackage. Within the scope of a package declaration, all regularidentifiers are created within that package (except for<tt class="literal">my</tt> variables).</p><p><a name="INDEX-1244" /></a><a name="INDEX-1245" /></a>From inside one package, you can referto variables from another package by"qualifying" them with the packagename. To do this, place the name of the package followed by twocolons (<tt class="literal">::</tt>) before theidentifier's name, e.g.,<tt class="literal">$Package::varname</tt>.</p><p>If the package name is null, the <tt class="literal">main</tt> package isassumed. For example, <tt class="literal">$var</tt> and<tt class="literal">$::var</tt> are the same as<tt class="literal">$main::var</tt>.</p><p>Packages may be nested inside other packages. However, the packagename must still be fully qualified. For example, if the package<tt class="literal">Province</tt> is declared inside the package<tt class="literal">Nation</tt>, a variable in the Province package iscalled as <tt class="literal">$Nation::Province::var</tt>. You cannot use a"relative" package name such as<tt class="literal">$Province::var</tt> within the<tt class="literal">Nation</tt> package for the same thing.</p><p>The default <tt class="literal">main</tt> namespace contains all otherpackages within it<a name="INDEX-1246" /></a><a name="INDEX-1247" /></a>. </p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="part3.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_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">III. Modules</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.2. Modules</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 + -