⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch04_03.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Statements (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><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_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch04_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">4.3. Statements</h2><a name="INDEX-267" /><a name="INDEX-268" /><p><a name="INDEX-269" />Asimple statement is an expression evaluated for its side effects.Every simple statement must end in a semicolon, unless it is thefinal statement in a block.</p><p><a name="INDEX-270" /><a name="INDEX-271" /><a name="INDEX-272" /><a name="INDEX-273" />A sequence of statements that defines ascope is called a <em class="emphasis">block</em>. Generally, a block isdelimited by braces, or <tt class="literal">{ }</tt>. Compound statementsare built out of expressions and blocks. A conditional expression isevaluated to determine whether a statement block will be executed.Compound statements are defined in terms of blocks, not statements,which means that braces are required.</p><p><a name="INDEX-274" /><a name="INDEX-275" />Any block can be given a label.<em class="emphasis">Labels</em> are identifiers that follow thevariable-naming rules (i.e., they begin with a letter or underscoreand can contain alphanumerics and underscores). They are placed justbefore the block and are followed by a colon, such as<tt class="literal">SOMELABEL</tt> here:</p><blockquote><pre class="code">SOMELABEL: {  ...<em class="replaceable"><tt>statements</tt></em>...  }</pre></blockquote><p>By convention, labels are all uppercase, so as not to conflict withreserved words. Labels are used with the loop control commands<tt class="literal">next</tt>, <tt class="literal">last</tt>, and<tt class="literal">redo</tt> to alter the flow of execution in yourprograms.</p><a name="perlnut2-CHP-4-SECT-3.1" /><div class="sect2"><h3 class="sect2">4.3.1. Conditionals and Loops</h3><p><a name="INDEX-276" /><a name="INDEX-277" /><a name="INDEX-278" /><a name="INDEX-279" />The <tt class="literal">if</tt> and<tt class="literal">unless</tt> statements execute blocks of code dependingon whether a condition is met. These statements take the followingforms:</p><blockquote><pre class="code">if (<em class="replaceable"><tt>expression</em>) {<em class="replaceable">block</em>} else {<em class="replaceable">block</tt></em>}unless (<em class="replaceable"><tt>expression</em>) {<em class="replaceable">block</em>} else {<em class="replaceable">block</tt></em>}if (<em class="replaceable"><tt>expression1</em>) {<em class="replaceable">block</tt></em>}elsif (<em class="replaceable"><tt>expression2</em>) {<em class="replaceable">block</tt></em>}  ...elsif (<em class="replaceable"><tt>lastexpression</em>) {<em class="replaceable">block</tt></em>}else {<em class="replaceable"><tt>block</tt></em>}</pre></blockquote><a name="perlnut2-CHP-4-SECT-3.1.1" /><div class="sect3"><h3 class="sect3">4.3.1.1. while loops</h3><p><a name="INDEX-280" />The<tt class="literal">while</tt> statement repeatedly executes a block aslong as its conditional expression is true. For example:</p><blockquote><pre class="code">while (&lt;INFILE&gt;) {    chomp;    print OUTFILE, "$_\n";}</pre></blockquote><p>This loop reads each line from the file opened with the filehandleINFILE and prints them to the OUTFILE filehandle. The loop will ceasewhen it encounters an end-of-file.</p><p><a name="INDEX-281" />If the word<tt class="literal">while</tt> is replaced by the word<tt class="literal">until</tt>, the sense of the test is reversed. Theconditional is still tested before the first iteration, though.</p><p><a name="INDEX-282" />The<tt class="literal">while</tt> statement has an optional extra block on theend called a <tt class="literal">continue</tt> block. This block isexecuted before every successive iteration of the loop, even if themain <tt class="literal">while</tt> block is exited early by the loopcontrol command <tt class="literal">next</tt>. However, the<tt class="literal">continue</tt> block is not executed if the main blockis exited by a <tt class="literal">last</tt> statement. The<tt class="literal">continue</tt> block is always executed before theconditional is reevaluated.</p></div><a name="perlnut2-CHP-4-SECT-3.1.2" /><div class="sect3"><h3 class="sect3">4.3.1.2. for loops</h3><p><a name="INDEX-283" />The<tt class="literal">for</tt> loop has three semicolon-separated expressionswithin its parentheses. These three expressions function respectivelyas the initialization, the condition, and the reinitializationexpressions of the loop. The <tt class="literal">for</tt> loop can bedefined in terms of the corresponding <tt class="literal">while</tt> loop:</p><blockquote><pre class="code">for ($i = 1; $i &lt; 10; $i++) {    ...}</pre></blockquote><p>This is the same as: </p><blockquote><pre class="code">$i = 1;while ($i &lt; 10) {    ...}continue {    $i++;}</pre></blockquote></div><a name="perlnut2-CHP-4-SECT-3.1.3" /><div class="sect3"><h3 class="sect3">4.3.1.3. foreach loops</h3><p><a name="INDEX-284" />The<tt class="literal">foreach</tt> loop iterates over a list value and setsthe control variable (<em class="replaceable"><tt>var</tt></em>) to be eachelement of the list in turn:</p><blockquote><pre class="code">foreach <em class="replaceable"><tt>var</em> (<em class="replaceable">list</tt></em>) {    ...}</pre></blockquote><p>Like the <tt class="literal">while</tt> statement, the<tt class="literal">foreach</tt> statement can also take a<tt class="literal">continue</tt> block.</p></div><a name="perlnut2-CHP-4-SECT-3.1.4" /><div class="sect3"><h3 class="sect3">4.3.1.4. Modifiers</h3><p><a name="INDEX-285" /><a name="INDEX-286" /><a name="INDEX-287" /><a name="INDEX-288" /><a name="INDEX-289" /><a name="INDEX-290" />Any simple statement may be followed bya single modifier that gives the statement a conditional or loopingmechanism. This syntax provides a simpler and often more elegantmethod than using the corresponding compound statements. Thesemodifiers are:</p><blockquote><pre class="code"><em class="replaceable"><tt>statement</em> if <em class="replaceable">EXPR</tt></em>;<em class="replaceable"><tt>statement</em> unless <em class="replaceable">EXPR</tt></em>;<em class="replaceable"><tt>statement</em> while <em class="replaceable">EXPR</tt></em>;<em class="replaceable"><tt>statement</em> until <em class="replaceable">EXPR</tt></em>;</pre></blockquote><p>For example: </p><blockquote><pre class="code">$i = $num if ($num &lt; 50); # $i will be less than 50$j = $cnt unless ($cnt &lt; 100); # $j will equal 100 or greater$lines++ while &lt;FILE&gt;;print "$_\n" until /The end/;</pre></blockquote><p>The conditional is evaluated first with the <tt class="literal">while</tt>and <tt class="literal">until</tt> modifiers except when applied to<a name="INDEX-291" />a<tt class="literal">do {}</tt> statement, inwhich case the block executes once before the conditional isevaluated. For example:</p><blockquote><pre class="code">do {    $line = &lt;STDIN&gt;;    ...} until $line eq ".\n";</pre></blockquote><p>For more information on <tt class="literal">do</tt>, see <a href="ch05_01.htm">Chapter 5, "Function Reference"</a>. </p></div><a name="perlnut2-CHP-4-SECT-3.1.5" /><div class="sect3"><h3 class="sect3">4.3.1.5. Loop control</h3><p><a name="INDEX-292" /><a name="INDEX-293" />You can put a label on a loop to giveit a name. The loop's label identifies the loop forthe loop-control commands <tt class="literal">next</tt>,<tt class="literal">last</tt>, and <tt class="literal">redo</tt>:</p><blockquote><pre class="code">LINE: while (&lt;SCRIPT&gt;) {    print;    next LINE if /^#/;      # Discard comments    }</pre></blockquote><p>The syntax for the loop-control commands is: </p><blockquote><pre class="code">last <em class="replaceable"><tt>label</tt></em>next <em class="replaceable"><tt>label</tt></em>redo <em class="replaceable"><tt>label</tt></em></pre></blockquote><p>If the label is omitted, the loop-control command refers to theinnermost enclosing loop.</p><p><a name="INDEX-294" />The<tt class="literal">last</tt> command is like the <tt class="literal">break</tt>statement in C (as used in loops); it immediately exits the loop inquestion. The <tt class="literal">continue</tt> block, if any, is notexecuted.</p><p><a name="INDEX-295" />The<tt class="literal">next</tt> command is like the<tt class="literal">continue</tt> statement in C; it skips the rest of thecurrent iteration and starts the next iteration of the loop. If thereis a <tt class="literal">continue</tt> block on the loop, it is alwaysexecuted just before the conditional is reevaluated.</p><p><a name="INDEX-296" />The<tt class="literal">redo</tt> command restarts the loop block withoutevaluating the conditional again. The <tt class="literal">continue</tt>block, if any, is not executed.</p></div><a name="perlnut2-CHP-4-SECT-3.1.6" /><div class="sect3"><h3 class="sect3">4.3.1.6. goto</h3><p><a name="INDEX-297" />Perl supports a<tt class="literal">goto</tt> command. There are three forms:<tt class="literal">goto</tt> <em class="replaceable"><tt>label</tt></em>,<tt class="literal">goto</tt> <em class="replaceable"><tt>expr</tt></em>, and<tt class="literal">goto</tt><tt class="literal">&amp;</tt><em class="replaceable"><tt>name</tt></em>. In general,you shouldn't need to use <tt class="literal">goto</tt>,which you'll conclude if you do a search for<tt class="literal">goto</tt> in the<em class="emphasis">comp.lang.perl.misc</em> archives on<a href="http://groups.google.com">groups.google.com</a>.</p><p>The <tt class="literal">goto</tt> <em class="replaceable"><tt>label</tt></em> formfinds the statement labeled with <em class="replaceable"><tt>label</tt></em> andresumes execution there. It may not be used to go inside anyconstruct that requires initialization, such as a subroutine or a<tt class="literal">foreach</tt> loop.</p><p>The <tt class="literal">goto</tt> <em class="replaceable"><tt>expr</tt></em> formexpects the expression to return a label name.</p><p>The <tt class="literal">goto &amp;</tt><em class="replaceable"><tt>name</tt></em> formsubstitutes a call to the named subroutine for the currently runningsubroutine. <a name="INDEX-298" /> </p></div></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch04_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch04_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">4.2. Data Types and Variables</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">4.4. Special Variables</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; 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 + -