📄 ch08_04.htm
字号:
<html><head><title>Symbolic References (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Symbolic References"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch08_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch08_01.htm">Chapter 8: References</a></td><td align="right" valign="top" width="172"><a href="ch08_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">8.4. Symbolic References</h2><p><a name="INDEX-2048"></a><a name="INDEX-2049"></a>What happens if you try to dereference a value that is not a hardreference? The value is then treated as a <em class="emphasis">symbolic reference</em>. That is, the reference isinterpreted as a string representing the <em class="emphasis">name</em> of a global variable.</p><p>Here is how this works:<blockquote><pre class="programlisting">$name = "bam";$$name = 1; # Sets $bam$name->[0] = 4; # Sets the first element of @bam$name->{X} = "Y"; # Sets the X element of %bam to Y@$name = (); # Clears @bamkeys %$name; # Yields the keys of %bam&$name; # Calls &bam</pre></blockquote><a name="INDEX-2050"></a></p><p>This is very powerful, and slightly dangerous, in that it's possible tointend (with the utmost sincerity) to use a hard reference, but toaccidentally use a symbolic reference instead. To protect against that,you can say:<blockquote><pre class="programlisting">use strict 'refs';</pre></blockquote>and then only hard references will be allowed for the rest of theenclosing block. An inner block may countermand the decree with:<blockquote><pre class="programlisting">no strict 'refs';</pre></blockquote>It is also important to understand the difference between thefollowing two lines of code:<blockquote><pre class="programlisting">${identifier}; # Same as $identifier.${"identifier"}; # Also $identifier, but a symbolic reference.</pre></blockquote>Because the second form is quoted, it is treated as a symbolicreference and will generate an error if <tt class="literal">use strict'refs'</tt> is in effect. Even if <tt class="literal">strict'refs'</tt> is not in effect, it can only refer to a packagevariable. But the first form is identical to the unbracketed form,and will refer to even a lexically scoped variable if one is declared.The next example shows this (and the next section discusses it).</p><p><a name="INDEX-2051"></a>Only package variables are accessible through symbolic references,because symbolic references always go through the package symboltable. Since lexical variables aren't in a package symbol table, theyare therefore invisible to this mechanism. For example:<blockquote><pre class="programlisting">our $value = "global";{ my $value = "private"; print "Inside, mine is ${value}, "; print "but ours is ${'value'}.\n";}print "Outside, ${value} is again ${'value'}.\n";</pre></blockquote>which prints:<blockquote><pre class="programlisting">Inside, mine is private, but ours is global.Outside, global is again global.</pre></blockquote></p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch08_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch08_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">8.3. Using Hard References</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">8.5. Braces, Brackets, and Quoting</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -