perlreftut.html
来自「perl教程」· HTML 代码 · 共 546 行 · 第 1/3 页
HTML
546 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlreftut - Mark's very short tutorial about references</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perlreftut - Mark's very short tutorial about references</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#who_needs_complicated_data_structures">Who Needs Complicated Data Structures?</a></li>
<li><a href="#the_solution">The Solution</a></li>
<li><a href="#syntax">Syntax</a></li>
<ul>
<li><a href="#making_references">Making References</a></li>
<ul>
<li><a href="#make_rule_1"><strong>Make Rule 1</strong></a></li>
</ul>
<li><a href="#using_references">Using References</a></li>
<ul>
<li><a href="#use_rule_1"><strong>Use Rule 1</strong></a></li>
<li><a href="#use_rule_2"><strong>Use Rule 2</strong></a></li>
</ul>
<li><a href="#an_example">An Example</a></li>
<li><a href="#arrow_rule">Arrow Rule</a></li>
</ul>
<li><a href="#solution">Solution</a></li>
<li><a href="#the_rest">The Rest</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#credits">Credits</a></li>
<ul>
<li><a href="#distribution_conditions">Distribution Conditions</a></li>
</ul>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlreftut - Mark's very short tutorial about references</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>One of the most important new features in Perl 5 was the capability to
manage complicated data structures like multidimensional arrays and
nested hashes. To enable these, Perl 5 introduced a feature called
`references', and using references is the key to managing complicated,
structured data in Perl. Unfortunately, there's a lot of funny syntax
to learn, and the main manual page can be hard to follow. The manual
is quite complete, and sometimes people find that a problem, because
it can be hard to tell what is important and what isn't.</p>
<p>Fortunately, you only need to know 10% of what's in the main page to get
90% of the benefit. This page will show you that 10%.</p>
<p>
</p>
<hr />
<h1><a name="who_needs_complicated_data_structures">Who Needs Complicated Data Structures?</a></h1>
<p>One problem that came up all the time in Perl 4 was how to represent a
hash whose values were lists. Perl 4 had hashes, of course, but the
values had to be scalars; they couldn't be lists.</p>
<p>Why would you want a hash of lists? Let's take a simple example: You
have a file of city and country names, like this:</p>
<pre>
Chicago, USA
Frankfurt, Germany
Berlin, Germany
Washington, USA
Helsinki, Finland
New York, USA</pre>
<p>and you want to produce an output like this, with each country mentioned
once, and then an alphabetical list of the cities in that country:</p>
<pre>
Finland: Helsinki.
Germany: Berlin, Frankfurt.
USA: Chicago, New York, Washington.</pre>
<p>The natural way to do this is to have a hash whose keys are country
names. Associated with each country name key is a list of the cities in
that country. Each time you read a line of input, split it into a country
and a city, look up the list of cities already known to be in that
country, and append the new city to the list. When you're done reading
the input, iterate over the hash as usual, sorting each list of cities
before you print it out.</p>
<p>If hash values can't be lists, you lose. In Perl 4, hash values can't
be lists; they can only be strings. You lose. You'd probably have to
combine all the cities into a single string somehow, and then when
time came to write the output, you'd have to break the string into a
list, sort the list, and turn it back into a string. This is messy
and error-prone. And it's frustrating, because Perl already has
perfectly good lists that would solve the problem if only you could
use them.</p>
<p>
</p>
<hr />
<h1><a name="the_solution">The Solution</a></h1>
<p>By the time Perl 5 rolled around, we were already stuck with this
design: Hash values must be scalars. The solution to this is
references.</p>
<p>A reference is a scalar value that <em>refers to</em> an entire array or an
entire hash (or to just about anything else). Names are one kind of
reference that you're already familiar with. Think of the President
of the United States: a messy, inconvenient bag of blood and bones.
But to talk about him, or to represent him in a computer program, all
you need is the easy, convenient scalar string "George Bush".</p>
<p>References in Perl are like names for arrays and hashes. They're
Perl's private, internal names, so you can be sure they're
unambiguous. Unlike "George Bush", a reference only refers to one
thing, and you always know what it refers to. If you have a reference
to an array, you can recover the entire array from it. If you have a
reference to a hash, you can recover the entire hash. But the
reference is still an easy, compact scalar value.</p>
<p>You can't have a hash whose values are arrays; hash values can only be
scalars. We're stuck with that. But a single reference can refer to
an entire array, and references are scalars, so you can have a hash of
references to arrays, and it'll act a lot like a hash of arrays, and
it'll be just as useful as a hash of arrays.</p>
<p>We'll come back to this city-country problem later, after we've seen
some syntax for managing references.</p>
<p>
</p>
<hr />
<h1><a name="syntax">Syntax</a></h1>
<p>There are just two ways to make a reference, and just two ways to use
it once you have it.</p>
<p>
</p>
<h2><a name="making_references">Making References</a></h2>
<p>
</p>
<h3><a name="make_rule_1"><strong>Make Rule 1</strong></a></h3>
<p>If you put a <code>\</code> in front of a variable, you get a
reference to that variable.</p>
<pre>
<span class="variable">$aref</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">@array</span><span class="operator">;</span> <span class="comment"># $aref now holds a reference to @array</span>
<span class="variable">$href</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">%hash</span><span class="operator">;</span> <span class="comment"># $href now holds a reference to %hash</span>
<span class="variable">$sref</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">$scalar</span><span class="operator">;</span> <span class="comment"># $sref now holds a reference to $scalar</span>
</pre>
<p>Once the reference is stored in a variable like $aref or $href, you
can copy it or store it just the same as any other scalar value:</p>
<pre>
<span class="variable">$xy</span> <span class="operator">=</span> <span class="variable">$aref</span><span class="operator">;</span> <span class="comment"># $xy now holds a reference to @array</span>
<span class="variable">$p</span><span class="operator">[</span><span class="number">3</span><span class="operator">]</span> <span class="operator">=</span> <span class="variable">$href</span><span class="operator">;</span> <span class="comment"># $p[3] now holds a reference to %hash</span>
<span class="variable">$z</span> <span class="operator">=</span> <span class="variable">$p</span><span class="operator">[</span><span class="number">3</span><span class="operator">]</span><span class="operator">;</span> <span class="comment"># $z now holds a reference to %hash</span>
</pre>
<p>These examples show how to make references to variables with names.
Sometimes you want to make an array or a hash that doesn't have a
name. This is analogous to the way you like to be able to use the
string <code>"\n"</code> or the number 80 without having to store it in a named
variable first.</p>
<p><strong>Make Rule 2</strong></p>
<p><code>[ ITEMS ]</code> makes a new, anonymous array, and returns a reference to
that array. <code>{ ITEMS }</code> makes a new, anonymous hash, and returns a
reference to that hash.</p>
<pre>
<span class="variable">$aref</span> <span class="operator">=</span> <span class="operator">[</span> <span class="number">1</span><span class="operator">,</span> <span class="string">"foo"</span><span class="operator">,</span> <span class="keyword">undef</span><span class="operator">,</span> <span class="number">13</span> <span class="operator">]</span><span class="operator">;</span>
<span class="comment"># $aref now holds a reference to an array</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?