📄 ch04_03.htm
字号:
<html><head><title>if and unless Statements (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="if and unless Statements"><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="ch04_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch04_01.htm">Chapter 4: Statements and Declarations</a></td><td align="right" valign="top" width="172"><a href="ch04_04.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">4.3. if and unless Statements</h2><a name="INDEX-1065"></a><a name="INDEX-1066"></a><a name="INDEX-1067"></a><a name="INDEX-1068"></a><p>The <tt class="literal">if</tt> statement is straightforward. Because <em class="replaceable">BLOCK</em>s are alwaysbounded by braces, there is never any ambiguity regarding whichparticular <tt class="literal">if</tt> an <tt class="literal">else</tt> or <tt class="literal">elsif</tt> goes with. In any givensequence of <tt class="literal">if</tt>/<tt class="literal">elsif</tt>/<tt class="literal">else</tt><em class="replaceable">BLOCK</em>s, only the first onewhose condition evaluates to true is executed. If none of them istrue, then the <tt class="literal">else</tt><em class="replaceable">BLOCK</em>, if there is one, is executed. It'susually a good idea to put an <tt class="literal">else</tt> at the end of a chain of<tt class="literal">elsif</tt>s to guard against a missed case.</p><p><a name="INDEX-1069"></a>If you use <tt class="literal">unless</tt> in place of <tt class="literal">if</tt>, the sense of its testis reversed. That is:<blockquote><pre class="programlisting">unless ($x == 1) ...</pre></blockquote>is equivalent to:<blockquote><pre class="programlisting">if ($x != 1) ...</pre></blockquote>or even to the unsightly:<blockquote><pre class="programlisting">if (!($x == 1)) ...</pre></blockquote><a name="INDEX-1070"></a></p><p>The scope of a variable declared in the controlling condition extendsfrom its declaration through the rest of that conditional only,including any <tt class="literal">elsif</tt>s and the final <tt class="literal">else</tt> clause if present,but not beyond:<blockquote><pre class="programlisting">if ((my $color = <STDIN>) =~ /red/i) { $value = 0xff0000;}elsif ($color =~ /green/i) { $value = 0x00ff00;}elsif ($color =~ /blue/i) { $value = 0x0000ff;}else { warn "unknown RGB component `$color', using black instead\n"; $value = 0x000000;}</pre></blockquote>After the <tt class="literal">else</tt>, the <tt class="literal">$color</tt> variable is no longer in scope. Ifyou want the scope to extend further, declare the variable beforehand.</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="ch04_02.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="ch04_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">4.2. Compound Statements</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">4.4. Loop Statements</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 + -