ch01_02.htm
来自「编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,」· HTM 代码 · 共 957 行 · 第 1/3 页
HTM
957 行
"Zilpah" => ["Gad", "Asher"],};</pre></blockquote>That would be more or less equivalent to saying:<blockquote><pre class="programlisting">$kids_of_wife{"Jacob"}{"Leah"}[0] = "Reuben";$kids_of_wife{"Jacob"}{"Leah"}[1] = "Simeon";$kids_of_wife{"Jacob"}{"Leah"}[2] = "Levi";$kids_of_wife{"Jacob"}{"Leah"}[3] = "Judah";$kids_of_wife{"Jacob"}{"Leah"}[4] = "Issachar";$kids_of_wife{"Jacob"}{"Leah"}[5] = "Zebulun";$kids_of_wife{"Jacob"}{"Rachel"}[0] = "Joseph";$kids_of_wife{"Jacob"}{"Rachel"}[1] = "Benjamin";$kids_of_wife{"Jacob"}{"Bilhah"}[0] = "Dan";$kids_of_wife{"Jacob"}{"Bilhah"}[1] = "Naphtali";$kids_of_wife{"Jacob"}{"Zilpah"}[0] = "Gad";$kids_of_wife{"Jacob"}{"Zilpah"}[1] = "Asher";</pre></blockquote>You can see from this that adding a level to a nesteddata structure it is like adding another dimension to amultidimensional array. Perl lets you think of it either way, butthe internal representation is the same.</p><p><a name="INDEX-115"></a><a name="INDEX-116"></a><a name="INDEX-117"></a><a name="INDEX-118"></a><a name="INDEX-119"></a>The important point here is that Perl lets you pretend thata complex data structure is a simple scalar. On this simple kind ofencapsulation, Perl's entire object-oriented structure is built.When we earlier invoked the <tt class="literal">Camel</tt> constructor like this:<blockquote><pre class="programlisting">$fido = new Camel "Amelia";</pre></blockquote>we created a <tt class="literal">Camel</tt> object that is represented bythe scalar <tt class="literal">$fido</tt>. But the inside of the<tt class="literal">Camel</tt> is more complicated. As well-behavedobject-oriented programmers, we're not supposed to care about theinsides of <tt class="literal">Camel</tt>s (unless we happen to be thepeople implementing the methods of the <tt class="literal">Camel</tt>class). But generally, an object like a <tt class="literal">Camel</tt>would consist of a hash containing the particular<tt class="literal">Camel</tt>'s attributes, such as its name("<tt class="literal">Amelia</tt>" in this case, not"<tt class="literal">fido</tt>"), and the number of humps (which we didn'tspecify, but probably defaults to 1; check the front cover).</p><h3 class="sect3">1.2.1.4. Simplicities</h3><p><a name="INDEX-120"></a>If your head isn't spinning a bit from reading that last section, thenyou have an unusual head. People don't generally like to deal withcomplex data structures, whether governmental or genealogical. So inour natural languages, we have many ways of sweeping complexity underthe carpet. Many of these fall into the category of<em class="emphasis">topicalization</em>, which is just a fancy linguisticsterm for agreeing with someone about what you're going to talk about(and by exclusion, what you're probably not going to talk about). This happens on many levels in language. On a high level, we divideourselves up into various subcultures that are interested in varioussubtopics and establish sublanguages that talk primarily about thosesubtopics. The lingo of the doctor's office ("indissoluableasphyxiant") is different from the lingo of the chocolate factory("everlasting gobstopper"). Most of us automatically switch contextsas we go from one lingo to another.</p><p>On a conversational level, the context switch has to be more explicit,so our language gives us many ways of saying what we're about to say.We put titles on our books and headers on our sections. On oursentences, we put quaint phrases like "In regard to your recent query"or "For all X". Usually, though, we just say things like, "You knowthat dangley thingy that hangs down in the back of your throat?"</p><p><a name="INDEX-121"></a><a name="INDEX-122"></a><a name="INDEX-123"></a>Perl also has several ways of topicalizing. One important topicalizeris the <tt class="literal">package</tt> declaration. Suppose you want to talk about<tt class="literal">Camel</tt>s in Perl. You'd likely start off your <tt class="literal">Camel</tt> module by saying:<blockquote><pre class="programlisting">package Camel;</pre></blockquote><a name="INDEX-124"></a><a name="INDEX-125"></a>This has several notable effects. One of them is that Perl willassume from this point on that any unspecified verbs or nounsare about <tt class="literal">Camel</tt>s. It does this by automatically prefixing any globalname with the module name "<tt class="literal">Camel::</tt>". So if you say:<blockquote><pre class="programlisting">package Camel;$fido = &fetch();</pre></blockquote>then the real name of <tt class="literal">$fido</tt> is <tt class="literal">$Camel::fido</tt> (and the real name of<tt class="literal">&fetch</tt> is <tt class="literal">&Camel::fetch</tt>, but we're not talkingabout verbs yet). This means that if some other module says:<blockquote><pre class="programlisting">package Dog;$fido = &fetch();</pre></blockquote>Perl won't get confused, because the real name of this<tt class="literal">$fido</tt> is <tt class="literal">$Dog::fido</tt>, not<tt class="literal">$Camel::fido</tt>. A computer scientist would say thata package establishes a <em class="emphasis">namespace</em>. You can haveas many namespaces as you like, but since you're only in one of themat a time, you can pretend that the other namespaces don't exist.That's how namespaces simplify reality for you. Simplification isbased on pretending.(Of course, so is oversimplification, which iswhat we're doing in this chapter.)</p><p><a name="INDEX-126"></a><a name="INDEX-127"></a>Now it's important to keep your nouns straight, but it's just asimportant to keep your verbs straight. It's nice that<tt class="literal">&Camel::fetch</tt> is not confused with<tt class="literal">&Dog::fetch</tt> within the <tt class="literal">Camel</tt>and <tt class="literal">Dog</tt> namespaces, but the really nice thing aboutpackages is that they classify your verbs so that <em class="emphasis">other</em>packages can use them. When we said:<blockquote><pre class="programlisting">$fido = new Camel "Amelia";</pre></blockquote>we were actually invoking the <tt class="literal">&new</tt> verb in the <tt class="literal">Camel</tt> package, which has the full name of <tt class="literal">&Camel::new</tt>. And when we said:<blockquote><pre class="programlisting">$fido->saddle();</pre></blockquote>we were invoking the <tt class="literal">&Camel::saddle</tt> routine,because <tt class="literal">$fido</tt> remembers that it is pointing to a<tt class="literal">Camel</tt>. This is how object-oriented programmingworks.</p><p><a name="INDEX-128"></a><a name="INDEX-129"></a>When you say <tt class="literal">package Camel</tt>, you're starting a new package. Butsometimes you just want to borrow the nouns and verbs of an existingpackage. Perl lets you do that with a <tt class="literal">use</tt> declaration, which not onlyborrows verbs from another package, but alsochecks that the module you name is loaded in from disk. In fact, you<em class="emphasis">must</em> say something like:<blockquote><pre class="programlisting">use Camel;</pre></blockquote>before you say:<blockquote><pre class="programlisting">$fido = new Camel "Amelia";</pre></blockquote>because otherwise Perl wouldn't know what a <tt class="literal">Camel</tt> is.</p><p><a name="INDEX-130"></a><a name="INDEX-131"></a><a name="INDEX-132"></a>The interesting thing is that you yourself don't really need to knowwhat a <tt class="literal">Camel</tt> is, provided you can get someone elseto write the <tt class="literal">Camel</tt> module for you. Even betterwould be if someone had <em class="emphasis">already</em> written the<tt class="literal">Camel</tt> module for you. It could be argued that themost powerful thing about Perl is not Perl itself, but CPAN(Comprehensive Perl Archive Network), which contains myriads ofmodules that accomplish many different tasks that you don't have toknow how to do. You just have to download it and know how to say:<blockquote><pre class="programlisting">use Some::Cool::Module;</pre></blockquote>and then use the verbs from that module in a manner appropriate to thetopic under discussion.</p><p><a name="INDEX-133"></a><a name="INDEX-134"></a>So, like topicalization in a natural language, topicalization in Perl"warps" the language that you'll use from there to the end of theprogram. In fact, some of the built-in modules don't actuallyintroduce verbs at all, but simply warp the Perllanguagein various useful ways. These special modules we call<em class="emphasis">pragmas</em>. Forinstance, you'll often see peopleuse the pragma <tt class="literal">strict</tt>, like this:<blockquote><pre class="programlisting">use strict;</pre></blockquote><a name="INDEX-135"></a><a name="INDEX-136"></a><a name="INDEX-137"></a>What the <tt class="literal">strict</tt> module does is tighten up some ofthe rules so that you have to be more explicit about various thingsthat Perl would otherwise guess about, such as how you want yourvariables to be scoped. Making things explicit is helpful when you'reworking on large projects. By default Perl is optimized for smallprojects, but with the <tt class="literal">strict</tt> pragma, Perl is alsogood for large projects that need to be more maintainable. Since youcan add the <tt class="literal">strict</tt> pragma at any time, Perl is alsogood for evolving small projects into large ones, even when you didn'texpect that to happen. Which is usually.</p><h3 class="sect2">1.2.2. Verbs</h3><p><a name="INDEX-138"></a><a name="INDEX-139"></a><a name="INDEX-140"></a><a name="INDEX-141"></a>As is typical of your typical imperative computer language, many ofthe verbs in Perl are commands: they tell the Perl interpreter to dosomething. On the other hand, as is typical of a natural language,the meanings of Perl verbs tend to mush off in various directionsdepending on the context. A statement starting with a verb isgenerally purely imperative and evaluated entirely for its sideeffects. (We sometimes call these verbs<em class="emphasis">procedures</em>, especially when they'reuser-defined.) A frequently seen built-in command (in fact, you'veseen it already) is the <tt class="literal">print</tt> command:<blockquote><pre class="programlisting">print "Adam's wife is $wife{'Adam'}.\n";</pre></blockquote>This has the side effect of producing the desired output:<blockquote><pre class="programlisting">Adam's wife is Eve.</pre></blockquote><a name="INDEX-142"></a><a name="INDEX-143"></a></p><p>But there are other "moods" besides the imperative mood. Some verbsare for asking questions and are useful in conditionals such as<tt class="literal">if</tt> statements. Other verbs translate their inputparameters into return values, just as a recipe tells you how to turnraw ingredients into something (hopefully) edible. We tend to callthese verbs <em class="emphasis">functions</em>, in deference togenerations of mathematicians who don't know what the word"functional" means in normal English.</p><p>An example of a built-in function would be the exponential function:<blockquote><pre class="programlisting">$e = exp(1); # 2.718281828459 or thereabouts</pre></blockquote><a name="INDEX-144"></a><a name="INDEX-145"></a><a name="INDEX-146"></a><a name="INDEX-147"></a><a name="INDEX-148"></a>But Perl doesn't make a hard distinction between procedures andfunctions. You'll find the terms used interchangeably. Verbs arealso sometimes called operators (when built-in), or subroutines (whenuser-defined).<a href="#FOOTNOTE-9">[9]</a> But call themwhatever you like--they all return a value, which may or may notbe a meaningful value, which you may or may not choose to ignore.</p><blockquote class="footnote"><a name="FOOTNOTE-9"></a><p>[9] Historically, Perl required you to putan ampersand character (<tt class="literal">&</tt>) on any calls touser-defined subroutines (see <tt class="literal">$fido = &fetch();</tt>earlier). But with Perl version 5, the ampersand became optional, sothat user-defined verbs can now be called with the same syntax asbuilt-in verbs (<tt class="literal">$fido = fetch();</tt>). We still usethe ampersand when talking about the <em class="emphasis">name</em> of theroutine, such as when we take a reference to it (<tt class="literal">$fetcher =\&fetch;</tt>). Linguistically speaking, you can think ofthe ampersand form <tt class="literal">&fetch</tt> as an infinitive, "tofetch", or the similar form "do fetch". But we rarely say "do fetch"when we can just say "fetch". That's the real reason we dropped themandatory ampersand in Perl 5.</p></blockquote><p>As we go on, you'll see additional examples of how Perl behaves like anatural language. But there are other ways to look at Perl too. We'vealready sneakily introduced some notions from mathematical language,such as subscripts, addition, and the exponential function. But Perlis also a control language, a glue language, a prototyping language, atext-processing language, a list-processing language, and anobject-oriented language. Among other things.</p><p>But Perl is also just a plain old computer language. And that's howwe'll look at it next.</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="ch01_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="ch01_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">1.1. Getting Started</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">1.3. An Average Example</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 + =
减小字号Ctrl + -
显示快捷键?