perlop.html

来自「perl教程」· HTML 代码 · 共 876 行 · 第 1/5 页

HTML
876
字号
<pre>
    <span class="keyword">print</span><span class="operator">((</span><span class="variable">$foo</span> <span class="operator">&amp;</span> <span class="number">255</span><span class="operator">)</span> <span class="operator">+</span> <span class="number">1</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">);</span>
</pre>
<p>See <a href="#named_unary_operators">Named Unary Operators</a> for more discussion of this.</p>
<p>Also parsed as terms are the <a href="../../lib/Pod/perlfunc.html#item_do"><code>do {}</code></a> and <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval {}</code></a> constructs, as
well as subroutine and method calls, and the anonymous
constructors <code>[]</code> and <code>{}</code>.</p>
<p>See also <a href="#quote_and_quotelike_operators">Quote and Quote-like Operators</a> toward the end of this section,
as well as <a href="#i_o_operators">I/O Operators</a>.</p>
<p>
</p>
<h2><a name="the_arrow_operator___">The Arrow Operator
   &gt;&gt;</a></h2>
<p>&quot;<code>-&gt;</code>&quot; is an infix dereference operator, just as it is in C
and C++.  If the right side is either a <code>[...]</code>, <code>{...}</code>, or a
<code>(...)</code> subscript, then the left side must be either a hard or
symbolic reference to an array, a hash, or a subroutine respectively.
(Or technically speaking, a location capable of holding a hard
reference, if it's an array or hash reference being used for
assignment.)  See <a href="../../lib/Pod/perlreftut.html">the perlreftut manpage</a> and <a href="../../lib/Pod/perlref.html">the perlref manpage</a>.</p>
<p>Otherwise, the right side is a method name or a simple scalar
variable containing either the method name or a subroutine reference,
and the left side must be either an object (a blessed reference)
or a class name (that is, a package name).  See <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a>.</p>
<p>
</p>
<h2><a name="autoincrement_and_autodecrement">Auto-increment and Auto-decrement</a></h2>
<p>&quot;++&quot; and &quot;--&quot; work as in C.  That is, if placed before a variable,
they increment or decrement the variable by one before returning the
value, and if placed after, increment or decrement after returning the
value.</p>
<pre>
    <span class="variable">$i</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>  <span class="variable">$j</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="variable">$i</span><span class="operator">++;</span>  <span class="comment"># prints 0</span>
    <span class="keyword">print</span> <span class="operator">++</span><span class="variable">$j</span><span class="operator">;</span>  <span class="comment"># prints 1</span>
</pre>
<p>Note that just as in C, Perl doesn't define <strong>when</strong> the variable is
incremented or decremented. You just know it will be done sometime 
before or after the value is returned. This also means that modifying
a variable twice in the same statement will lead to undefined behaviour.
Avoid statements like:</p>
<pre>
    <span class="variable">$i</span> <span class="operator">=</span> <span class="variable">$i</span> <span class="operator">++;</span>
    <span class="keyword">print</span> <span class="operator">++</span> <span class="variable">$i</span> <span class="operator">+</span> <span class="variable">$i</span> <span class="operator">++;</span>
</pre>
<p>Perl will not guarantee what the result of the above statements is.</p>
<p>The auto-increment operator has a little extra builtin magic to it.  If
you increment a variable that is numeric, or that has ever been used in
a numeric context, you get a normal increment.  If, however, the
variable has been used in only string contexts since it was set, and
has a value that is not the empty string and matches the pattern
<code>/^[a-zA-Z]*[0-9]*\z/</code>, the increment is done as a string, preserving each
character within its range, with carry:</p>
<pre>
    <span class="keyword">print</span> <span class="operator">++(</span><span class="variable">$foo</span> <span class="operator">=</span> <span class="string">'99'</span><span class="operator">);</span>      <span class="comment"># prints '100'</span>
    <span class="keyword">print</span> <span class="operator">++(</span><span class="variable">$foo</span> <span class="operator">=</span> <span class="string">'a0'</span><span class="operator">);</span>      <span class="comment"># prints 'a1'</span>
    <span class="keyword">print</span> <span class="operator">++(</span><span class="variable">$foo</span> <span class="operator">=</span> <span class="string">'Az'</span><span class="operator">);</span>      <span class="comment"># prints 'Ba'</span>
    <span class="keyword">print</span> <span class="operator">++(</span><span class="variable">$foo</span> <span class="operator">=</span> <span class="string">'zz'</span><span class="operator">);</span>      <span class="comment"># prints 'aaa'</span>
</pre>
<p><a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> is always treated as numeric, and in particular is changed
to <code>0</code> before incrementing (so that a post-increment of an undef value
will return <code>0</code> rather than <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>).</p>
<p>The auto-decrement operator is not magical.</p>
<p>
</p>
<h2><a name="exponentiation">Exponentiation</a></h2>
<p>Binary &quot;**&quot; is the exponentiation operator.  It binds even more
tightly than unary minus, so -2**4 is -(2**4), not (-2)**4. (This is
implemented using C's <code>pow(3)</code> function, which actually works on doubles
internally.)</p>
<p>
</p>
<h2><a name="symbolic_unary_operators">Symbolic Unary Operators</a></h2>
<p>Unary &quot;!&quot; performs logical negation, i.e., &quot;not&quot;.  See also <code>not</code> for a lower
precedence version of this.</p>
<p>Unary &quot;-&quot; performs arithmetic negation if the operand is numeric.  If
the operand is an identifier, a string consisting of a minus sign
concatenated with the identifier is returned.  Otherwise, if the string
starts with a plus or minus, a string starting with the opposite sign
is returned.  One effect of these rules is that -bareword is equivalent
to the string &quot;-bareword&quot;.  If, however, the string begins with a
non-alphabetic character (exluding &quot;+&quot; or &quot;-&quot;), Perl will attempt to convert
the string to a numeric and the arithmetic negation is performed. If the
string cannot be cleanly converted to a numeric, Perl will give the warning
<strong>Argument &quot;the string&quot; isn't numeric in negation (-) at ...</strong>.</p>
<p>Unary &quot;~&quot; performs bitwise negation, i.e., 1's complement.  For
example, <code>0666 &amp; ~027</code> is 0640.  (See also <a href="#integer_arithmetic">Integer Arithmetic</a> and
<a href="#bitwise_string_operators">Bitwise String Operators</a>.)  Note that the width of the result is
platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64
bits wide on a 64-bit platform, so if you are expecting a certain bit
width, remember to use the &amp; operator to mask off the excess bits.</p>
<p>Unary &quot;+&quot; has no effect whatsoever, even on strings.  It is useful
syntactically for separating a function name from a parenthesized expression
that would otherwise be interpreted as the complete list of function
arguments.  (See examples above under <a href="#terms_and_list_operators__leftward_">Terms and List Operators (Leftward)</a>.)</p>
<p>Unary &quot;\&quot; creates a reference to whatever follows it.  See <a href="../../lib/Pod/perlreftut.html">the perlreftut manpage</a>
and <a href="../../lib/Pod/perlref.html">the perlref manpage</a>.  Do not confuse this behavior with the behavior of
backslash within a string, although both forms do convey the notion
of protecting the next thing from interpolation.</p>
<p>
</p>
<h2><a name="binding_operators">Binding Operators</a></h2>
<p>Binary &quot;=~&quot; binds a scalar expression to a pattern match.  Certain operations
search or modify the string $_ by default.  This operator makes that kind
of operation work on some other string.  The right argument is a search
pattern, substitution, or transliteration.  The left argument is what is
supposed to be searched, substituted, or transliterated instead of the default
$_.  When used in scalar context, the return value generally indicates the
success of the operation.  Behavior in list context depends on the particular
operator.  See <a href="#regexp_quotelike_operators">Regexp Quote-Like Operators</a> for details and 
<a href="../../lib/Pod/perlretut.html">the perlretut manpage</a> for examples using these operators.</p>
<p>If the right argument is an expression rather than a search pattern,
substitution, or transliteration, it is interpreted as a search pattern at run
time.</p>
<p>Binary &quot;!~&quot; is just like &quot;=~&quot; except the return value is negated in
the logical sense.</p>
<p>
</p>
<h2><a name="multiplicative_operators">Multiplicative Operators</a></h2>
<p>Binary &quot;*&quot; multiplies two numbers.</p>
<p>Binary &quot;/&quot; divides two numbers.</p>
<p>Binary &quot;%&quot; computes the modulus of two numbers.  Given integer
operands <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> and <a href="../../lib/Pod/perlvar.html#item__b"><code>$b</code></a>: If <a href="../../lib/Pod/perlvar.html#item__b"><code>$b</code></a> is positive, then <a href="../../lib/Pod/perlvar.html#item__a"><code>$a % $b</code></a> is
<a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> minus the largest multiple of <a href="../../lib/Pod/perlvar.html#item__b"><code>$b</code></a> that is not greater than
<a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a>.  If <a href="../../lib/Pod/perlvar.html#item__b"><code>$b</code></a> is negative, then <a href="../../lib/Pod/perlvar.html#item__a"><code>$a % $b</code></a> is <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> minus the
smallest multiple of <a href="../../lib/Pod/perlvar.html#item__b"><code>$b</code></a> that is not less than <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> (i.e. the
result will be less than or equal to zero). 
Note that when <code>use integer</code> is in scope, &quot;%&quot; gives you direct access
to the modulus operator as implemented by your C compiler.  This
operator is not as well defined for negative operands, but it will
execute faster.</p>
<p>Binary &quot;x&quot; is the repetition operator.  In scalar context or if the left
operand is not enclosed in parentheses, it returns a string consisting
of the left operand repeated the number of times specified by the right
operand.  In list context, if the left operand is enclosed in
parentheses or is a list formed by <a href="#item_qw_"><code>qw/STRING/</code></a>, it repeats the list.
If the right operand is zero or negative, it returns an empty string
or an empty list, depending on the context.</p>
<pre>
    <span class="keyword">print</span> <span class="string">'-'</span> <span class="operator">x</span> <span class="number">80</span><span class="operator">;</span>             <span class="comment"># print row of dashes</span>
</pre>
<pre>
    <span class="keyword">print</span> <span class="string">"\t"</span> <span class="operator">x</span> <span class="operator">(</span><span class="variable">$tab</span><span class="operator">/</span><span class="number">8</span><span class="operator">),</span> <span class="string">' '</span> <span class="operator">x</span> <span class="operator">(</span><span class="variable">$tab</span><span class="variable">%8</span><span class="operator">);</span>      <span class="comment"># tab over</span>
</pre>
<pre>
    <span class="variable">@ones</span> <span class="operator">=</span> <span class="operator">(</span><span class="number">1</span><span class="operator">)</span> <span class="operator">x</span> <span class="number">80</span><span class="operator">;</span>           <span class="comment"># a list of 80 1's</span>
    <span class="variable">@ones</span> <span class="operator">=</span> <span class="operator">(</span><span class="number">5</span><span class="operator">)</span> <span class="operator">x</span> <span class="variable">@ones</span><span class="operator">;</span>        <span class="comment"># set all elements to 5</span>
</pre>
<p>
</p>
<h2><a name="additive_operators">Additive Operators</a></h2>
<p>Binary &quot;+&quot; returns the sum of two numbers.</p>
<p>Binary &quot;-&quot; returns the difference of two numbers.</p>
<p>Binary &quot;.&quot; concatenates two strings.</p>
<p>
</p>
<h2><a name="shift_operators_________">Shift Operators
  &gt;&gt;
&gt; &gt;&gt;&gt;</a></h2>
<p>Binary &quot;&lt;&lt;&quot; returns the value of its left argument shifted left by the
number of bits specified by the right argument.  Arguments should be
integers.  (See also <a href="#integer_arithmetic">Integer Arithmetic</a>.)</p>
<p>Binary &quot;&gt;&gt;&quot; returns the value of its left argument shifted right by
the number of bits specified by the right argument.  Arguments should
be integers.  (See also <a href="#integer_arithmetic">Integer Arithmetic</a>.)</p>
<p>Note that both &quot;&lt;&lt;&quot; and &quot;&gt;&gt;&quot; in Perl are implemented directly using
&quot;&lt;&lt;&quot; and &quot;&gt;&gt;&quot; in C.  If <code>use integer</code> (see <a href="#integer_arithmetic">Integer Arithmetic</a>) is
in force then signed C integers are used, else unsigned C integers are
used.  Either way, the implementation isn't going to generate results
larger than the size of the integer type Perl was built with (32 bits
or 64 bits).</p>
<p>The result of overflowing the range of the integers is undefined
because it is undefined also in C.  In other words, using 32-bit
integers, <code>1 &lt;&lt; 32</code> is undefined.  Shifting by a negative number
of bits is also undefined.</p>
<p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?