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

📄 ch08_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>References (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="References"><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="ch07_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="ch08_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 8.  References</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch08_01.htm">What Is a Reference?</a><br><a href="ch08_02.htm">Creating References</a><br><a href="ch08_03.htm">Using Hard References</a><br><a href="ch08_04.htm">Symbolic References</a><br><a href="ch08_05.htm">Braces, Brackets, and Quoting</a><br></p></div><a name="INDEX-1945"></a><a name="INDEX-1946"></a><a name="INDEX-1947"></a><a name="INDEX-1948"></a><p><a name="INDEX-1949"></a>For both practical and philosophical reasons, Perl has always beenbiased in favor of flat, linear data structures.  And for manyproblems, this is just what you want.</p><p>Suppose you wanted to build a simple table (two-dimensional array)showing vital statistics--age, eye color, and weight--for a groupof people.  You could do this by first creating an array for eachindividual:<blockquote><pre class="programlisting">@john = (47, "brown", 186);@mary = (23, "hazel", 128);@bill = (35, "blue",  157);</pre></blockquote>You could then construct a single, additional array consisting of the namesof the other arrays:<blockquote><pre class="programlisting">@vitals = ('john', 'mary', 'bill');</pre></blockquote><a name="INDEX-1950"></a><a name="INDEX-1951"></a><a name="INDEX-1952"></a>To change John's eyes to "red" after a night on the town, we want away to change the contents of the <tt class="literal">@john</tt> array givenonly the simple string "<tt class="literal">john</tt>".  This isthe basic problem of <em class="emphasis">indirection</em>, which variouslanguages solve in various ways.  In C, the most common form ofindirection is the pointer, which lets one variable hold the memoryaddress of another variable.  In Perl, the most common form ofindirection is the <em class="emphasis">reference</em>.</p><h2 class="sect1">8.1. What Is a Reference?</h2><a name="INDEX-1953"></a><a name="INDEX-1954"></a><a name="INDEX-1955"></a><p>In our example, <tt class="literal">$vitals[0]</tt> has the value "<tt class="literal">john</tt>". That is, itcontains a string that happens to be the name of another (global)variable.  We say that the first variable <em class="emphasis">refers</em> to the second, andthis sort of reference is called a <em class="emphasis">symbolic</em> reference, since Perlhas to look up <tt class="literal">@john</tt> in a symbol table to find it.  (You might thinkof symbolic references as analogous to symbolic links in the filesystem.)  We'll talk about symbolic references later in this chapter.</p><p>The other kind of reference is a <em class="emphasis">hard</em> reference, and this is whatmost Perl programmers use to accomplish their indirections (if nottheir indiscretions).  We call them hard references not because they'redifficult, but because they're real and solid.  If you like, think ofhard references as real references and symbolic references as fakereferences.  It's like the difference between true friendship and merename-dropping.  When we don't specify which type of reference we mean,it's a hard reference.  <a href="ch08_01.htm#perl3-figure-hard-ref">Figure 8-1</a> depicts a variable named <tt class="literal">$bar</tt>referring to the contents of a scalar named <tt class="literal">$foo</tt> which has the value "<tt class="literal">bot</tt>".</p><a name="perl3-figure-hard-ref"></a><div class="figure"></div><h4 class="objtitle">Figure 8.1. A hard reference and a symbolic reference</h4><p><a name="INDEX-1956"></a>Unlike a symbolic reference, a real reference refers not to the name ofanother variable (which is just a container for a value) but to anactual value itself, some internal glob of data.  There's no good wordfor that thing, but when we have to, we'll call it a <em class="emphasis">referent</em>.Suppose, for example, that you create a hard reference to a lexicallyscoped array named <tt class="literal">@array</tt>.  This hard reference, and the referent itrefers to, will continue to exist even after <tt class="literal">@array</tt> goesout of scope.  A referent is only destroyed when all the references to itare eliminated.</p><p>A referent doesn't really have a name of its own, apart from thereferences to it.  To put it another way, every Perl variable namelives in some kind of symbol table, holding one hard reference to itsunderlying (otherwise nameless) referent.  That referent might besimple, like a number or string, or complex, like an array or hash.Either way, there's still exactly one reference from the variable toits value.  You might create additional hard references to the samereferent, but if so, the variable doesn't know (or care) about them.<a href="#FOOTNOTE-1">[1]</a></p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1] If you're curious, you can determine the underlying refcountwith the <tt class="literal">Devel::Peek</tt> module, bundled with Perl.</p></blockquote><p><a name="INDEX-1957"></a><a name="INDEX-1958"></a><a name="INDEX-1959"></a>A symbolic reference is just a string that happens to name something ina package symbol table.  It's not so much a distinct type as it issomething you do with a string.  But a hard reference is a differentbeast entirely.  It is the third of the three kinds of fundamentalscalar data types, the other two being strings and numbers.  A hardreference doesn't know something's name just to refer to it,and it's actually completely normal for there to <em class="emphasis">be</em> no name to usein the first place.  Such totally nameless referents are called<em class="emphasis">anonymous</em>; we discuss them in "Anonymous Data" below.</p><p><a name="INDEX-1960"></a><a name="INDEX-1961"></a><a name="INDEX-1962"></a>To <em class="emphasis">reference</em> a value, in the terminology of this chapter, is tocreate a hard reference to it.  (There's a special operator for thiscreative act.)  The reference so created is simply a scalar, whichbehaves in all familiar contexts just like any other scalar.  To<em class="emphasis">dereference</em> this scalar means to use the reference to get at thereferent.  Both referencing and dereferencing occur only when youinvoke certain explicit mechanisms; implicit referencing ordereferencing never occurs in Perl.  Well, almost never.<a name="INDEX-1963"></a></p><p><a name="INDEX-1964"></a><a name="INDEX-1965"></a><a name="INDEX-1966"></a><a name="INDEX-1967"></a><a name="INDEX-1968"></a>A function call <em class="emphasis">can</em> use implicit pass-by-reference semantics--if ithas a prototype declaring it that way.  If so, the caller of thefunction doesn't explicitly pass a reference, although youstill have to dereference it explicitly within the function.  See thesection <a href="ch06_04.htm#ch06-sect-proto">Section 8.4, "Prototypes"</a> in <a href="ch06_01.htm">Chapter 6, "Subroutines"</a>.  And to beperfectly honest, there's also some behind-the-scenes dereferencinghappening when you use certain kinds of filehandles, but that's forbackward compatibility and is transparent to the casual user.  Finally,two built-in functions, <tt class="literal">bless</tt> and <tt class="literal">lock</tt>, each take a reference fortheir argument but implicitly dereference it to work their magic onwhat lies behind.  But those confessions aside, the basic principlestill holds that Perl isn't interested in muddling your levels ofindirection.</p><p><a name="INDEX-1969"></a><a name="INDEX-1970"></a><a name="INDEX-1971"></a><a name="INDEX-1972"></a>A reference can point to any data structure.  Since references arescalars, you can store them in arrays and hashes, and thus buildarrays of arrays, arrays of hashes, hashes of arrays, arrays of hashesand functions, and so on.  There are examples of these in <a href="ch09_01.htm">Chapter 9, "Data Structures"</a>.</p><p>Keep in mind, though, that Perl arrays and hashes are internallyone-dimensional.  That is, their elements can hold only scalar values(strings, numbers, and references).  When we use a phrase like "arrayof arrays", we really mean "array of references to arrays", just aswhen we say "hash of functions" we really mean "hash of references tosubroutines".  But since references are the only way to implement suchstructures in Perl, it follows that the shorter, less accurate phraseis not so inaccurate as to be false, and therefore should not betotally despised, unless you're into that sort of thing.</p><a name="INDEX-1973"></a><a name="INDEX-2069"></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="ch07_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="ch08_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">7.2. Footers</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">8.2. Creating References</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 + -