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

📄 ch10_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>More Control Structures (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 &amp; 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="ch09_09.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div>




<h1 class="chapter">Chapter 10. More Control Structures</h1>
<div class="htmltoc"><h4 class="tochead">Contents:</h4>
  <p> <a href="#lperl3-CHP-10-SECT-1">The unless Control Structure</a><br />
<a href="ch10_02.htm">The until Control Structure</a><br />
<a href="ch10_03.htm">Expression Modifiers</a><br />
<a href="ch10_04.htm">The Naked Block Control Structure</a><br />
<a href="ch10_05.htm">The elsif Clause</a><br />
<a href="ch10_06.htm">Autoincrement and Autodecrement</a><br />
<a href="ch10_07.htm">The for Control Structure</a><br />
<a href="ch10_08.htm">Loop Controls</a><br />
<a href="ch10_09.htm">Logical Operators</a><br />
<a href="ch10_10.htm">Exercise</a><br /></p></div>

<p><a name="INDEX-637" /></a>In this chapter, we'll see some
alternative ways to write Perl code. For the most part, these
techniques don't make the language more powerful, but they make
it easier or more convenient to get the job done. You don't
have to use these techniques in your own code, but don't be
tempted to skip this chapter -- you're certain to see these
control structures in other people's code, sooner or later (in
fact, you're absolutely certain to see these things in use by
the time you finish reading this book).
</p>

<div class="sect1"><a name="lperl3-CHP-10-SECT-1" /></a>
<h2 class="sect1">10.1. The unless Control Structure</h2>

<p>In an <tt class="literal">if</tt> control structure, the block of code is
executed only when the conditional expression is true. If you want a
block of code to be executed only when the conditional is false,
change <tt class="literal">if</tt> to
<tt class="literal">unless</tt><a name="INDEX-638" /></a> <a name="INDEX-639" /></a>:
</p>

<blockquote><pre class="code">unless ($fred =~ /^[A-Z_]\w*$/i) {
  print "The value of \$fred doesn't look like a Perl identifier name.\n";
}</pre></blockquote>

<p>Using <tt class="literal">unless</tt> says to run the block of code
<em class="emphasis">unless</em> this condition is true. It's just
like using an <tt class="literal">if</tt> test with the opposite condition.
Another way to say it is that it's like having the
<tt class="literal">else</tt> clause on its own. That is, whenever you see
an <tt class="literal">unless</tt> that you don't understand, you can
rewrite it (either in your head or in reality) to be an
<tt class="literal">if</tt> test:
</p>

<blockquote><pre class="code">if ($fred =~ /^[A-Z_]\w*$/i) {
  # Do nothing
} else {
  print "The value of \$fred doesn't look like a Perl identifier name.\n";
}</pre></blockquote>

<p>It's no more or less efficient, and it should compile to the
same internal byte codes. Or, another way to rewrite it would be to
negate the conditional expression by using the
<a name="INDEX-640" /></a>negation operator (<tt class="literal">!</tt>):
</p>

<blockquote><pre class="code">if ( ! ($fred =~ /^[A-Z_]\w*$/i) ) {
  print "The value of \$fred doesn't look like a Perl identifier name.\n";
}</pre></blockquote>

<p>Generally, you should pick the way of writing code that makes the
most sense to you, since that will probably make the most sense to
your maintenance programmer. If it makes the most sense to write
<tt class="literal">if</tt> with a negation, do that. More often, however,
you'll probably find it natural to use
<tt class="literal">unless</tt>.
</p>

<a name="lperl3-CHP-10-SECT-1.1" /></a><div class="sect2">
<h3 class="sect2">10.1.1. The else Clause with unless</h3>

<p>You could even have an
<tt class="literal">else</tt><a name="INDEX-641" /></a> clause with an
<tt class="literal">unless</tt>. While this syntax is supported, it's
potentially confusing:
</p>

<blockquote><pre class="code">unless ($mon =~ /^(Feb)/) {
  print "This month has at least thirty days.\n";
} else {
  print "Do you see what's going on here?\n";
}</pre></blockquote>

<p>Some people may wish to use this, especially when the first clause is
very short (perhaps only one line) and the second is several lines of
code. But we'd make this one a negated <tt class="literal">if</tt>,
or maybe simply swap the clauses to make a normal
<tt class="literal">if</tt><a name="INDEX-642" /></a>:
</p>

<blockquote><pre class="code">if ($mon =~ /^(Feb)/) {
  print "Do you see what's going on here?\n";
} else {
  print "This month has at least thirty days.\n";
}</pre></blockquote>

<p>It's important to remember that you're always writing
code for two readers: the computer that will run the code and the
<a name="INDEX-643" /></a>
<a name="INDEX-644" /></a>human being
who has to keep the code working. If the human can't understand
what you've written, pretty soon the computer won't be
doing the right thing either.
</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="ch09_09.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_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">9.9. Exercises</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.2. The until Control Structure</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 &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 + -