📄 ch11_01.htm
字号:
CLASS="replaceable"><I>fred</I></CODE></B></CODE>print $a[4][23][53];<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>ARRAY(0x81e2494)</I></CODE></B></CODE>print $a[4][23];<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>ARRAY(0x81e0748)</I></CODE></B></CODE>print $a[4];<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>ARRAY(0x822cd40)</I></CODE></B></CODE></PRE><PCLASS="para">The following table shows mechanisms for producing references to both named and anonymous scalars, arrays, hashes, and functions. (Anonymous typeglobs are too scary to show - and virtually never used. It's best to use <CODECLASS="literal">Symbol::gensym()</CODE> or <CODECLASS="literal">IO::Handle->new()</CODE> for them.)</P><TABLECLASS="informaltable"BORDER="1"CELLPADDING="3"><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Reference to</P></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Named</P></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Anonymous</P></TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Scalar</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">\$scalar</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">\do{my $anon}</CODE></P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Array</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">\@array</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">[ LIST ]</CODE></P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Hash</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">\%hash</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">{ LIST }</CODE></P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Code</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">\&function</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">sub { CODE } </CODE></P></TD></TR></TBODY></TABLE><PCLASS="para">These diagrams illustrate the differences between named and anonymous values. <ACLASS="xref"HREF="ch11_01.htm#ch11-17738"TITLE="Named values">Figure 11.2</A> shows named values.</P><H4CLASS="figure"><ACLASS="title"NAME="ch11-17738">Figure 11.2: Named values</A></H4><IMGCLASS="graphic"SRC="figs/pcb_1102.gif"ALT="Figure 11.2"><PCLASS="para">In other words, saying <CODECLASS="literal">$a</CODE> <CODECLASS="literal">=</CODE> <CODECLASS="literal">\$b</CODE> makes <CODECLASS="literal">$$a</CODE> and <CODECLASS="literal">$b</CODE> the <EMCLASS="emphasis">same piece of memory</EM>. If you say <CODECLASS="literal">$$a</CODE> <CODECLASS="literal">=</CODE> <CODECLASS="literal">3</CODE>, then the value of <CODECLASS="literal">$b</CODE> is set to 3.</P><PCLASS="para"><ACLASS="xref"HREF="ch11_01.htm#ch11-31355"TITLE="Anonymous values">Figure 11.3</A> shows anonymous values.</P><H4CLASS="figure"><ACLASS="title"NAME="ch11-31355">Figure 11.3: Anonymous values</A></H4><IMGCLASS="graphic"SRC="figs/pcb_1103.gif"ALT="Figure 11.3"><PCLASS="para">Every reference evaluates as true, by definition, so if you write a subroutine that returns a reference, you can return <CODECLASS="literal">undef</CODE> on error and check for it with:</P><PRECLASS="programlisting">$op_cit = cite($ibid) or die "couldn't make a reference";</PRE><PCLASS="para">The <CODECLASS="literal">undef</CODE><ACLASS="indexterm"NAME="ch11-idx-1000004791-0"></A> operator can be used on any variable or function in Perl to free its memory. This does not necessarily free memory, call object destructors, etc. It just decrements the reference count by one.<ACLASS="indexterm"NAME="ch11-idx-1000004781-0"></A> Without an argument, <CODECLASS="literal">undef</CODE> produces an undefined value.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-chap11_records_0">Records</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch11-idx-1000004782-0"></A>The classic use of references in Perl is to circumvent the restriction that arrays and hashes may hold scalars only. References are scalars, so to make an array of arrays, make an array of array <EMCLASS="emphasis">references</EM>. Similarly, hashes of hashes are implemented as hashes of hash references, arrays of hashes as arrays of hash references, hashes of arrays as hashes of array references, and so on.</P><PCLASS="para">Once you have these complex structures, you can use them to implement records. A record is a single logical unit composed of different attributes. For instance, a name, an address, and a birthday might comprise a record representing a person. C calls such things <EMCLASS="emphasis">structs</EM>, and Pascal calls them <EMCLASS="emphasis">RECORDs</EM>. Perl doesn't have a particular name for these because you can implement this notion in different ways.</P><PCLASS="para">The most common technique in Perl is to treat a hash as a record, where the keys of the hash are the record's field names and the values of the hash are those fields' values.</P><PCLASS="para">For instance, we might create a "person" record like this:</P><PRECLASS="programlisting">$Nat = { "Name" => "Leonhard Euler", "Address" => "1729 Ramanujan Lane\nMathworld, PI 31416", "Birthday" => 0x5bb5580, };</PRE><PCLASS="para">Because <CODECLASS="literal">$Nat</CODE> is a scalar, it can be stored in an array or hash element, thus creating create groups of people. Now apply the array and hash techniques from Chapters 4 and 5 to sort the sets, merge hashes, pick a random record, and so on.</P><PCLASS="para">The attributes of a record, including the "person" record, are always scalars. You can certainly use numbers as readily as strings there, but that's no great trick. The real power play happens when you use even more references for values in the record. <CODECLASS="literal">"Birthday"</CODE>, for instance, might be stored as an anonymous array with three elements: day, month, and year. You could then say <CODECLASS="literal">$person->{"Birthday"}->[0]</CODE> to access just the day field. Or a date might be represented as a hash record, which would then lend itself to access such as <CODECLASS="literal">$person->{"Birthday"}->{"day"}</CODE>. Adding references to your collection of skills makes possible many more complex and useful programming strategies.</P><PCLASS="para">At this point, we've conceptually moved beyond simple records. We're now creating elaborate data structures that represent complicated relationships between the data they hold. Although we <EMCLASS="emphasis">can</EM> use these to implement traditional data structures like linked lists, the recipes in the second half of this chapter don't deal specifically with any particular structure. Instead, they give generic techniques for loading, printing, copying, and saving generic data structures. The final program example demonstrates how to manipulate binary trees.<ACLASS="indexterm"NAME="ch11-idx-1000004783-0"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-180">See Also</A></H3><PCLASS="para"><ACLASS="olink"HREF="../prog/ch04_01.htm">Chapter 4</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; <ICLASS="filename">perlref </I>(1), <ICLASS="filename">perllol</I> (1), and <ICLASS="filename">perldsc</I> (1)</P></DIV></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch10_18.htm"TITLE="10.17. Program: Sorting Your Mail"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 10.17. Program: Sorting Your Mail"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch11_02.htm"TITLE="11.1. Taking References to Arrays"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 11.1. Taking References to Arrays"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">10.17. Program: Sorting Your Mail</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">11.1. Taking References to Arrays</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + -