perlref.html

来自「perl教程」· HTML 代码 · 共 733 行 · 第 1/5 页

HTML
733
字号
    <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$ref1</span> <span class="operator">==</span> <span class="variable">$ref2</span><span class="operator">)</span> <span class="operator">{</span>  <span class="comment"># cheap numeric compare of references</span>
        <span class="keyword">print</span> <span class="string">"refs 1 and 2 refer to the same thing\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>Using a reference as a string produces both its referent's type,
including any package blessing as described in <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a>, as well
as the numeric address expressed in hex.  The <a href="../../lib/Pod/perlfunc.html#item_ref"><code>ref()</code></a> operator returns
just the type of thing the reference is pointing to, without the
address.  See <a href="../../lib/Pod/perlfunc.html#ref">ref in the perlfunc manpage</a> for details and examples of its use.</p>
<p>The <a href="../../lib/Pod/perlfunc.html#item_bless"><code>bless()</code></a> operator may be used to associate the object a reference
points to with a package functioning as an object class.  See <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a>.</p>
<p>A typeglob may be dereferenced the same way a reference can, because
the dereference syntax always indicates the type of reference desired.
So <code>${*foo}</code> and <code>${\$foo}</code> both indicate the same scalar variable.</p>
<p>Here's a trick for interpolating a subroutine call into a string:</p>
<pre>
    <span class="keyword">print</span> <span class="string">"My sub returned @{[mysub(1,2,3)]} that time.\n"</span><span class="operator">;</span>
</pre>
<p>The way it works is that when the <code>@{...}</code> is seen in the double-quoted
string, it's evaluated as a block.  The block creates a reference to an
anonymous array containing the results of the call to <code>mysub(1,2,3)</code>.  So
the whole block returns a reference to an array, which is then
dereferenced by <code>@{...}</code> and stuck into the double-quoted string. This
chicanery is also useful for arbitrary expressions:</p>
<pre>
    <span class="keyword">print</span> <span class="string">"That yields @{[$n + 5]} widgets\n"</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="symbolic_references">Symbolic references</a></h2>
<p>We said that references spring into existence as necessary if they are
undefined, but we didn't say what happens if a value used as a
reference is already defined, but <em>isn't</em> a hard reference.  If you
use it as a reference, it'll be treated as a symbolic
reference.  That is, the value of the scalar is taken to be the <em>name</em>
of a variable, rather than a direct link to a (possibly) anonymous
value.</p>
<p>People frequently expect it to work like this.  So it does.</p>
<pre>
    <span class="variable">$name</span> <span class="operator">=</span> <span class="string">"foo"</span><span class="operator">;</span>
    <span class="variable">$$name</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>                 <span class="comment"># Sets $foo</span>
    <span class="variable">$</span><span class="operator">{</span><span class="variable">$name</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">2</span><span class="operator">;</span>               <span class="comment"># Sets $foo</span>
    <span class="variable">$</span><span class="operator">{</span><span class="variable">$name</span> <span class="operator">x</span> <span class="number">2</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">3</span><span class="operator">;</span>           <span class="comment"># Sets $foofoo</span>
    <span class="variable">$name</span><span class="operator">-&gt;</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span> <span class="operator">=</span> <span class="number">4</span><span class="operator">;</span>             <span class="comment"># Sets $foo[0]</span>
    <span class="variable">@$name</span> <span class="operator">=</span> <span class="operator">();</span>                <span class="comment"># Clears @foo</span>
    <span class="operator">&amp;</span><span class="variable">$name</span><span class="operator">();</span>                   <span class="comment"># Calls &amp;foo() (as in Perl 4)</span>
    <span class="variable">$pack</span> <span class="operator">=</span> <span class="string">"THAT"</span><span class="operator">;</span>
    <span class="variable">$</span><span class="operator">{</span><span class="string">"${pack}::$name"</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">5</span><span class="operator">;</span>    <span class="comment"># Sets $THAT::foo without eval</span>
</pre>
<p>This is powerful, and slightly dangerous, in that it's possible
to intend (with the utmost sincerity) to use a hard reference, and
accidentally use a symbolic reference instead.  To protect against
that, you can say</p>
<pre>
    <span class="keyword">use</span> <span class="variable">strict</span> <span class="string">'refs'</span><span class="operator">;</span>
</pre>
<p>and then only hard references will be allowed for the rest of the enclosing
block.  An inner block may countermand that with</p>
<pre>
    <span class="keyword">no</span> <span class="variable">strict</span> <span class="string">'refs'</span><span class="operator">;</span>
</pre>
<p>Only package variables (globals, even if localized) are visible to
symbolic references.  Lexical variables (declared with <a href="../../lib/Pod/perlfunc.html#item_my"><code>my())</code></a> aren't in
a symbol table, and thus are invisible to this mechanism.  For example:</p>
<pre>
    <span class="keyword">local</span> <span class="variable">$value</span> <span class="operator">=</span> <span class="number">10</span><span class="operator">;</span>
    <span class="variable">$ref</span> <span class="operator">=</span> <span class="string">"value"</span><span class="operator">;</span>
    <span class="operator">{</span>
        <span class="keyword">my</span> <span class="variable">$value</span> <span class="operator">=</span> <span class="number">20</span><span class="operator">;</span>
        <span class="keyword">print</span> <span class="variable">$$ref</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>This will still print 10, not 20.  Remember that <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> affects package
variables, which are all &quot;global&quot; to the package.</p>
<p>
</p>
<h2><a name="notsosymbolic_references">Not-so-symbolic references</a></h2>
<p>A new feature contributing to readability in perl version 5.001 is that the
brackets around a symbolic reference behave more like quotes, just as they
always have within a string.  That is,</p>
<pre>
    <span class="variable">$push</span> <span class="operator">=</span> <span class="string">"pop on "</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="string">"${push}over"</span><span class="operator">;</span>
</pre>
<p>has always meant to print &quot;pop on over&quot;, even though push is
a reserved word.  This has been generalized to work the same outside
of quotes, so that</p>
<pre>
    <span class="keyword">print</span> <span class="variable">$</span><span class="operator">{</span><span class="string">push</span><span class="operator">}</span> <span class="operator">.</span> <span class="string">"over"</span><span class="operator">;</span>
</pre>
<p>and even</p>
<pre>
    <span class="keyword">print</span> <span class="variable">$</span><span class="operator">{</span> <span class="string">push</span> <span class="operator">}</span> <span class="operator">.</span> <span class="string">"over"</span><span class="operator">;</span>
</pre>
<p>will have the same effect.  (This would have been a syntax error in
Perl 5.000, though Perl 4 allowed it in the spaceless form.)  This
construct is <em>not</em> considered to be a symbolic reference when you're
using strict refs:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">strict</span> <span class="string">'refs'</span><span class="operator">;</span>
    <span class="variable">$</span><span class="operator">{</span> <span class="string">bareword</span> <span class="operator">}</span><span class="operator">;</span>      <span class="comment"># Okay, means $bareword.</span>
    <span class="variable">$</span><span class="operator">{</span> <span class="string">"bareword"</span> <span class="operator">}</span><span class="operator">;</span>    <span class="comment"># Error, symbolic reference.</span>
</pre>
<p>Similarly, because of all the subscripting that is done using single
words, we've applied the same rule to any bareword that is used for
subscripting a hash.  So now, instead of writing</p>
<pre>
    <span class="variable">$array</span><span class="operator">{</span> <span class="string">"aaa"</span> <span class="operator">}{</span> <span class="string">"bbb"</span> <span class="operator">}{</span> <span class="string">"ccc"</span> <span class="operator">}</span>
</pre>
<p>you can write just</p>
<pre>
    <span class="variable">$array</span><span class="operator">{</span> <span class="string">aaa</span> <span class="operator">}{</span> <span class="string">bbb</span> <span class="operator">}{</span> <span class="string">ccc</span> <span class="operator">}</span>
</pre>
<p>and not worry about whether the subscripts are reserved words.  In the
rare event that you do wish to do something like</p>
<pre>
    <span class="variable">$array</span><span class="operator">{</span> <span class="string">shift</span> <span class="operator">}</span>
</pre>
<p>you can force interpretation as a reserved word by adding anything that
makes it more than a bareword:</p>
<pre>
    <span class="variable">$array</span><span class="operator">{</span> <span class="keyword">shift</span><span class="operator">()</span> <span class="operator">}</span>
    <span class="variable">$array</span><span class="operator">{</span> <span class="operator">+</span><span class="keyword">shift</span> <span class="operator">}</span>
    <span class="variable">$array</span><span class="operator">{</span> <span class="keyword">shift</span> <span class="variable">@_</span> <span class="operator">}</span>
</pre>
<p>The <code>use warnings</code> pragma or the <strong>-w</strong> switch will warn you if it
interprets a reserved word as a string.
But it will no longer warn you about using lowercase words, because the
string is effectively quoted.</p>
<p>
</p>
<h2><a name="pseudohashes__using_an_array_as_a_hash">Pseudo-hashes: Using an array as a hash</a></h2>
<p><strong>WARNING</strong>:  This section describes an experimental feature.  Details may
change without notice in future versions.</p>
<p><strong>NOTE</strong>: The current user-visible implementation of pseudo-hashes
(the weird use of the first array element) is deprecated starting from
Perl 5.8.0 and will be removed in Perl 5.10.0, and the feature will be
implemented differently.  Not only is the current interface rather ugly,
but the current implementation slows down normal array and hash use quite
noticeably.  The 'fields' pragma interface will remain available.</p>
<p>Beginning with release 5.005 of Perl, you may use an array reference
in some contexts that would normally require a hash reference.  This
allows you to access array elements using symbolic names, as if they
were fields in a structure.</p>
<p>For this to work, the array must contain extra information.  The first
element of the array has to be a hash reference that maps field names
to array indices.  Here is an example:</p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?