perlop.html
来自「perl教程」· HTML 代码 · 共 876 行 · 第 1/5 页
HTML
876 行
<span class="operator">(</span><span class="variable">$a</span> <span class="operator">+=</span> <span class="number">2</span><span class="operator">)</span> <span class="operator">*=</span> <span class="number">3</span><span class="operator">;</span>
</pre>
<p>is equivalent to</p>
<pre>
<span class="variable">$a</span> <span class="operator">+=</span> <span class="number">2</span><span class="operator">;</span>
<span class="variable">$a</span> <span class="operator">*=</span> <span class="number">3</span><span class="operator">;</span>
</pre>
<p>Similarly, a list assignment in list context produces the list of
lvalues assigned to, and a list assignment in scalar context returns
the number of elements produced by the expression on the right hand
side of the assignment.</p>
<p>
</p>
<h2><a name="comma_operator">Comma Operator</a></h2>
<p>Binary "," is the comma operator. In scalar context it evaluates
its left argument, throws that value away, then evaluates its right
argument and returns that value. This is just like C's comma operator.</p>
<p>In list context, it's just the list argument separator, and inserts
both its arguments into the list.</p>
<p>The <code>=></code> operator is a synonym for the comma, but forces any word
(consisting entirely of word characters) to its left to be interpreted
as a string (as of 5.001). This includes words that might otherwise be
considered a constant or function call.</p>
<pre>
<span class="keyword">use</span> <span class="variable">constant</span> <span class="string">FOO</span> <span class="operator">=></span> <span class="string">"something"</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">%h</span> <span class="operator">=</span> <span class="operator">(</span> <span class="string">FOO</span> <span class="operator">=></span> <span class="number">23</span> <span class="operator">);</span>
</pre>
<p>is equivalent to:</p>
<pre>
<span class="keyword">my</span> <span class="variable">%h</span> <span class="operator">=</span> <span class="operator">(</span><span class="string">"FOO"</span><span class="operator">,</span> <span class="number">23</span><span class="operator">);</span>
</pre>
<p>It is <em>NOT</em>:</p>
<pre>
<span class="keyword">my</span> <span class="variable">%h</span> <span class="operator">=</span> <span class="operator">(</span><span class="string">"something"</span><span class="operator">,</span> <span class="number">23</span><span class="operator">);</span>
</pre>
<p>If the argument on the left is not a word, it is first interpreted as
an expression, and then the string value of that is used.</p>
<p>The <code>=></code> operator is helpful in documenting the correspondence
between keys and values in hashes, and other paired elements in lists.</p>
<pre>
<span class="variable">%hash</span> <span class="operator">=</span> <span class="operator">(</span> <span class="variable">$key</span> <span class="operator">=></span> <span class="variable">$value</span> <span class="operator">);</span>
<span class="variable">login</span><span class="operator">(</span> <span class="variable">$username</span> <span class="operator">=></span> <span class="variable">$password</span> <span class="operator">);</span>
</pre>
<p>
</p>
<h2><a name="list_operators__rightward_">List Operators (Rightward)</a></h2>
<p>On the right side of a list operator, it has very low precedence,
such that it controls all comma-separated expressions found there.
The only operators with lower precedence are the logical operators
"and", "or", and "not", which may be used to evaluate calls to list
operators without the need for extra parentheses:</p>
<pre>
<span class="keyword">open</span> <span class="variable">HANDLE</span><span class="operator">,</span> <span class="string">"filename"</span>
<span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Can't open: $!\n"</span><span class="operator">;</span>
</pre>
<p>See also discussion of list operators in <a href="#terms_and_list_operators__leftward_">Terms and List Operators (Leftward)</a>.</p>
<p>
</p>
<h2><a name="logical_not">Logical Not</a></h2>
<p>Unary "not" returns the logical negation of the expression to its right.
It's the equivalent of "!" except for the very low precedence.</p>
<p>
</p>
<h2><a name="logical_and">Logical And</a></h2>
<p>Binary "and" returns the logical conjunction of the two surrounding
expressions. It's equivalent to && except for the very low
precedence. This means that it short-circuits: i.e., the right
expression is evaluated only if the left expression is true.</p>
<p>
</p>
<h2><a name="logical_or_and_exclusive_or">Logical or and Exclusive Or</a></h2>
<p>Binary "or" returns the logical disjunction of the two surrounding
expressions. It's equivalent to || except for the very low precedence.
This makes it useful for control flow</p>
<pre>
<span class="keyword">print</span> <span class="variable">FH</span> <span class="variable">$data</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Can't write to FH: $!"</span><span class="operator">;</span>
</pre>
<p>This means that it short-circuits: i.e., the right expression is evaluated
only if the left expression is false. Due to its precedence, you should
probably avoid using this for assignment, only for control flow.</p>
<pre>
<span class="variable">$a</span> <span class="operator">=</span> <span class="variable">$b</span> <span class="keyword">or</span> <span class="variable">$c</span><span class="operator">;</span> <span class="comment"># bug: this is wrong</span>
<span class="operator">(</span><span class="variable">$a</span> <span class="operator">=</span> <span class="variable">$b</span><span class="operator">)</span> <span class="keyword">or</span> <span class="variable">$c</span><span class="operator">;</span> <span class="comment"># really means this</span>
<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"># better written this way</span>
</pre>
<p>However, when it's a list-context assignment and you're trying to use
"||" for control flow, you probably need "or" so that the assignment
takes higher precedence.</p>
<pre>
<span class="variable">@info</span> <span class="operator">=</span> <span class="keyword">stat</span><span class="operator">(</span><span class="variable">$file</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span><span class="operator">;</span> <span class="comment"># oops, scalar sense of stat!</span>
<span class="variable">@info</span> <span class="operator">=</span> <span class="keyword">stat</span><span class="operator">(</span><span class="variable">$file</span><span class="operator">)</span> <span class="keyword">or</span> <span class="keyword">die</span><span class="operator">;</span> <span class="comment"># better, now @info gets its due</span>
</pre>
<p>Then again, you could always use parentheses.</p>
<p>Binary "xor" returns the exclusive-OR of the two surrounding expressions.
It cannot short circuit, of course.</p>
<p>
</p>
<h2><a name="c_operators_missing_from_perl">C Operators Missing From Perl</a></h2>
<p>Here is what C has that Perl doesn't:</p>
<dl>
<dt><strong><a name="item_unary">unary &</a></strong>
<dd>
<p>Address-of operator. (But see the "\" operator for taking a reference.)</p>
</dd>
</li>
<dt><strong><a name="item_unary__2a">unary *</a></strong>
<dd>
<p>Dereference-address operator. (Perl's prefix dereferencing
operators are typed: $, @, %, and &.)</p>
</dd>
</li>
<dt><strong><a name="item__28type_29">(TYPE)</a></strong>
<dd>
<p>Type-casting operator.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="quote_and_quotelike_operators___">Quote and Quote-like Operators
>></a></h2>
<p>While we usually think of quotes as literal values, in Perl they
function as operators, providing various kinds of interpolating and
pattern matching capabilities. Perl provides customary quote characters
for these behaviors, but also provides a way for you to choose your
quote character for any of them. In the following table, a <code>{}</code> represents
any pair of delimiters you choose.</p>
<pre>
Customary Generic Meaning Interpolates
'' q{} Literal no
"" qq{} Literal yes
`` qx{} Command yes*
qw{} Word list no
// m{} Pattern match yes*
qr{} Pattern yes*
s{}{} Substitution yes*
tr{}{} Transliteration no (but see below)
<<EOF here-doc yes*</pre>
<pre>
* unless the delimiter is ''.</pre>
<p>Non-bracketing delimiters use the same character fore and aft, but the four
sorts of brackets (round, angle, square, curly) will all nest, which means
that</p>
<pre>
<span class="string">q{foo{bar}baz}</span>
</pre>
<p>is the same as</p>
<pre>
<span class="string">'foo{bar}baz'</span>
</pre>
<p>Note, however, that this does not always work for quoting Perl code:</p>
<pre>
<span class="variable">$s</span> <span class="operator">=</span> <span class="string">q{ if($a eq "}</span><span class="string">") ... }; # WRONG
</span>
</pre>
<p>is a syntax error. The <code>Text::Balanced</code> module (from CPAN, and
starting from Perl 5.8 part of the standard distribution) is able
to do this properly.</p>
<p>There can be whitespace between the operator and the quoting
characters, except when <code>#</code> is being used as the quoting character.
<code>q#foo#</code> is parsed as the string <code>foo</code>, while <code>q #foo#</code> is the
operator <code>q</code> followed by a comment. Its argument will be taken
from the next line. This allows you to write:</p>
<pre>
<span class="regex">s {foo} # Replace foo
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?