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

📄 ch04_02.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Compound 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 &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Compound 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_01.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_03.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.2. Compound Statements</h2><a name="INDEX-1051"></a><a name="INDEX-1052"></a><a name="INDEX-1053"></a><a name="INDEX-1054"></a><a name="INDEX-1055"></a><a name="INDEX-1056"></a><a name="INDEX-1057"></a><p>A sequence of statements within a scope<a href="#FOOTNOTE-1">[1]</a> is called a <em class="emphasis">block</em>.  Sometimes the scope isthe entire file, such as a <tt class="literal">require</tt>d file or the file containingyour main program.  Sometimes the scope is a string being evaluatedwith <tt class="literal">eval</tt>.  But generally, a block is surrounded by braces(<tt class="literal">{}</tt>).  When we say scope, we mean any of these three.  When wemean a block with braces, we'll use the term <em class="replaceable">BLOCK</em>.</p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1]Scopes and namespacesare described in <a href="ch02_01.htm">Chapter 2, "Bits and Pieces"</a>, in <a href="ch02_05.htm#ch02-sect-names">Section 4.5, "Names"</a>.</p></blockquote><p><a name="INDEX-1058"></a><a name="INDEX-1059"></a><a name="INDEX-1060"></a><a name="INDEX-1061"></a><a name="INDEX-1062"></a><a name="INDEX-1063"></a>Compound statements are built out of expressions and <em class="replaceable">BLOCK</em>s.Expressions are built out of terms and operators.  In our syntax descriptions, we'll use the word <em class="replaceable">EXPR</em> toindicate a place where you can use any scalar expression. To indicate an expression evaluated in list context, we'll say <em class="replaceable">LIST</em>.</p><p>The following statements may be used to control conditional andrepeated execution of <em class="replaceable">BLOCK</em>s.  (The <em class="replaceable">LABEL</em> portion is optional.)<blockquote><pre class="programlisting">if (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em>if (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> else <em class="replaceable">BLOCK</em>if (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> elsif (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> ...if (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> elsif (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> ... else <em class="replaceable">BLOCK</em>unless (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em>unless (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> else <em class="replaceable">BLOCK</em>unless (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> elsif (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> ...unless (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> elsif (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> ... else <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> while (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> while (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> continue <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> until (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> until (<em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em> continue <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> for (<em class="replaceable">EXPR</em>; <em class="replaceable">EXPR</em>; <em class="replaceable">EXPR</em>) <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> foreach (<em class="replaceable">LIST</em>) <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> foreach <em class="replaceable">VAR</em> (<em class="replaceable">LIST</em>) <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> foreach <em class="replaceable">VAR</em> (<em class="replaceable">LIST</em>) <em class="replaceable">BLOCK</em> continue <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> <em class="replaceable">BLOCK</em><em class="replaceable">LABEL</em> <em class="replaceable">BLOCK</em> continue <em class="replaceable">BLOCK</em></pre></blockquote><a name="INDEX-1064"></a>Note that unlike in C and Java, these are defined in terms of<em class="replaceable">BLOCK</em>s, not statements.  This means that the braces are required--nodangling statements allowed.  If you want to write conditionalswithout braces there are several ways to do so.  The followingall do the same thing:<blockquote><pre class="programlisting">unless (open(FOO, $foo))    { die "Can't open $foo: $!" }if (!open(FOO, $foo))       { die "Can't open $foo: $!" }die "Can't open $foo: $!"   unless open(FOO, $foo);die "Can't open $foo: $!"   if !open(FOO, $foo);open(FOO, $foo)             || die "Can't open $foo: $!";open FOO, $foo              or die "Can't open $foo: $!";</pre></blockquote>Under most circumstances, we tend to prefer the last pair.  These formscome with less eye-clutter than the others, especially the"<tt class="literal">or die</tt>" version.  With the <tt class="literal">||</tt> form you need to get used tousing parentheses religiously, but with the <tt class="literal">or</tt> version, it doesn'tmatter if you forget.</p><p>But the main reason we like the last versions better is because ofhow they pull the important part of the statement right up to thefront of the line where you'll see it first.  The error handlingis shoved off to the side so that you don't have to pay attentionto it unless you want to.<a href="#FOOTNOTE-2">[2]</a>If you tab all your "<tt class="literal">or die</tt>" checks over to the same column on theright each time, it's even easier to read:<blockquote><pre class="programlisting">chdir $dir                  or die "chdir $dir: $!";open FOO, $file             or die "open $file: $!";@lines = &lt;FOO&gt;              or die "$file is empty?";close FOO                   or die "close $file: $!";</pre></blockquote></p><blockquote class="footnote"><a name="FOOTNOTE-2"></a><p>[2] (Like this footnote.)</p></blockquote><!-- 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_01.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_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">4.1. Simple 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.3. if and unless 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 &copy; 2001</a> O'Reilly &amp; 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 + -