📄 ch10_04.htm
字号:
<html><head><title>The Naked Block Control Structure (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="ch10_03.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="ch10_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">10.4. The Naked Block Control Structure</h2><p>The so-called "<a name="INDEX-658" />naked" block is one without akeyword or condition. That is, suppose you start with a<tt class="literal">while</tt><a name="INDEX-659" /> loop, which looks something like this:</p><blockquote><pre class="code">while (condition) { <em class="replaceable">body;</em> <em class="replaceable">body;</em> <em class="replaceable">body;</em>}</pre></blockquote><p>Now, take away the <tt class="literal">while</tt> keyword and theconditional expression, and you'll have a naked block:</p><blockquote><pre class="code">{ <em class="replaceable">body;</em> <em class="replaceable">body;</em> <em class="replaceable">body;</em>}</pre></blockquote><p>The naked block is like a <tt class="literal">while</tt> or<tt class="literal">foreach</tt><a name="INDEX-660" /> loop, except that it doesn't loop;it just executes the body of the loop once, and it's done.It's an un-loop!</p><p>We'll see in a while that there are other uses for the nakedblock, but one of its features is that it provides a scope fortemporary lexical variables:</p><blockquote><pre class="code">{ print "Please enter a number: "; chomp(my $n = <STDIN>); my $root = sqrt $n; # calculate the square root print "The square root of $n is $root.\n";}</pre></blockquote><p>In this block, <tt class="literal">$n</tt> and <tt class="literal">$root</tt> aretemporary variables scoped to the block. As a general guideline, allvariables should be declared in the smallest<a name="INDEX-661" />scope available.If you need a variable for just a few lines of code, you can putthose lines into a naked block and declare the variable inside thatblock. Of course, if we would need the value of either<tt class="literal">$n</tt> or <tt class="literal">$root</tt> later, we wouldneed to declare them in a larger scope.</p><p>You may have noticed the<tt class="literal">sqrt</tt><a name="INDEX-662" /> function in that code and wonderedabout it -- yes, it's a function we haven't shownbefore. Perl has many builtin functions that are beyond the scope ofthis book. When you're ready, check the<em class="emphasis">perlfunc</em><a name="INDEX-663" /> manpage to learn about more of them.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch10_03.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="ch10_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">10.3. Expression Modifiers</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">10.5. The elsif Clause</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 + -