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

📄 ch10_03.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Expression Modifiers (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="ch10_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">10.3. Expression Modifiers</h2><p>In order to have a more compact notation, an<a name="INDEX-647" />expression may be followed by a modifierthat controls it. For example, the<tt class="literal">if</tt><a name="INDEX-648" /> modifier works in a way analogous toan <tt class="literal">if</tt> block:</p><blockquote><pre class="code">print "$n is a negative number.\n" if $n &lt; 0;</pre></blockquote><p>That gives exactly the same result as if we had used this code,except that we saved some typing by leaving out the parentheses andcurly braces:<a href="#FOOTNOTE-216">[216]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-216" /><p>[216]We also left out the line breaks. Butwe should mention that the curly-brace form does create a new scope.In the rare case that you need the full details, check thedocumentation.</p> </blockquote><blockquote><pre class="code">if ($n &lt; 0) {  print "$n is a negative number.\n";}</pre></blockquote><p>As we've said, Perl folks generally like to avoid typing. Andthe shorter form reads like in English: print this message, if<tt class="literal">$n</tt> is less than zero.</p><p>Notice that the conditional expression is still evaluated first, eventhough it's written at the end. This is backwards from theusual left-to-right ordering; in understanding Perl<a name="INDEX-649" />code,we'll have to do as Perl's internal compiler does, andread to the end of the statement before we can tell what it'sreally doing.</p><p>There are other <a name="INDEX-650" /> <a name="INDEX-651" /> <a name="INDEX-652" /> <a name="INDEX-653" />modifiers as well:</p><blockquote><pre class="code">&amp;error("Invalid input") unless &amp;valid($input);$i *= 2 until $i &gt; $j;print " ", ($n += 2) while $n &lt; 10;&amp;greet($_) foreach @person;</pre></blockquote><p>These all work just as (we hope) you would expect. That is, each onecould be rewritten in a similar way to rewriting the<tt class="literal">if</tt>-modifier example earlier. Here is one:</p><blockquote><pre class="code">while ($n &lt; 10) {  print " ", ($n += 2);}</pre></blockquote><p>The expression in parentheses inside the <tt class="literal">print</tt>argument list is noteworthy because it adds two to<tt class="literal">$n</tt>, storing the result back into<tt class="literal">$n</tt>. Then it returns that new value, which will beprinted.</p><p>These shorter forms read almost like a natural language: call the<tt class="literal">&amp;greet</tt> subroutine for each<tt class="literal">@person</tt> in the list. Double <tt class="literal">$i</tt>until it's larger than <tt class="literal">$j</tt>.<a href="#FOOTNOTE-217">[217]</a></p><blockquote class="footnote"><a name="FOOTNOTE-217" /><p>[217]Well, it helps <em class="emphasis">us</em> to think of them likethat.</p> </blockquote><p>One of the common uses of these modifiers is in a statement like thisone:</p><blockquote><pre class="code">print "fred is '$fred', barney is '$barney'\n"           if $I_am_curious;</pre></blockquote><p>By writing the code "in reverse" like this, you can putthe important part of the statement at the beginning. The point ofthat statement is to monitor some variables; the point is not tocheck whether you're curious.<a href="#FOOTNOTE-218">[218]</a> Some people prefer to write the whole statement on oneline, perhaps with some tab characters before the<tt class="literal">if</tt>, to move it over toward the right margin, as weshowed in the previous example, while others put the<tt class="literal">if</tt> modifier indented on a new line:</p><blockquote class="footnote"> <a name="FOOTNOTE-218" /><p>[218]Of course, wemade up the name <tt class="literal">$I_am_curious</tt>; it's not abuiltin Perl variable. Generally, folks who use this technique willeither call their variable <tt class="literal">$TRACING</tt>, or will use aconstant declared with the <tt class="literal">constant</tt> pragma.</p></blockquote><blockquote><pre class="code">print "fred is '$fred', barney is '$barney'\n"    if $I_am_curious;</pre></blockquote><p>Although you can rewrite any of these expressions with modifiers as ablock (the "old-fashioned" way), the converse isn'tnecessarily true. Only a single expression is allowed on either sideof the modifier. So you can't write something<tt class="literal">if</tt> something<tt class="literal">while</tt><a name="INDEX-654" /> <a name="INDEX-655" /> <a name="INDEX-656" /> <a name="INDEX-657" /> something <tt class="literal">until</tt>something <tt class="literal">unless</tt> something<tt class="literal">foreach</tt> something, which would just be tooconfusing. And you can't put multiple statements on the left ofthe modifier. If you need more than just a simple expression on eachside, just write the code the old-fashioned way, with the parenthesesand curly braces.</p><p>As we mentioned in relation to the <tt class="literal">if</tt> modifier,the control expression (on the right) is always evaluated first, justas it would be in the old-fashioned form.</p><p>With the <tt class="literal">foreach</tt> modifier, there's no way tochoose a different control variable -- it's always<tt class="literal">$_</tt>. Usually, that's no problem, but if youwant to use a different variable, you'll need to rewrite it asa traditional <tt class="literal">foreach</tt> loop.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch10_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">10.2. The until 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.4. The Naked Block 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 + -