📄 ch04_06.htm
字号:
<html><head><title>Private Variables in Subroutines (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_05.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_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">4.6. Private Variables in Subroutines</h2><p>But if Perl can give us a new <tt class="literal">@_</tt> for everyinvocation, can't it give us variables for our own use as well?Of course it can.</p><p>By default, all variables in Perl are global variables; that is, theyare accessable from every part of the program. But you can createprivate variables called <em class="firstterm">lexicalvariables</em><a name="INDEX-337" /> <a name="INDEX-338" /> at anytime with the <tt class="literal">my</tt><a name="INDEX-339" /> operator:</p><blockquote><pre class="code">sub max { my($a, $b); # new, private variables for this block ($a, $b) = @_; # give names to the parameters if ($a > $b) { $a } else { $b }}</pre></blockquote><p>These variables are private (or<em class="firstterm">scoped</em><a name="INDEX-340" />) to theenclosing block; any other <tt class="literal">$a</tt> or<tt class="literal">$b</tt> is totally unaffected by these two. And thatgoes the other way, too -- no other code can access or modifythese private variables, by accident or design.<a href="#FOOTNOTE-108">[108]</a> So, we could drop this<a name="INDEX-341" />subroutine into any Perl program in theworld and know that we wouldn't mess up that program's<tt class="literal">$a</tt> and <tt class="literal">$b</tt> (if any).<a href="#FOOTNOTE-109">[109]</a></p><blockquote class="footnote"><a name="FOOTNOTE-108" /><p>[108]Advanced programmers will realize that a lexical variable maybe accessible by reference from outside its scope, but never byname.</p> </blockquote><blockquote class="footnote"><a name="FOOTNOTE-109" /><p>[109]Of course, if that program already had a subroutine called<tt class="literal">&max</tt>, we'd mess <em class="emphasis">that</em>up. </p> </blockquote><p>It's also worth pointing out that, inside the<tt class="literal">if</tt>'s blocks, there's no<a name="INDEX-342" />semicolon needed after thereturn value expression. Although Perl allows for the last semicolonin a block to be omitted, in practice that's omitted only whenthe code is so simple that the block is written in a single line,like the previous ones.</p><p>The subroutine in the previous example could be made even simpler.Did you notice that the list <tt class="literal">($a,</tt><tt class="literal">$b)</tt> was written twice? That <tt class="literal">my</tt>operator can also be applied to a list of variables enclosed inparentheses, so it's more customary to combine those first twostatements in the subroutine:</p><blockquote><pre class="code">my($a, $b) = @_; # Name the subroutine parameters</pre></blockquote><p>That one statement creates the private variables and sets theirvalues, so the first parameter now has the easier-to-use name<tt class="literal">$a</tt> and the second has <tt class="literal">$b</tt>.Nearly every subroutine will start with a line much like that one,naming its parameters. When you see that line, you'll know thatthe subroutine expects two scalar parameters, which we'll call<tt class="literal">$a</tt> and <tt class="literal">$b</tt> inside thesubroutine.</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_05.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_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">4.5. Arguments</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.7. The local Operator</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 + -