📄 ch10_05.htm
字号:
<html><head><title>The elsif Clause (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_04.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_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">10.5. The elsif Clause</h2><p>Every so often, you may need to check a number of conditionalexpressions, one after another, to see which one of them is true.This can be done with the <tt class="literal">if</tt> controlstructure's <tt class="literal">elsif</tt><a name="INDEX-664" /> clause, as in thisexample:</p><blockquote><pre class="code">if ( ! defined $dino) { print "The value is undef.\n";} elsif ($dino =~ /^-?\d+\.?$/) { print "The value is an integer.\n";} elsif ($dino =~ /^-?\d*\.\d+$/) { print "The value is a _simple_ floating-point number.\n";} elsif ($dino eq '') { print "The value is the empty string.\n";} else { print "The value is the string '$dino'.\n";}</pre></blockquote><p>Perl will test the conditional expressions one after another. Whenone succeeds, the corresponding block of code is executed, and thenthe whole control structure is done,<a href="#FOOTNOTE-219">[219]</a> and execution goes on to the rest of the program. If nonehas succeeded, the <tt class="literal">else</tt> block at the end isexecuted. (Of course, the <tt class="literal">else</tt> clause is stilloptional, although in this case it's often a good idea toinclude it.)</p><blockquote class="footnote"> <a name="FOOTNOTE-219" /><p>[219]There's no"fall-through" to the next block, as in the"switch" structure of languages like C.</p></blockquote><p>There's no limit to the number of <tt class="literal">elsif</tt>clauses, but remember that Perl has to evaluate the first ninety-ninetests before it can get to the hundredth. If you'll have morethan half a dozen <tt class="literal">elsif</tt>s, you should considerwhether there's a more efficient way to write it. The Perl FAQ(see the <tt class="literal">perlfaq</tt><a name="INDEX-665" /> manpage) has a number of suggestionsfor emulating the "case" or "switch"statements of other languages.</p><p>You may have noticed by this point that the keyword is spelled<tt class="literal">elsif</tt>, with only one <tt class="literal">e</tt>. If youwrite it as "elseif", with a second <tt class="literal">e</tt>,Perl will tell you that it is not the correct spelling. Why not?Because Larry says so.<a href="#FOOTNOTE-220">[220]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-220" /><p>[220]In fact, he resists anysuggestion that it even be permitted as a valid alternative spelling."If you want to spell it with a second <tt class="literal">e</tt>,it's simple. Step 1 -- Make up your own language. Step2 -- Make it popular." When you make your own programminglanguage, you can spell the keywords in any way you'd like. Wehope that you will decide that yours shouldn't be the first tohave an "elseunless".</p> </blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch10_04.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_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">10.4. The Naked Block Control Structure</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.6. Autoincrement and Autodecrement</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 + -