perlref.html
来自「perl教程」· HTML 代码 · 共 733 行 · 第 1/5 页
HTML
733 行
<?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>perlref - Perl references and nested data structures</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>perlref - Perl references and nested data structures</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#note">NOTE</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#making_references">Making References</a></li>
<li><a href="#using_references">Using References</a></li>
<li><a href="#symbolic_references">Symbolic references</a></li>
<li><a href="#notsosymbolic_references">Not-so-symbolic references</a></li>
<li><a href="#pseudohashes__using_an_array_as_a_hash">Pseudo-hashes: Using an array as a hash</a></li>
<li><a href="#function_templates">Function Templates</a></li>
</ul>
<li><a href="#warning">WARNING</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlref - Perl references and nested data structures</p>
<p>
</p>
<hr />
<h1><a name="note">NOTE</a></h1>
<p>This is complete documentation about all aspects of references.
For a shorter, tutorial introduction to just the essential features,
see <a href="../../lib/Pod/perlreftut.html">the perlreftut manpage</a>.</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Before release 5 of Perl it was difficult to represent complex data
structures, because all references had to be symbolic--and even then
it was difficult to refer to a variable instead of a symbol table entry.
Perl now not only makes it easier to use symbolic references to variables,
but also lets you have "hard" references to any piece of data or code.
Any scalar may hold a hard reference. Because arrays and hashes contain
scalars, you can now easily build arrays of arrays, arrays of hashes,
hashes of arrays, arrays of hashes of functions, and so on.</p>
<p>Hard references are smart--they keep track of reference counts for you,
automatically freeing the thing referred to when its reference count goes
to zero. (Reference counts for values in self-referential or
cyclic data structures may not go to zero without a little help; see
<a href="../../lib/Pod/perlobj.html#twophased_garbage_collection">Two-Phased Garbage Collection in the perlobj manpage</a> for a detailed explanation.)
If that thing happens to be an object, the object is destructed. See
<a href="../../lib/Pod/perlobj.html">the perlobj manpage</a> for more about objects. (In a sense, everything in Perl is an
object, but we usually reserve the word for references to objects that
have been officially "blessed" into a class package.)</p>
<p>Symbolic references are names of variables or other objects, just as a
symbolic link in a Unix filesystem contains merely the name of a file.
The <code>*glob</code> notation is something of a symbolic reference. (Symbolic
references are sometimes called "soft references", but please don't call
them that; references are confusing enough without useless synonyms.)</p>
<p>In contrast, hard references are more like hard links in a Unix file
system: They are used to access an underlying object without concern for
what its (other) name is. When the word "reference" is used without an
adjective, as in the following paragraph, it is usually talking about a
hard reference.</p>
<p>References are easy to use in Perl. There is just one overriding
principle: Perl does no implicit referencing or dereferencing. When a
scalar is holding a reference, it always behaves as a simple scalar. It
doesn't magically start being an array or hash or subroutine; you have to
tell it explicitly to do so, by dereferencing it.</p>
<p>
</p>
<h2><a name="making_references">Making References</a></h2>
<p>References can be created in several ways.</p>
<ol>
<li><strong><a name="item_"></a></strong>
<p>By using the backslash operator on a variable, subroutine, or value.
(This works much like the & (address-of) operator in C.)
This typically creates <em>another</em> reference to a variable, because
there's already a reference to the variable in the symbol table. But
the symbol table reference might go away, and you'll still have the
reference that the backslash returned. Here are some examples:</p>
<pre>
<span class="variable">$scalarref</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">$foo</span><span class="operator">;</span>
<span class="variable">$arrayref</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">@ARGV</span><span class="operator">;</span>
<span class="variable">$hashref</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">%ENV</span><span class="operator">;</span>
<span class="variable">$coderef</span> <span class="operator">=</span> <span class="operator">\&</span><span class="variable">handler</span><span class="operator">;</span>
<span class="variable">$globref</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">*foo</span><span class="operator">;</span>
</pre>
<p>It isn't possible to create a true reference to an IO handle (filehandle
or dirhandle) using the backslash operator. The most you can get is a
reference to a typeglob, which is actually a complete symbol table entry.
But see the explanation of the <code>*foo{THING}</code> syntax below. However,
you can still use type globs and globrefs as though they were IO handles.</p>
</li>
<li><strong></strong>
<p>A reference to an anonymous array can be created using square
brackets:</p>
<pre>
<span class="variable">$arrayref</span> <span class="operator">=</span> <span class="operator">[</span><span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="operator">[</span><span class="string">'a'</span><span class="operator">,</span> <span class="string">'b'</span><span class="operator">,</span> <span class="string">'c'</span><span class="operator">]]</span><span class="operator">;</span>
</pre>
<p>Here we've created a reference to an anonymous array of three elements
whose final element is itself a reference to another anonymous array of three
elements. (The multidimensional syntax described later can be used to
access this. For example, after the above, <code>$arrayref->[2][1]</code> would have
the value "b".)</p>
<p>Taking a reference to an enumerated list is not the same
as using square brackets--instead it's the same as creating
a list of references!</p>
<pre>
<span class="variable">@list</span> <span class="operator">=</span> <span class="operator">(\</span><span class="variable">$a</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">@b</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">%c</span><span class="operator">);</span>
<span class="variable">@list</span> <span class="operator">=</span> <span class="operator">\(</span><span class="variable">$a</span><span class="operator">,</span> <span class="variable">@b</span><span class="operator">,</span> <span class="variable">%c</span><span class="operator">);</span> <span class="comment"># same thing!</span>
</pre>
<p>As a special case, <code>\(@foo)</code> returns a list of references to the contents
of <code>@foo</code>, not a reference to <code>@foo</code> itself. Likewise for <code>%foo</code>,
except that the key references are to copies (since the keys are just
strings rather than full-fledged scalars).</p>
</li>
<li><strong></strong>
<p>A reference to an anonymous hash can be created using curly
brackets:</p>
<pre>
<span class="variable">$hashref</span> <span class="operator">=</span> <span class="operator">{</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?