perlop.html
来自「perl教程」· HTML 代码 · 共 876 行 · 第 1/5 页
HTML
876 行
</p>
<h2><a name="named_unary_operators">Named Unary Operators</a></h2>
<p>The various named unary operators are treated as functions with one
argument, with optional parentheses.</p>
<p>If any list operator (print(), etc.) or any unary operator (chdir(), etc.)
is followed by a left parenthesis as the next token, the operator and
arguments within parentheses are taken to be of highest precedence,
just like a normal function call. For example,
because named unary operators are higher precedence than ||:</p>
<pre>
<span class="keyword">chdir</span> <span class="variable">$foo</span> <span class="operator">||</span> <span class="keyword">die</span><span class="operator">;</span> <span class="comment"># (chdir $foo) || die</span>
<span class="keyword">chdir</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="operator">;</span> <span class="comment"># (chdir $foo) || die</span>
<span class="keyword">chdir</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="operator">;</span> <span class="comment"># (chdir $foo) || die</span>
<span class="keyword">chdir</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="operator">;</span> <span class="comment"># (chdir $foo) || die</span>
</pre>
<p>but, because * is higher precedence than named operators:</p>
<pre>
<span class="keyword">chdir</span> <span class="variable">$foo</span> <span class="operator">*</span> <span class="number">20</span><span class="operator">;</span> <span class="comment"># chdir ($foo * 20)</span>
<span class="keyword">chdir</span><span class="operator">(</span><span class="variable">$foo</span><span class="operator">)</span> <span class="operator">*</span> <span class="number">20</span><span class="operator">;</span> <span class="comment"># (chdir $foo) * 20</span>
<span class="keyword">chdir</span> <span class="operator">(</span><span class="variable">$foo</span><span class="operator">)</span> <span class="operator">*</span> <span class="number">20</span><span class="operator">;</span> <span class="comment"># (chdir $foo) * 20</span>
<span class="keyword">chdir</span> <span class="operator">+(</span><span class="variable">$foo</span><span class="operator">)</span> <span class="operator">*</span> <span class="number">20</span><span class="operator">;</span> <span class="comment"># chdir ($foo * 20)</span>
</pre>
<pre>
<span class="keyword">rand</span> <span class="number">10</span> <span class="operator">*</span> <span class="number">20</span><span class="operator">;</span> <span class="comment"># rand (10 * 20)</span>
<span class="keyword">rand</span><span class="operator">(</span><span class="number">10</span><span class="operator">)</span> <span class="operator">*</span> <span class="number">20</span><span class="operator">;</span> <span class="comment"># (rand 10) * 20</span>
<span class="keyword">rand</span> <span class="operator">(</span><span class="number">10</span><span class="operator">)</span> <span class="operator">*</span> <span class="number">20</span><span class="operator">;</span> <span class="comment"># (rand 10) * 20</span>
<span class="keyword">rand</span> <span class="operator">+(</span><span class="number">10</span><span class="operator">)</span> <span class="operator">*</span> <span class="number">20</span><span class="operator">;</span> <span class="comment"># rand (10 * 20)</span>
</pre>
<p>Regarding precedence, the filetest operators, like <a href="../../lib/Pod/perlrun.html#item__2df"><code>-f</code></a>, <code>-M</code>, etc. are
treated like named unary operators, but they don't follow this functional
parenthesis rule. That means, for example, that <a href="../../lib/Pod/perlguts.html#item_f"><code>-f($file).".bak"</code></a> is
equivalent to <code>-f "$file.bak"</code>.</p>
<p>See also <a href="#terms_and_list_operators__leftward_">Terms and List Operators (Leftward)</a>.</p>
<p>
</p>
<h2><a name="relational_operators">Relational Operators</a></h2>
<p>Binary "<" returns true if the left argument is numerically less than
the right argument.
></p>
<p>Binary ">" returns true if the left argument is numerically greater
than the right argument.
>></p>
<p>Binary "<=" returns true if the left argument is numerically less than
or equal to the right argument.
></p>
<p>Binary ">=" returns true if the left argument is numerically greater
than or equal to the right argument.
= >></p>
<p>Binary "lt" returns true if the left argument is stringwise less than
the right argument.
></p>
<p>Binary "gt" returns true if the left argument is stringwise greater
than the right argument.
></p>
<p>Binary "le" returns true if the left argument is stringwise less than
or equal to the right argument.
></p>
<p>Binary "ge" returns true if the left argument is stringwise greater
than or equal to the right argument.
></p>
<p>
</p>
<h2><a name="equality_operators">Equality Operators</a></h2>
<p>Binary "==" returns true if the left argument is numerically equal to
the right argument.</p>
<p>Binary "!=" returns true if the left argument is numerically not equal
to the right argument.</p>
<p>Binary "<=>" returns -1, 0, or 1 depending on whether the left
argument is numerically less than, equal to, or greater than the right
argument. If your platform supports NaNs (not-a-numbers) as numeric
values, using them with "<=>" returns undef. NaN is not "<", "==", ">",
"<=" or ">=" anything (even NaN), so those 5 return false. NaN != NaN
returns true, as does NaN != anything else. If your platform doesn't
support NaNs then NaN is just a string with numeric value 0.
>></p>
<pre>
<span class="variable">perl</span> <span class="operator">-</span><span class="keyword">le</span> <span class="string">'$a = "NaN"; print "No NaN support here" if $a == $a'</span>
<span class="variable">perl</span> <span class="operator">-</span><span class="keyword">le</span> <span class="string">'$a = "NaN"; print "NaN support here" if $a != $a'</span>
</pre>
<p>Binary "eq" returns true if the left argument is stringwise equal to
the right argument.</p>
<p>Binary "ne" returns true if the left argument is stringwise not equal
to the right argument.</p>
<p>Binary "cmp" returns -1, 0, or 1 depending on whether the left
argument is stringwise less than, equal to, or greater than the right
argument.</p>
<p>"lt", "le", "ge", "gt" and "cmp" use the collation (sort) order specified
by the current locale if <code>use locale</code> is in effect. See <a href="../../lib/Pod/perllocale.html">the perllocale manpage</a>.</p>
<p>
</p>
<h2><a name="bitwise_and">Bitwise And</a></h2>
<p>Binary "&" returns its operands ANDed together bit by bit.
(See also <a href="#integer_arithmetic">Integer Arithmetic</a> and <a href="#bitwise_string_operators">Bitwise String Operators</a>.)</p>
<p>Note that "&" has lower priority than relational operators, so for example
the brackets are essential in a test like</p>
<pre>
<span class="keyword">print</span> <span class="string">"Even\n"</span> <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$x</span> <span class="operator">&</span> <span class="number">1</span><span class="operator">)</span> <span class="operator">==</span> <span class="number">0</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="bitwise_or_and_exclusive_or">Bitwise Or and Exclusive Or</a></h2>
<p>Binary "|" returns its operands ORed together bit by bit.
(See also <a href="#integer_arithmetic">Integer Arithmetic</a> and <a href="#bitwise_string_operators">Bitwise String Operators</a>.)</p>
<p>Binary "^" returns its operands XORed together bit by bit.
(See also <a href="#integer_arithmetic">Integer Arithmetic</a> and <a href="#bitwise_string_operators">Bitwise String Operators</a>.)</p>
<p>Note that "|" and "^" have lower priority than relational operators, so
for example the brackets are essential in a test like</p>
<pre>
<span class="keyword">print</span> <span class="string">"false\n"</span> <span class="keyword">if</span> <span class="operator">(</span><span class="number">8</span> <span class="operator">|</span> <span class="number">2</span><span class="operator">)</span> <span class="operator">!=</span> <span class="number">10</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="cstyle_logical_and">C-style Logical And</a></h2>
<p>Binary "&&" performs a short-circuit logical AND operation. That is,
if the left operand is false, the right operand is not even evaluated.
Scalar or list context propagates down to the right operand if it
is evaluated.</p>
<p>
</p>
<h2><a name="cstyle_logical_or">C-style Logical Or</a></h2>
<p>Binary "||" performs a short-circuit logical OR operation. That is,
if the left operand is true, the right operand is not even evaluated.
Scalar or list context propagates down to the right operand if it
is evaluated.</p>
<p>The <code>||</code> and <code>&&</code> operators return the last value evaluated
(unlike C's <code>||</code> and <code>&&</code>, which return 0 or 1). Thus, a reasonably
portable way to find out the home directory might be:</p>
<pre>
<span class="variable">$home</span> <span class="operator">=</span> <span class="variable">$ENV</span><span class="operator">{</span><span class="string">'HOME'</span><span class="operator">}</span> <span class="operator">||</span> <span class="variable">$ENV</span><span class="operator">{</span><span class="string">'LOGDIR'</span><span class="operator">}</span> <span class="operator">||</span>
<span class="operator">(</span><span class="keyword">getpwuid</span><span class="operator">(</span><span class="variable">$<</span><span class="operator">))</span><span class="operator">[</span><span class="number">7</span><span class="operator">]</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"You're homeless!\n"</span><span class="operator">;</span>
</pre>
<p>In particular, this means that you shouldn't use this
for selecting between two aggregates for assignment:</p>
<pre>
<span class="variable">@a</span> <span class="operator">=</span> <span class="variable">@b</span> <span class="operator">||</span> <span class="variable">@c</span><span class="operator">;</span> <span class="comment"># this is wrong</span>
<span class="variable">@a</span> <span class="operator">=</span> <span class="keyword">scalar</span><span class="operator">(</span><span class="variable">@b</span><span class="operator">)</span> <span class="operator">||</span> <span class="variable">@c</span><span class="operator">;</span> <span class="comment"># really meant this</span>
<span class="variable">@a</span> <span class="operator">=</span> <span class="variable">@b</span> <span class="operator">?</span> <span class="variable">@b</span> <span class="operator">:</span> <span class="variable">@c</span><span class="operator">;</span> <span class="comment"># this works fine, though</span>
</pre>
<p>As more readable alternatives to <code>&&</code> and <code>||</code> when used for
control flow, Perl provides <code>and</code> and <code>or</code> operators (see below).
The short-circuit behavior is identical. The precedence of "and" and
"or" is much lower, however, so that you can safely use them after a
list operator without the need for parentheses:</p>
<pre>
<span class="keyword">unlink</span> <span class="string">"alpha"</span><span class="operator">,</span> <span class="string">"beta"</span><span class="operator">,</span> <span class="string">"gamma"</span>
<span class="keyword">or</span> <span class="variable">gripe</span><span class="operator">(),</span> <span class="keyword">next</span> <span class="variable">LINE</span><span class="operator">;</span>
</pre>
<p>With the C-style operators that would have been written like this:</p>
<pre>
<span class="keyword">unlink</span><span class="operator">(</span><span class="string">"alpha"</span><span class="operator">,</span> <span class="string">"beta"</span><span class="operator">,</span> <span class="string">"gamma"</span><span class="operator">)</span>
<span class="operator">||</span> <span class="operator">(</span><span class="variable">gripe</span><span class="operator">(),</span> <span class="keyword">next</span> <span class="variable">LINE</span><span class="operator">);</span>
</pre>
<p>Using "or" for assignment is unlikely to do what you want; see below.</p>
<p>
</p>
<h2><a name="range_operators">Range Operators</a></h2>
<p>Binary ".." is the range operator, which is really two different
operators depending on the context. In list context, it returns a
list of values counting (up by ones) from the left value to the right
value. If the left value is greater than the right value then it
returns the empty list. The range operator is useful for writing
<code>foreach (1..10)</code> loops and for doing slice operations on arrays. In
the current implementation, no temporary array is created when the
range operator is used as the expression in <code>foreach</code> loops, but older
versions of Perl might burn a lot of memory when you write something
like this:</p>
<pre>
<span class="keyword">for</span> <span class="operator">(</span><span class="number">1</span> <span class="operator">..</span> <span class="number">1_000_000</span><span class="operator">)</span> <span class="operator">{</span>
<span class="comment"># code</span>
<span class="operator">}</span>
</pre>
<p>The range operator also works on strings, using the magical auto-increment,
see below.</p>
<p>In scalar context, ".." returns a boolean value. The operator is
bistable, like a flip-flop, and emulates the line-range (comma) operator
of <strong>sed</strong>, <strong>awk</strong>, and various editors. Each ".." operator maintains its
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?