perltrap.html

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

HTML
1,076
字号
<li>
<p>System calls such as link(), unlink(), rename(), etc. return nonzero for
success, not 0. (system(), however, returns zero for success.)</p>
</li>
<li>
<p>Signal handlers deal with signal names, not numbers.  Use <code>kill -l</code>
to find their names on your system.</p>
</li>
</ul>
<p>
</p>
<h2><a name="sed_traps">Sed Traps</a></h2>
<p>Seasoned <strong>sed</strong> programmers should take note of the following:</p>
<ul>
<li>
<p>A Perl program executes only once, not once for each input line.  You can
do an implicit loop with <a href="../../lib/Pod/perlrun.html#item__2dn"><code>-n</code></a> or <a href="../../lib/Pod/perlrun.html#item__2dp"><code>-p</code></a>.</p>
</li>
<li>
<p>Backreferences in substitutions use &quot;$&quot; rather than &quot;\&quot;.</p>
</li>
<li>
<p>The pattern matching metacharacters &quot;(&quot;, &quot;)&quot;, and &quot;|&quot; do not have backslashes
in front.</p>
</li>
<li>
<p>The range operator is <code>...</code>, rather than comma.</p>
</li>
</ul>
<p>
</p>
<h2><a name="shell_traps">Shell Traps</a></h2>
<p>Sharp shell programmers should take note of the following:</p>
<ul>
<li>
<p>The backtick operator does variable interpolation without regard to
the presence of single quotes in the command.</p>
</li>
<li>
<p>The backtick operator does no translation of the return value, unlike <strong>csh</strong>.</p>
</li>
<li>
<p>Shells (especially <strong>csh</strong>) do several levels of substitution on each
command line.  Perl does substitution in only certain constructs
such as double quotes, backticks, angle brackets, and search patterns.</p>
</li>
<li>
<p>Shells interpret scripts a little bit at a time.  Perl compiles the
entire program before executing it (except for <code>BEGIN</code> blocks, which
execute at compile time).</p>
</li>
<li>
<p>The arguments are available via @ARGV, not $1, $2, etc.</p>
</li>
<li>
<p>The environment is not automatically made available as separate scalar
variables.</p>
</li>
<li>
<p>The shell's <code>test</code> uses &quot;=&quot;, &quot;!=&quot;, &quot;&lt;&quot; etc for string comparisons and &quot;-eq&quot;,
&quot;-ne&quot;, &quot;-lt&quot; etc for numeric comparisons. This is the reverse of Perl, which
uses <code>eq</code>, <code>ne</code>, <code>lt</code> for string comparisons, and <code>==</code>, <code>!=</code> <code>&lt;</code> etc
for numeric comparisons.</p>
</li>
</ul>
<p>
</p>
<h2><a name="perl_traps">Perl Traps</a></h2>
<p>Practicing Perl Programmers should take note of the following:</p>
<ul>
<li>
<p>Remember that many operations behave differently in a list
context than they do in a scalar one.  See <a href="../../lib/Pod/perldata.html">the perldata manpage</a> for details.</p>
</li>
<li>
<p>Avoid barewords if you can, especially all lowercase ones.
You can't tell by just looking at it whether a bareword is
a function or a string.  By using quotes on strings and
parentheses on function calls, you won't ever get them confused.</p>
</li>
<li>
<p>You cannot discern from mere inspection which builtins
are unary operators (like <a href="../../lib/Pod/perlfunc.html#item_chop"><code>chop()</code></a> and <a href="../../lib/Pod/perlfunc.html#item_chdir"><code>chdir())</code></a>
and which are list operators (like <a href="../../lib/Pod/perlfunc.html#item_print"><code>print()</code></a> and unlink()).
(Unless prototyped, user-defined subroutines can <strong>only</strong> be list
operators, never unary ones.)  See <a href="../../lib/Pod/perlop.html">the perlop manpage</a> and <a href="../../lib/Pod/perlsub.html">the perlsub manpage</a>.</p>
</li>
<li>
<p>People have a hard time remembering that some functions
default to $_, or @ARGV, or whatever, but that others which
you might expect to do not.</p>
</li>
<li>
<p>The &lt;FH&gt; construct is not the name of the filehandle, it is a readline
operation on that handle.  The data read is assigned to $_ only if the
file read is the sole condition in a while loop:</p>
<pre>
    <span class="keyword">while</span> <span class="operator">(&lt;</span><span class="variable">FH</span><span class="operator">&gt;)</span>      <span class="operator">{</span> <span class="operator">}</span>
    <span class="keyword">while</span> <span class="operator">(</span><span class="keyword">defined</span><span class="operator">(</span><span class="variable">$_</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">FH</span><span class="operator">&gt;))</span> <span class="operator">{</span> <span class="operator">}..</span>
    <span class="operator">&lt;</span><span class="variable">FH</span><span class="operator">&gt;;</span>  <span class="comment"># data discarded!</span>
</pre>
</li>
<li>
<p>Remember not to use <code>=</code> when you need <code>=~</code>;
these two constructs are quite different:</p>
<pre>
    <span class="variable">$x</span> <span class="operator">=</span>  <span class="regex">/foo/</span><span class="operator">;</span>
    <span class="variable">$x</span> <span class="operator">=~</span> <span class="regex">/foo/</span><span class="operator">;</span>
</pre>
</li>
<li>
<p>The <a href="../../lib/Pod/perlfunc.html#item_do"><code>do {}</code></a> construct isn't a real loop that you can use
loop control on.</p>
</li>
<li>
<p>Use <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> for local variables whenever you can get away with
it (but see <a href="../../lib/Pod/perlform.html">the perlform manpage</a> for where you can't).
Using <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> actually gives a local value to a global
variable, which leaves you open to unforeseen side-effects
of dynamic scoping.</p>
</li>
<li>
<p>If you localize an exported variable in a module, its exported value will
not change.  The local name becomes an alias to a new value but the
external name is still an alias for the original.</p>
</li>
</ul>
<p>
</p>
<h2><a name="perl4_to_perl5_traps">Perl4 to Perl5 Traps</a></h2>
<p>Practicing Perl4 Programmers should take note of the following
Perl4-to-Perl5 specific traps.</p>
<p>They're crudely ordered according to the following list:</p>
<dl>
<dt><strong><a name="item_discontinuance_2c_deprecation_2c_and_bugfix_traps">Discontinuance, Deprecation, and BugFix traps</a></strong>

<dd>
<p>Anything that's been fixed as a perl4 bug, removed as a perl4 feature
or deprecated as a perl4 feature with the intent to encourage usage of
some other perl5 feature.</p>
</dd>
</li>
<dt><strong><a name="item_parsing_traps">Parsing Traps</a></strong>

<dd>
<p>Traps that appear to stem from the new parser.</p>
</dd>
</li>
<dt><strong><a name="item_numerical_traps">Numerical Traps</a></strong>

<dd>
<p>Traps having to do with numerical or mathematical operators.</p>
</dd>
</li>
<dt><strong><a name="item_general_data_type_traps">General data type traps</a></strong>

<dd>
<p>Traps involving perl standard data types.</p>
</dd>
</li>
<dt><strong><a name="item_context_traps__2d_scalar_2c_list_contexts">Context Traps - scalar, list contexts</a></strong>

<dd>
<p>Traps related to context within lists, scalar statements/declarations.</p>
</dd>
</li>
<dt><strong><a name="item_precedence_traps">Precedence Traps</a></strong>

<dd>
<p>Traps related to the precedence of parsing, evaluation, and execution of
code.</p>
</dd>
</li>
<dt><strong><a name="item_general_regular_expression_traps_using_s_2f_2f_2f_">General Regular Expression Traps using s///, etc.</a></strong>

<dd>
<p>Traps related to the use of pattern matching.</p>
</dd>
</li>
<dt><strong><a name="item_subroutine_2c_signal_2c_sorting_traps">Subroutine, Signal, Sorting Traps</a></strong>

<dd>
<p>Traps related to the use of signals and signal handlers, general subroutines,
and sorting, along with sorting subroutines.</p>
</dd>
</li>
<dt><strong><a name="item_os_traps">OS Traps</a></strong>

<dd>
<p>OS-specific traps.</p>
</dd>
</li>
<dt><strong><a name="item_dbm_traps">DBM Traps</a></strong>

<dd>
<p>Traps specific to the use of <a href="#item_dbmopen"><code>dbmopen()</code></a>, and specific dbm implementations.</p>
</dd>
</li>
<dt><strong><a name="item_unclassified_traps">Unclassified Traps</a></strong>

<dd>
<p>Everything else.</p>
</dd>
</li>
</dl>
<p>If you find an example of a conversion trap that is not listed here,
please submit it to &lt;<em><a href="mailto:perlbug@perl.org">perlbug@perl.org</a></em>&gt; for inclusion.
Also note that at least some of these can be caught with the
<code>use warnings</code> pragma or the <strong>-w</strong> switch.</p>
<p>
</p>
<h2><a name="discontinuance__deprecation__and_bugfix_traps">Discontinuance, Deprecation, and BugFix traps</a></h2>
<p>Anything that has been discontinued, deprecated, or fixed as
a bug from perl4.</p>
<ul>
<li><strong><a name="item_symbols_starting_with__22__22_no_longer_forced_int">Symbols starting with &quot;_&quot; no longer forced into main</a></strong>

⌨️ 快捷键说明

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