perlreftut.html
来自「perl教程」· HTML 代码 · 共 546 行 · 第 1/3 页
HTML
546 行
| | | +---------+------------+----------+
| USA | *---->| Chicago | Washington | New York |
| | | +---------+------------+----------+
+-------+---+</pre>
<p>We'll look at output first. Supposing we already have this structure,
how do we print it out?</p>
<pre>
<span class="number">8</span> <span class="keyword">foreach</span> <span class="variable">$country</span> <span class="operator">(</span><span class="keyword">sort</span> <span class="keyword">keys</span> <span class="variable">%table</span><span class="operator">)</span> <span class="operator">{</span>
<span class="number">9</span> <span class="keyword">print</span> <span class="string">"$country: "</span><span class="operator">;</span>
<span class="number">10</span> <span class="keyword">my</span> <span class="variable">@cities</span> <span class="operator">=</span> <span class="variable">@</span><span class="operator">{</span><span class="variable">$table</span><span class="operator">{</span><span class="variable">$country</span><span class="operator">}}</span><span class="operator">;</span>
<span class="number">11</span> <span class="keyword">print</span> <span class="keyword">join</span> <span class="string">', '</span><span class="operator">,</span> <span class="keyword">sort</span> <span class="variable">@cities</span><span class="operator">;</span>
<span class="number">12</span> <span class="keyword">print</span> <span class="string">".\n"</span><span class="operator">;</span>
<span class="number">13</span> <span class="operator">}</span>
</pre>
<p><code>%table</code> is an
ordinary hash, and we get a list of keys from it, sort the keys, and
loop over the keys as usual. The only use of references is in line 10.
<code>$table{$country}</code> looks up the key <code>$country</code> in the hash
and gets the value, which is a reference to an array of cities in that country.
<strong>Use Rule 1</strong> says that
we can recover the array by saying
<code>@{$table{$country}}</code>. Line 10 is just like</p>
<pre>
<span class="variable">@cities</span> <span class="operator">=</span> <span class="variable">@array</span><span class="operator">;</span>
</pre>
<p>except that the name <code>array</code> has been replaced by the reference
<code>{$table{$country}}</code>. The <code>@</code> tells Perl to get the entire array.
Having gotten the list of cities, we sort it, join it, and print it
out as usual.</p>
<p>Lines 2-7 are responsible for building the structure in the first
place. Here they are again:</p>
<pre>
<span class="number">2</span> <span class="keyword">while</span> <span class="operator">(<>)</span> <span class="operator">{</span>
<span class="number">3</span> <span class="keyword">chomp</span><span class="operator">;</span>
<span class="number">4</span> <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$city</span><span class="operator">,</span> <span class="variable">$country</span><span class="operator">)</span> <span class="operator">=</span> <span class="keyword">split</span> <span class="regex">/, /</span><span class="operator">;</span>
<span class="number">5</span> <span class="variable">$table</span><span class="operator">{</span><span class="variable">$country</span><span class="operator">}</span> <span class="operator">=</span> <span class="operator">[]</span> <span class="keyword">unless</span> <span class="keyword">exists</span> <span class="variable">$table</span><span class="operator">{</span><span class="variable">$country</span><span class="operator">}</span><span class="operator">;</span>
<span class="number">6</span> <span class="keyword">push</span> <span class="variable">@</span><span class="operator">{</span><span class="variable">$table</span><span class="operator">{</span><span class="variable">$country</span><span class="operator">}}</span><span class="operator">,</span> <span class="variable">$city</span><span class="operator">;</span>
<span class="number">7</span> <span class="operator">}</span>
</pre>
<p>Lines 2-4 acquire a city and country name. Line 5 looks to see if the
country is already present as a key in the hash. If it's not, the
program uses the <code>[]</code> notation (<strong>Make Rule 2</strong>) to manufacture a new,
empty anonymous array of cities, and installs a reference to it into
the hash under the appropriate key.</p>
<p>Line 6 installs the city name into the appropriate array.
<code>$table{$country}</code> now holds a reference to the array of cities seen
in that country so far. Line 6 is exactly like</p>
<pre>
<span class="keyword">push</span> <span class="variable">@array</span><span class="operator">,</span> <span class="variable">$city</span><span class="operator">;</span>
</pre>
<p>except that the name <code>array</code> has been replaced by the reference
<code>{$table{$country}}</code>. The <a href="../../lib/Pod/perlfunc.html#item_push"><code>push</code></a> adds a city name to the end of the
referred-to array.</p>
<p>There's one fine point I skipped. Line 5 is unnecessary, and we can
get rid of it.</p>
<pre>
<span class="number">2</span> <span class="keyword">while</span> <span class="operator">(<>)</span> <span class="operator">{</span>
<span class="number">3</span> <span class="keyword">chomp</span><span class="operator">;</span>
<span class="number">4</span> <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$city</span><span class="operator">,</span> <span class="variable">$country</span><span class="operator">)</span> <span class="operator">=</span> <span class="keyword">split</span> <span class="regex">/, /</span><span class="operator">;</span>
<span class="number">5</span> <span class="comment">#### $table{$country} = [] unless exists $table{$country};</span>
<span class="number">6</span> <span class="keyword">push</span> <span class="variable">@</span><span class="operator">{</span><span class="variable">$table</span><span class="operator">{</span><span class="variable">$country</span><span class="operator">}}</span><span class="operator">,</span> <span class="variable">$city</span><span class="operator">;</span>
<span class="number">7</span> <span class="operator">}</span>
</pre>
<p>If there's already an entry in <code>%table</code> for the current <code>$country</code>,
then nothing is different. Line 6 will locate the value in
<code>$table{$country}</code>, which is a reference to an array, and push
<code>$city</code> into the array. But
what does it do when
<code>$country</code> holds a key, say <code>Greece</code>, that is not yet in <code>%table</code>?</p>
<p>This is Perl, so it does the exact right thing. It sees that you want
to push <code>Athens</code> onto an array that doesn't exist, so it helpfully
makes a new, empty, anonymous array for you, installs it into
<code>%table</code>, and then pushes <code>Athens</code> onto it. This is called
`autovivification'--bringing things to life automatically. Perl saw
that they key wasn't in the hash, so it created a new hash entry
automatically. Perl saw that you wanted to use the hash value as an
array, so it created a new empty array and installed a reference to it
in the hash automatically. And as usual, Perl made the array one
element longer to hold the new city name.</p>
<p>
</p>
<hr />
<h1><a name="the_rest">The Rest</a></h1>
<p>I promised to give you 90% of the benefit with 10% of the details, and
that means I left out 90% of the details. Now that you have an
overview of the important parts, it should be easier to read the
<a href="../../lib/Pod/perlref.html">the perlref manpage</a> manual page, which discusses 100% of the details.</p>
<p>Some of the highlights of <a href="../../lib/Pod/perlref.html">the perlref manpage</a>:</p>
<ul>
<li>
<p>You can make references to anything, including scalars, functions, and
other references.</p>
</li>
<li>
<p>In <strong>Use Rule 1</strong>, you can omit the curly brackets whenever the thing
inside them is an atomic scalar variable like <code>$aref</code>. For example,
<code>@$aref</code> is the same as <code>@{$aref}</code>, and <code>$$aref[1]</code> is the same as
<code>${$aref}[1]</code>. If you're just starting out, you may want to adopt
the habit of always including the curly brackets.</p>
</li>
<li>
<p>This doesn't copy the underlying array:</p>
<pre>
<span class="variable">$aref2</span> <span class="operator">=</span> <span class="variable">$aref1</span><span class="operator">;</span>
</pre>
<p>You get two references to the same array. If you modify
<code>$aref1->[23]</code> and then look at
<code>$aref2->[23]</code> you'll see the change.</p>
<p>To copy the array, use</p>
<pre>
<span class="variable">$aref2</span> <span class="operator">=</span> <span class="operator">[</span><span class="variable">@</span><span class="operator">{</span><span class="variable">$aref1</span><span class="operator">}]</span><span class="operator">;</span>
</pre>
<p>This uses <code>[...]</code> notation to create a new anonymous array, and
<code>$aref2</code> is assigned a reference to the new array. The new array is
initialized with the contents of the array referred to by <code>$aref1</code>.</p>
<p>Similarly, to copy an anonymous hash, you can use</p>
<pre>
<span class="variable">$href2</span> <span class="operator">=</span> <span class="operator">{</span><span class="variable">%</span><span class="operator">{</span><span class="variable">$href1</span><span class="operator">}}</span><span class="operator">;</span>
</pre>
</li>
<li>
<p>To see if a variable contains a reference, use the <a href="../../lib/Pod/perlfunc.html#item_ref"><code>ref</code></a> function. It
returns true if its argument is a reference. Actually it's a little
better than that: It returns <code>HASH</code> for hash references and <code>ARRAY</code>
for array references.</p>
</li>
<li>
<p>If you try to use a reference like a string, you get strings like</p>
<pre>
ARRAY(0x80f5dec) or HASH(0x826afc0)</pre>
<p>If you ever see a string that looks like this, you'll know you
printed out a reference by mistake.</p>
<p>A side effect of this representation is that you can use <code>eq</code> to see
if two references refer to the same thing. (But you should usually use
<code>==</code> instead because it's much faster.)</p>
</li>
<li>
<p>You can use a string as if it were a reference. If you use the string
<code>"foo"</code> as an array reference, it's taken to be a reference to the
array <code>@foo</code>. This is called a <em>soft reference</em> or <em>symbolic
reference</em>. The declaration <code>use strict 'refs'</code> disables this
feature, which can cause all sorts of trouble if you use it by accident.</p>
</li>
</ul>
<p>You might prefer to go on to <a href="../../lib/Pod/perllol.html">the perllol manpage</a> instead of <a href="../../lib/Pod/perlref.html">the perlref manpage</a>; it
discusses lists of lists and multidimensional arrays in detail. After
that, you should move on to <a href="../../lib/Pod/perldsc.html">the perldsc manpage</a>; it's a Data Structure Cookbook
that shows recipes for using and printing out arrays of hashes, hashes
of arrays, and other kinds of data.</p>
<p>
</p>
<hr />
<h1><a name="summary">Summary</a></h1>
<p>Everyone needs compound data structures, and in Perl the way you get
them is with references. There are four important rules for managing
references: Two for making references and two for using them. Once
you know these rules you can do most of the important things you need
to do with references.</p>
<p>
</p>
<hr />
<h1><a name="credits">Credits</a></h1>
<p>Author: Mark Jason Dominus, Plover Systems (<code>mjd-perl-ref+@plover.com</code>)</p>
<p>This article originally appeared in <em>The Perl Journal</em>
( <a href="http://www.tpj.com/">http://www.tpj.com/</a> ) volume 3, #2. Reprinted with permission.</p>
<p>The original title was <em>Understand References Today</em>.</p>
<p>
</p>
<h2><a name="distribution_conditions">Distribution Conditions</a></h2>
<p>Copyright 1998 The Perl Journal.</p>
<p>This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.</p>
<p>Irrespective of its distribution, all code examples in these files are
hereby placed into the public domain. You are permitted and
encouraged to use this code in your own programs for fun or for profit
as you see fit. A simple comment in the code giving credit would be
courteous but is not required.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?