📄 ch02_07.htm
字号:
<html><head><title>The if 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="ch02_06.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="ch02_08.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.7. The if Control Structure</h2><p>Once you can compare two values, you'll probably want yourprogram to make decisions based upon that comparison. Like allsimilar languages, Perl has an<a name="INDEX-229" /> <tt class="literal">if</tt> controlstructure:</p><blockquote><pre class="code">if ($name gt 'fred') { print "'$name' comes after 'fred' in sorted order.\n";}</pre></blockquote><p>If you need an alternative choice, the<tt class="literal">else</tt><a name="INDEX-230" /> keyword provides that as well:</p><blockquote><pre class="code">if ($name gt 'fred') { print "'$name' comes after 'fred' in sorted order.\n";} else { print "'$name' does not come after 'fred'.\n"; print "Maybe it's the same string, in fact.\n";}</pre></blockquote><p>Unlike in C, those block <a name="INDEX-231" /><a name="INDEX-232" /> <a name="INDEX-233" />curlybraces are required around the conditional code. It's a goodidea to indent the contents of the blocks of code as we show here;that makes it easier to see what's going on. If you'reusing a programmers' text editor (as discussed in <a href="ch01_01.htm">Chapter 1, "Introduction"</a>), it'll do most of the work for you.</p><a name="lperl3-CHP-2-SECT-7.1" /><div class="sect2"><h3 class="sect2">2.7.1. Boolean Values</h3><p>You may actually use any scalar value as the conditional of the<tt class="literal">if</tt> control structure. That's handy if youwant to store a true or false value into a variable, like this:</p><blockquote><pre class="code">$is_bigger = $name gt 'fred';if ($is_bigger) { ... }</pre></blockquote><p>But how does Perl decide whether a given value is true or false? Perldoesn't have a separate <a name="INDEX-234" />Boolean data type, like some languageshave. Instead, it uses a few simple rules:</p><ol><li><p>The special value <tt class="literal">undef</tt> is false. (We'll seethis a little later in this section.)</p></li><li><p>Zero is false; all other numbers are true.</p></li><li><p>The empty string (<tt class="literal">''</tt>) is false; all other stringsare normally true.</p></li><li><p>The one exception: since numbers and strings are equivalent, thestring form of zero, <tt class="literal">'0'</tt>, has the same value asits numeric form: false.</p></li></ol><p>So, if your scalar value is <tt class="literal">undef</tt>,<tt class="literal">0</tt>, <tt class="literal">''</tt>, or<tt class="literal">'0'</tt>, it's false. All other scalars aretrue -- including all of the types of scalars that wehaven't told you about yet.</p><p>If you need to get the opposite of any Boolean value, use the unary<em class="emphasis">not</em><a name="INDEX-235" /> <a name="INDEX-236" /><a name="INDEX-237" /> operator, <tt class="literal">!</tt>. If whatfollows it is a true value, it returns false; if what follows isfalse, it returns true:</p><blockquote><pre class="code">if (! $is_bigger) { # Do something when $is_bigger is not true}</pre></blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_06.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="ch02_08.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.6. Output with print </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">2.8. Getting User Input</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 + -