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

📄 ch04_08.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>References and Complex Data Structures (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 &amp; 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="ch04_07.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="ch04_09.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">4.8. References and Complex Data Structures</h2><p><a name="INDEX-745" /><a name="INDEX-746" />A Perl reference is a fundamentaldata type that "points" to anotherpiece of data or code. A reference knows the location of theinformation and the type of data stored there.</p><p>A reference is a scalar and can be used anywhere a scalar can beused. Any array element or hash value can contain a reference (a hashkey cannot contain a reference), which is how nested data structuresare built in Perl. You can construct lists containing references toother lists, which can contain references to hashes, and so on.</p><a name="perlnut2-CHP-4-SECT-8.1" /><div class="sect2"><h3 class="sect2">4.8.1. Creating References</h3><p><a name="INDEX-747" /><a name="INDEX-748" />You can create a reference to anexisting variable or subroutine by prefixing it with a backslash:</p><blockquote><pre class="code">$a = "fondue";@alist = ("pitt", "hanks", "cage", "cruise");%song = ("mother" =&gt; "crying", "brother" =&gt; "dying");sub freaky_friday { s/mother/daughter/ }# Create references$ra = \$a;$ralist = \@alist;$rsong = \%song;$rsub = \&amp;freaky_friday; # '&amp;' required for subroutine names</pre></blockquote><p>References to scalar constants are created similarly: </p><blockquote><pre class="code">$pi = \3.14159;$myname = \"Charlie";</pre></blockquote><p><a name="INDEX-749" /><a name="INDEX-750" />Notethat all references are prefixed by a <tt class="literal">$</tt>, even ifthey refer to an array or hash. All references are scalars; thus, youcan copy a reference to another scalar or even reference anotherreference:</p><blockquote><pre class="code">$aref = \@names;$bref = $aref;            # Both refer to @names$cref = \$aref;           # $cref is a reference to $aref</pre></blockquote><p>Because arrays and hashes are collections of scalars, you can createreferences to individual elements by prefixing their names withbackslashes:</p><blockquote><pre class="code">$star = \$alist[2];       # Refers to third element of @alist$action = \$song{mother}; # Refers to the 'mother' value of %song</pre></blockquote><a name="perlnut2-CHP-4-SECT-8.1.1" /><div class="sect3"><h3 class="sect3">4.8.1.1. Referencing anonymous data</h3><p><a name="INDEX-751" />It is also possible to takereferences to literal data not stored in a variable. This data iscalled <em class="emphasis">anonymous</em> because it is not bound to anynamed variable.</p><p>To create a reference to a scalar constant, simply backslash theliteral string or number.</p><p>To create a reference to an anonymous array, place the list of valuesin square brackets:</p><blockquote><pre class="code">$shortbread = [ "flour", "butter", "eggs", "sugar" ];</pre></blockquote><p>This creates a reference to an array, but the array is available onlythrough the reference <tt class="literal">$shortbread</tt>.</p><p>A reference to an anonymous hash uses braces around the list ofelements:</p><blockquote><pre class="code">$cast =  { host     =&gt; "Space Ghost",           musician =&gt; "Zorak",           director =&gt; "Moltar" };</pre></blockquote></div></div><a name="perlnut2-CHP-4-SECT-8.2" /><div class="sect2"><h3 class="sect2">4.8.2. Dereferencing</h3><p><a name="INDEX-752" /><a name="INDEX-753" /><a name="INDEX-754" /><a name="INDEX-755" /><a name="INDEX-756" /><a name="INDEX-757" /><a name="INDEX-758" />Dereferencing returns the value areference points to. The general method of dereferencing uses thereference variable substituted for the regular name part of avariable. If <tt class="literal">$r</tt> is a reference, then<tt class="literal">$$r</tt>, <tt class="literal">@$r</tt>, or<tt class="literal">%$r</tt> retrieve the value that is referred to,depending on whether <tt class="literal">$r</tt> is pointing to a scalar,array, or hash. A reference can be used in all the places where anordinary data type can be used.</p><p>When a reference is accidentally evaluated as a plain scalar, itreturns a string that indicates the type of data it points to and thememory address of the data.</p><p>If you just want to know the type of data that is beingreferenced<a name="INDEX-759" />, use <tt class="literal">ref</tt>,which returns one of the following strings if its argument is areference. Otherwise, it returns false.</p><blockquote><pre class="code">SCALARARRAYHASHCODEGLOBREF</pre></blockquote><a name="perlnut2-CHP-4-SECT-8.2.1" /><div class="sect3"><h3 class="sect3">4.8.2.1. Arrow dereferencing</h3><p><a name="INDEX-760" /><a name="INDEX-761" /><a name="INDEX-762" /><a name="INDEX-763" />References to arrays, hashes, andsubroutines can be dereferenced using the <tt class="literal">-&gt;</tt>operator. This operator dereferences the expression to its left,which must resolve to an array or hash and accesses the elementrepresented by the subscripted expression on its right. For example,these three statements are equivalent:</p><blockquote><pre class="code">$$arrayref[0] = "man";${$arrayref}[0] = "man";$arrayref-&gt;[0] = "man";</pre></blockquote><p>The first statement dereferences <tt class="literal">$arrayref</tt> firstand finds the first element of that array. The second uses braces toclarify this procedure. The third statement uses the arrow notationto do the same thing.</p><p>The arrow dereferencing notation can be used only to access a singlescalar value. You cannot use arrow operators in expressions thatreturn either slices or whole arrays or hashes<a name="INDEX-764" /><a name="INDEX-765" /><a name="INDEX-766" />.</p></div></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_07.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="ch04_09.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">4.7. Subroutines</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">4.9. Filehandles</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 &copy; 2002</a> O'Reilly &amp; 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 + -