perlsyn.html
来自「perl教程」· HTML 代码 · 共 685 行 · 第 1/5 页
HTML
685 行
side effects. Every simple statement must be terminated with a
semicolon, unless it is the final statement in a block, in which case
the semicolon is optional. (A semicolon is still encouraged if the
block takes up more than one line, because you may eventually add
another line.) Note that there are some operators like <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval {}</code></a> and
<a href="../../lib/Pod/perlfunc.html#item_do"><code>do {}</code></a> that look like compound statements, but aren't (they're just
TERMs in an expression), and thus need an explicit termination if used
as the last item in a statement.</p>
<p>
</p>
<h2><a name="truth_and_falsehood">Truth and Falsehood</a></h2>
<p>The number 0, the strings <code>'0'</code> and <code>''</code>, the empty list <code>()</code>, and
<a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> are all false in a boolean context. All other values are true.
Negation of a true value by <code>!</code> or <code>not</code> returns a special false value.
When evaluated as a string it is treated as <code>''</code>, but as a number, it
is treated as 0.</p>
<p>
</p>
<h2><a name="statement_modifiers">Statement Modifiers</a></h2>
<p>Any simple statement may optionally be followed by a <em>SINGLE</em> modifier,
just before the terminating semicolon (or block ending). The possible
modifiers are:</p>
<pre>
if EXPR
unless EXPR
while EXPR
until EXPR
foreach LIST</pre>
<p>The <code>EXPR</code> following the modifier is referred to as the "condition".
Its truth or falsehood determines how the modifier will behave.</p>
<p><code>if</code> executes the statement once <em>if</em> and only if the condition is
true. <code>unless</code> is the opposite, it executes the statement <em>unless</em>
the condition is true (i.e., if the condition is false).</p>
<pre>
<span class="keyword">print</span> <span class="string">"Basset hounds got long ears"</span> <span class="keyword">if</span> <span class="keyword">length</span> <span class="variable">$ear</span> <span class="operator">>=</span> <span class="number">10</span><span class="operator">;</span>
<span class="variable">go_outside</span><span class="operator">()</span> <span class="keyword">and</span> <span class="variable">play</span><span class="operator">()</span> <span class="keyword">unless</span> <span class="variable">$is_raining</span><span class="operator">;</span>
</pre>
<p>The <code>foreach</code> modifier is an iterator: it executes the statement once
for each item in the LIST (with <a href="../../lib/Pod/perlvar.html#item___"><code>$_</code></a> aliased to each item in turn).</p>
<pre>
<span class="keyword">print</span> <span class="string">"Hello $_!\n"</span> <span class="keyword">foreach</span> <span class="string">qw(world Dolly nurse)</span><span class="operator">;</span>
</pre>
<p><code>while</code> repeats the statement <em>while</em> the condition is true.
<code>until</code> does the opposite, it repeats the statement <em>until</em> the
condition is true (or while the condition is false):</p>
<pre>
<span class="comment"># Both of these count from 0 to 10.</span>
<span class="keyword">print</span> <span class="variable">$i</span><span class="operator">++</span> <span class="keyword">while</span> <span class="variable">$i</span> <span class="operator"><=</span> <span class="number">10</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="variable">$j</span><span class="operator">++</span> <span class="keyword">until</span> <span class="variable">$j</span> <span class="operator">></span> <span class="number">10</span><span class="operator">;</span>
</pre>
<p>The <code>while</code> and <code>until</code> modifiers have the usual "<code>while</code> loop"
semantics (conditional evaluated first), except when applied to a
<a href="../../lib/Pod/perlfunc.html#item_do"><code>do</code></a>-BLOCK (or to the deprecated <a href="../../lib/Pod/perlfunc.html#item_do"><code>do</code></a>-SUBROUTINE statement), in
which case the block executes once before the conditional is
evaluated. This is so that you can write loops like:</p>
<pre>
<span class="keyword">do</span> <span class="operator">{</span>
<span class="variable">$line</span> <span class="operator">=</span> <span class="operator"><</span><span class="variable">STDIN</span><span class="operator">>;</span>
<span class="operator">...</span>
<span class="operator">}</span> <span class="keyword">until</span> <span class="variable">$line</span> <span class="keyword">eq</span> <span class="string">".\n"</span><span class="operator">;</span>
</pre>
<p>See <a href="../../lib/Pod/perlfunc.html#do">do in the perlfunc manpage</a>. Note also that the loop control statements described
later will <em>NOT</em> work in this construct, because modifiers don't take
loop labels. Sorry. You can always put another block inside of it
(for <a href="../../lib/Pod/perlfunc.html#item_next"><code>next</code></a>) or around it (for <a href="../../lib/Pod/perlfunc.html#item_last"><code>last</code></a>) to do that sort of thing.
For <a href="../../lib/Pod/perlfunc.html#item_next"><code>next</code></a>, just double the braces:</p>
<pre>
<span class="keyword">do</span> <span class="operator">{{</span>
<span class="keyword">next</span> <span class="keyword">if</span> <span class="variable">$x</span> <span class="operator">==</span> <span class="variable">$y</span><span class="operator">;</span>
<span class="comment"># do something here</span>
<span class="operator">}}</span> <span class="keyword">until</span> <span class="variable">$x</span><span class="operator">++</span> <span class="operator">></span> <span class="variable">$z</span><span class="operator">;</span>
</pre>
<p>For <a href="../../lib/Pod/perlfunc.html#item_last"><code>last</code></a>, you have to be more elaborate:</p>
<pre>
<span class="variable">LOOP</span><span class="operator">:</span> <span class="operator">{</span>
<span class="keyword">do</span> <span class="operator">{</span>
<span class="keyword">last</span> <span class="keyword">if</span> <span class="variable">$x</span> <span class="operator">=</span> <span class="variable">$y</span><span class="operator">**</span><span class="number">2</span><span class="operator">;</span>
<span class="comment"># do something here</span>
<span class="operator">}</span> <span class="keyword">while</span> <span class="variable">$x</span><span class="operator">++</span> <span class="operator"><=</span> <span class="variable">$z</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p><strong>NOTE:</strong> The behaviour of a <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> statement modified with a statement
modifier conditional or loop construct (e.g. <code>my $x if ...</code>) is
<strong>undefined</strong>. The value of the <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> variable may be <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>, any
previously assigned value, or possibly anything else. Don't rely on
it. Future versions of perl might do something different from the
version of perl you try it out on. Here be dragons.</p>
<p>
</p>
<h2><a name="compound_statements">Compound Statements</a></h2>
<p>In Perl, a sequence of statements that defines a scope is called a block.
Sometimes a block is delimited by the file containing it (in the case
of a required file, or the program as a whole), and sometimes a block
is delimited by the extent of a string (in the case of an eval).</p>
<p>But generally, a block is delimited by curly brackets, also known as braces.
We will call this syntactic construct a BLOCK.</p>
<p>The following compound statements may be used to control flow:</p>
<pre>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">EXPR</span><span class="operator">)</span> <span class="variable">BLOCK</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">EXPR</span><span class="operator">)</span> <span class="variable">BLOCK</span> <span class="keyword">else</span> <span class="variable">BLOCK</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">EXPR</span><span class="operator">)</span> <span class="variable">BLOCK</span> <span class="keyword">elsif</span> <span class="operator">(</span><span class="variable">EXPR</span><span class="operator">)</span> <span class="variable">BLOCK</span> <span class="operator">...</span> <span class="keyword">else</span> <span class="variable">BLOCK</span>
<span class="variable">LABEL</span> <span class="keyword">while</span> <span class="operator">(</span><span class="variable">EXPR</span><span class="operator">)</span> <span class="variable">BLOCK</span>
<span class="variable">LABEL</span> <span class="keyword">while</span> <span class="operator">(</span><span class="variable">EXPR</span><span class="operator">)</span> <span class="variable">BLOCK</span> <span class="keyword">continue</span> <span class="variable">BLOCK</span>
<span class="variable">LABEL</span> <span class="keyword">until</span> <span class="operator">(</span><span class="variable">EXPR</span><span class="operator">)</span> <span class="variable">BLOCK</span>
<span class="variable">LABEL</span> <span class="keyword">until</span> <span class="operator">(</span><span class="variable">EXPR</span><span class="operator">)</span> <span class="variable">BLOCK</span> <span class="keyword">continue</span> <span class="variable">BLOCK</span>
<span class="variable">LABEL</span> <span class="keyword">for</span> <span class="operator">(</span><span class="variable">EXPR</span><span class="operator">;</span> <span class="variable">EXPR</span><span class="operator">;</span> <span class="variable">EXPR</span><span class="operator">)</span> <span class="variable">BLOCK</span>
<span class="variable">LABEL</span> <span class="keyword">foreach</span> <span class="variable">VAR</span> <span class="operator">(</span><span class="variable">LIST</span><span class="operator">)</span> <span class="variable">BLOCK</span>
<span class="variable">LABEL</span> <span class="keyword">foreach</span> <span class="variable">VAR</span> <span class="operator">(</span><span class="variable">LIST</span><span class="operator">)</span> <span class="variable">BLOCK</span> <span class="keyword">continue</span> <span class="variable">BLOCK</span>
<span class="variable">LABEL</span> <span class="variable">BLOCK</span> <span class="keyword">continue</span> <span class="variable">BLOCK</span>
</pre>
<p>Note that, unlike C and Pascal, these are defined in terms of BLOCKs,
not statements. This means that the curly brackets are <em>required</em>--no
dangling statements allowed. If you want to write conditionals without
curly brackets there are several other ways to do it. The following
all do the same thing:</p>
<pre>
<span class="keyword">if</span> <span class="operator">(!</span><span class="keyword">open</span><span class="operator">(</span><span class="variable">FOO</span><span class="operator">))</span> <span class="operator">{</span> <span class="keyword">die</span> <span class="string">"Can't open $FOO: $!"</span><span class="operator">;</span> <span class="operator">}</span>
<span class="keyword">die</span> <span class="string">"Can't open $FOO: $!"</span> <span class="keyword">unless</span> <span class="keyword">open</span><span class="operator">(</span><span class="variable">FOO</span><span class="operator">);</span>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">FOO</span><span class="operator">)</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Can't open $FOO: $!"</span><span class="operator">;</span> <span class="comment"># FOO or bust!</span>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">FOO</span><span class="operator">)</span> <span class="operator">?</span> <span class="string">'hi mom'</span> <span class="operator">:</span> <span class="keyword">die</span> <span class="string">"Can't open $FOO: $!"</span><span class="operator">;</span>
<span class="comment"># a bit exotic, that last one</span>
</pre>
<p>The <code>if</code> statement is straightforward. Because BLOCKs are always
bounded by curly brackets, there is never any ambiguity about which
<code>if</code> an <code>else</code> goes with. If you use <code>unless</code> in place of <code>if</code>,
the sense of the test is reversed.</p>
<p>The <code>while</code> statement executes the block as long as the expression is
true (does not evaluate to the null string <code>""</code> or <code>0</code> or <code>"0"</code>).
The <code>until</code> statement executes the block as long as the expression is
false.
The LABEL is optional, and if present, consists of an identifier followed
by a colon. The LABEL identifies the loop for the loop control
statements <a href="../../lib/Pod/perlfunc.html#item_next"><code>next</code></a>, <a href="../../lib/Pod/perlfunc.html#item_last"><code>last</code></a>, and <a href="../../lib/Pod/perlfunc.html#item_redo"><code>redo</code></a>.
If the LABEL is omitted, the loop control statement
refers to the innermost enclosing loop. This may include dynamically
looking back your call-stack at run time to find the LABEL. Such
desperate behavior triggers a warning if you use the <code>use warnings</code>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?