📄 ch04_09.htm
字号:
<html><head><title>Notes on Lexical (my) Variables (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_08.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch04_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">4.9. Notes on Lexical (my) Variables</h2><p>Those <a name="INDEX-354" />lexical variables can actually be usedin any block, not merely in a subroutine's block. For example,they can be used in the block of an <tt class="literal">if</tt>,<tt class="literal">while</tt>, or <tt class="literal">foreach</tt>:</p><blockquote><pre class="code">foreach (1..10) { my($square) = $_ * $_; # private variable in this loop print "$_ squared is $square.\n";}</pre></blockquote><p>The variable <tt class="literal">$square</tt> is private to the enclosingblock; in this case, that's the block of the<tt class="literal">foreach</tt> loop. If there's no block, thevariable is private to the entire source file. For now, your programsaren't going to use more than one source file, so thisisn't an issue. But the important concept is that the<em class="firstterm">scope</em><a name="INDEX-355" /> of a lexical variable's name islimited to the smallest enclosing block or file. The<em class="emphasis">only</em> code that can say<tt class="literal">$square</tt> and mean that variable is the code insidethat textual scope. This is a big win for maintainability -- ifthe wrong value is found in <tt class="literal">$square</tt>, the culpritwill be found within a limited amount of source code. As experiencedprogrammers have learned (often the hard way), limiting the scope ofa variable to a page of code, or even to a few lines of code, reallyspeeds along the development and testing cycle.</p><p>Note also that the <tt class="literal">my</tt><a name="INDEX-356" /> operator doesn't change thecontext of an assignment:</p><blockquote><pre class="code">my($num) = @_; # list context, same as ($num) = @_;my $num = @_; # scalar context, same as $num = @_;</pre></blockquote><p>In the first one, <tt class="literal">$num</tt> gets the first parameter,as a list-context assignment; in the second, it gets the number ofparameters, in a scalar context. Either line of code could be whatthe programmer wanted; we can't tell from that one line alone,and so Perl can't warn you if you use the wrong one. (Ofcourse, you wouldn't have <em class="emphasis">both</em> of thoselines in the same subroutine, since you can't have two lexicalvariables with the same name declared in the same scope; this is justan example.) So, when reading code like this, you can always tell thecontext of the assignment by seeing what the context would be withoutthe word <tt class="literal">my</tt>.</p><p>Of course, you can use <tt class="literal">my</tt> to create new, privatearrays as well:<a href="#FOOTNOTE-114">[114]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-114" /><p>[114]Or hashes, which we'll see inthe next chapter.</p> </blockquote><blockquote><pre class="code">my @phone_number;</pre></blockquote><p>Any new variable will start out empty -- <tt class="literal">undef</tt>for scalars, or the empty list for arrays.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_08.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch04_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">4.8. Variable-length Parameter Lists</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">4.10. The use strict Pragma</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -