📄 ch04_01.htm
字号:
<html><head><title>Statements and Declarations (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 & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Statements and Declarations"><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="ch03_21.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part2.htm">Part 2: The Gory Details</a></td><td align="right" valign="top" width="172"><a href="ch04_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h1 class="chapter">Chapter 4. Statements and Declarations</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch04_01.htm">Simple Statements</a><br><a href="ch04_02.htm">Compound Statements</a><br><a href="ch04_03.htm">if and unless Statements</a><br><a href="ch04_04.htm">Loop Statements</a><br><a href="ch04_05.htm">Bare Blocks</a><br><a href="ch04_06.htm">goto</a><br><a href="ch04_07.htm">Global Declarations</a><br><a href="ch04_08.htm">Scoped Declarations</a><br><a href="ch04_09.htm">Pragmas</a><br></p></div><a name="INDEX-1030"></a><a name="INDEX-1031"></a><a name="INDEX-1032"></a><p>A Perl program consists of a sequence of declarations and statements.A declaration may be placed anywhere a statement may be placed, butits primary effect occurs at compile time. A few declarations dodouble duty as ordinary statements, but most are totally transparentat run time. After compilation, the main sequence of statements isexecuted just once.</p><p><a name="INDEX-1033"></a><a name="INDEX-1034"></a>Unlike many programming languages, Perl doesn't require variables tobe explicitly declared; they spring into existence upon their firstuse, whether you've declared them or not. If you try to use a valuefrom a variable that's never had a value assigned to it, it's quietlytreated as <tt class="literal">0</tt> when used as a number, as<tt class="literal">""</tt> (the null string) when used as a string, orsimply as false when used as a logical value. If you prefer to bewarned about using undefined values as though they were real stringsor numbers, or even to treat doing so as an error, the <tt class="literal">usewarnings</tt> declaration will take care of that; see the section <a href="ch04_09.htm#ch04-sect-pragma">Section 4.9, "Pragmas"</a> at the end of this chapter.</p><p><a name="INDEX-1035"></a><a name="INDEX-1036"></a>You <em class="emphasis">may</em> declare your variables though, if youlike, using either <tt class="literal">my</tt> or <tt class="literal">our</tt> infront of the variable name. You can even make it an error to use anundeclared variable. This kind of discipline is fine, but you have todeclare that you want the discipline. Normally, Perl minds its ownbusiness about your programming habits, but under the <tt class="literal">usestrict</tt> declaration, the use of undeclared variables isapprehended at compile time. Again, see <a href="ch04_09.htm#ch04-sect-pragma">Section 4.9, "Pragmas"</a>.</p><h2 class="sect1">4.1. Simple Statements</h2><a name="INDEX-1037"></a><p><a name="INDEX-1038"></a><a name="INDEX-1039"></a><a name="INDEX-1040"></a>A simple statement is an expression evaluated for its side effects.Every simple statement must end in a semicolon, unless it is thefinal statement in a block. In that case, the semicolon isoptional--Perl knows that you must be done with the statement, sinceyou've finished the block. But put the semicolon in anyway if it'sat the end of a multiline block, because you might eventually addanother line.</p><p><a name="INDEX-1041"></a>Even thoughoperators like <tt class="literal">eval {}</tt>, <tt class="literal">do {}</tt>,and <tt class="literal">sub {}</tt> all look like compound statements, theyreally aren't. True, they allow multiple statements on the inside,but that doesn't count. From the outside, those operators are justterms in an expression, and thus they need an explicit semicolon ifused as the last item in a statement.</p><p><a name="INDEX-1042"></a><a name="INDEX-1043"></a><a name="INDEX-1044"></a><a name="INDEX-1045"></a>Any simple statement may optionally be followed by a single modifier,just before the terminating semicolon (or block ending). The possiblemodifiers are:<blockquote><pre class="programlisting">if <em class="replaceable">EXPR</em>unless <em class="replaceable">EXPR</em>while <em class="replaceable">EXPR</em>until <em class="replaceable">EXPR</em>foreach <em class="replaceable">LIST</em></pre></blockquote>The <tt class="literal">if</tt> and <tt class="literal">unless</tt> modifiers work pretty much as they do in English:<blockquote><pre class="programlisting">$trash->take('out') if $you_love_me;shutup() unless $you_want_me_to_leave;</pre></blockquote></p><p>The <tt class="literal">while</tt> and <tt class="literal">until</tt> modifiersevaluate repeatedly. As you might expect, a <tt class="literal">while</tt>modifier keeps executing the expression as long as its expressionremains true, and an <tt class="literal">until</tt> modifier keeps executingonly as long as it remains false:<blockquote><pre class="programlisting">$expression++ while -e "$file$expression";kiss('me') until $I_die;</pre></blockquote><a name="INDEX-1046"></a><a name="INDEX-1047"></a></p><p>The <tt class="literal">foreach</tt> modifier (also spelled<tt class="literal">for</tt>) evaluates once for each element in its<em class="replaceable">LIST</em>, with <tt class="literal">$_</tt> aliased tothe current element:<blockquote><pre class="programlisting">s/java/perl/ for @resumes;print "field: $_\n" foreach split /:/, $dataline;</pre></blockquote><a name="INDEX-1048"></a><a name="INDEX-1049"></a><a name="INDEX-1050"></a></p><p>The <tt class="literal">while</tt> and <tt class="literal">until</tt> modifiershave the usual while-loop semantics (conditional evaluated first),except when applied to a <tt class="literal">do</tt><em class="replaceable">BLOCK</em> (or to the now-deprecated<tt class="literal">do</tt><em class="replaceable">SUBROUTINE</em>statement), in which case the block executes once before theconditional is evaluated. This allows you to write loops like this:<blockquote><pre class="programlisting">do { $line = <STDIN>; ...} until $line eq ".\n";</pre></blockquote>See the three different <tt class="literal">do</tt> entries in <a href="ch29_01.htm">Chapter 29, "Functions"</a>. Note also that the loop-controloperators described later will not work in this construct, sincemodifiers don't take loop labels. You can always place an extra blockaround it to terminate early, or inside it to iterate early, asdescribed later in the section <a href="ch04_05.htm#ch04-sect-bb">Section 4.5, "Bare Blocks"</a>. Or you could write areal loop with multiple loop controls inside. Speaking of real loops,we'll talk about compound statements next.</p><a name="INDEX-1249"></a><a name="INDEX-1250"></a><!-- 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="ch03_21.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_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.21. C Operators Missing from Perl</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.2. Compound 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 © 2001</a> O'Reilly & 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 + -