perltrap.html
来自「perl教程」· HTML 代码 · 共 1,076 行 · 第 1/5 页
HTML
1,076 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perltrap - Perl traps for the unwary</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perltrap - Perl traps for the unwary</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#awk_traps">Awk Traps</a></li>
<li><a href="#c_c___traps">C/C++ Traps</a></li>
<li><a href="#sed_traps">Sed Traps</a></li>
<li><a href="#shell_traps">Shell Traps</a></li>
<li><a href="#perl_traps">Perl Traps</a></li>
<li><a href="#perl4_to_perl5_traps">Perl4 to Perl5 Traps</a></li>
<li><a href="#discontinuance__deprecation__and_bugfix_traps">Discontinuance, Deprecation, and BugFix traps</a></li>
<li><a href="#parsing_traps">Parsing Traps</a></li>
<li><a href="#numerical_traps">Numerical Traps</a></li>
<li><a href="#general_data_type_traps">General data type traps</a></li>
<li><a href="#context_traps__scalar__list_contexts">Context Traps - scalar, list contexts</a></li>
<li><a href="#precedence_traps">Precedence Traps</a></li>
<li><a href="#general_regular_expression_traps_using_s_____etc_">General Regular Expression Traps using s///, etc.</a></li>
<li><a href="#subroutine__signal__sorting_traps">Subroutine, Signal, Sorting Traps</a></li>
<li><a href="#os_traps">OS Traps</a></li>
<li><a href="#interpolation_traps">Interpolation Traps</a></li>
<li><a href="#dbm_traps">DBM Traps</a></li>
<li><a href="#unclassified_traps">Unclassified Traps</a></li>
</ul>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perltrap - Perl traps for the unwary</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The biggest trap of all is forgetting to <code>use warnings</code> or use the <strong>-w</strong>
switch; see <a href="../../lib/Pod/perllexwarn.html">the perllexwarn manpage</a> and <a href="../../lib/Pod/perlrun.html">the perlrun manpage</a>. The second biggest trap is not
making your entire program runnable under <code>use strict</code>. The third biggest
trap is not reading the list of changes in this version of Perl; see
<a href="../../lib/Pod/perldelta.html">the perldelta manpage</a>.</p>
<p>
</p>
<h2><a name="awk_traps">Awk Traps</a></h2>
<p>Accustomed <strong>awk</strong> users should take special 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>The English module, loaded via</p>
<pre>
<span class="keyword">use</span> <span class="variable">English</span><span class="operator">;</span>
</pre>
<p>allows you to refer to special variables (like <a href="../../lib/Pod/perlvar.html#item___"><code>$/</code></a>) with names (like
$RS), as though they were in <strong>awk</strong>; see <a href="../../lib/Pod/perlvar.html">the perlvar manpage</a> for details.</p>
</li>
<li>
<p>Semicolons are required after all simple statements in Perl (except
at the end of a block). Newline is not a statement delimiter.</p>
</li>
<li>
<p>Curly brackets are required on <code>if</code>s and <code>while</code>s.</p>
</li>
<li>
<p>Variables begin with "$", "@" or "%" in Perl.</p>
</li>
<li>
<p>Arrays index from 0. Likewise string positions in <a href="../../lib/Pod/perlvar.html#item_substr"><code>substr()</code></a> and
index().</p>
</li>
<li>
<p>You have to decide whether your array has numeric or string indices.</p>
</li>
<li>
<p>Hash values do not spring into existence upon mere reference.</p>
</li>
<li>
<p>You have to decide whether you want to use string or numeric
comparisons.</p>
</li>
<li>
<p>Reading an input line does not split it for you. You get to split it
to an array yourself. And the <a href="#item_split"><code>split()</code></a> operator has different
arguments than <strong>awk</strong>'s.</p>
</li>
<li>
<p>The current input line is normally in $_, not $0. It generally does
not have the newline stripped. ($0 is the name of the program
executed.) See <a href="../../lib/Pod/perlvar.html">the perlvar manpage</a>.</p>
</li>
<li>
<p>$<<em>digit</em>> does not refer to fields--it refers to substrings matched
by the last match pattern.</p>
</li>
<li>
<p>The <a href="../../lib/Pod/perlfunc.html#item_print"><code>print()</code></a> statement does not add field and record separators unless
you set <a href="../../lib/Pod/perlvar.html#item___"><code>$,</code></a> and <a href="../../lib/Pod/perlvar.html#item___"><code>$\</code></a>. You can set $OFS and $ORS if you're using
the English module.</p>
</li>
<li>
<p>You must open your files before you print to them.</p>
</li>
<li>
<p>The range operator is "..", not comma. The comma operator works as in
C.</p>
</li>
<li>
<p>The match operator is "=~", not "~". ("~" is the one's complement
operator, as in C.)</p>
</li>
<li>
<p>The exponentiation operator is "**", not "^". "^" is the XOR
operator, as in C. (You know, one could get the feeling that <strong>awk</strong> is
basically incompatible with C.)</p>
</li>
<li>
<p>The concatenation operator is ".", not the null string. (Using the
null string would render <code>/pat/ /pat/</code> unparsable, because the third slash
would be interpreted as a division operator--the tokenizer is in fact
slightly context sensitive for operators like "/", "?", and ">".
And in fact, "." itself can be the beginning of a number.)</p>
</li>
<li>
<p>The <a href="../../lib/Pod/perlfunc.html#item_next"><code>next</code></a>, <a href="../../lib/Pod/perlfunc.html#item_exit"><code>exit</code></a>, and <a href="../../lib/Pod/perlfunc.html#item_continue"><code>continue</code></a> keywords work differently.</p>
</li>
<li>
<p>The following variables work differently:</p>
<pre>
<span class="variable">Awk</span> <span class="variable">Perl</span>
<span class="variable">ARGC</span> <span class="keyword">scalar</span> <span class="variable">@ARGV</span> <span class="operator">(</span><span class="variable">compare</span> <span class="variable">with</span> <span class="variable">$#ARGV</span><span class="operator">)</span>
<span class="variable">ARGV</span><span class="operator">[</span><span class="number">0</span><span class="operator">]</span> <span class="variable">$0</span>
<span class="variable">FILENAME</span> <span class="variable">$ARGV</span>
<span class="variable">FNR</span> <span class="variable">$.</span> <span class="operator">-</span> <span class="variable">something</span>
<span class="variable">FS</span> <span class="operator">(</span><span class="variable">whatever</span> <span class="variable">you</span> <span class="variable">like</span><span class="operator">)</span>
<span class="variable">NF</span> <span class="variable">$#Fld</span><span class="operator">,</span> <span class="keyword">or</span> <span class="variable">some</span> <span class="variable">such</span>
<span class="variable">NR</span> <span class="variable">$.</span>
<span class="variable">OFMT</span> <span class="variable">$#
OFS</span> <span class="variable">$,</span>
<span class="variable">ORS</span> <span class="variable">$\</span>
<span class="variable">RLENGTH</span> <span class="keyword">length</span><span class="operator">(</span><span class="variable">$&</span><span class="operator">)</span>
<span class="variable">RS</span> <span class="variable">$/</span>
<span class="variable">RSTART</span> <span class="keyword">length</span><span class="operator">(</span><span class="variable">$`</span><span class="operator">)</span>
<span class="variable">SUBSEP</span> <span class="variable">$;</span>
</pre>
</li>
<li>
<p>You cannot set $RS to a pattern, only a string.</p>
</li>
<li>
<p>When in doubt, run the <strong>awk</strong> construct through <strong>a2p</strong> and see what it
gives you.</p>
</li>
</ul>
<p>
</p>
<h2><a name="c_c___traps">C/C++ Traps</a></h2>
<p>Cerebral C and C++ programmers should take note of the following:</p>
<ul>
<li>
<p>Curly brackets are required on <code>if</code>'s and <code>while</code>'s.</p>
</li>
<li>
<p>You must use <code>elsif</code> rather than <code>else if</code>.</p>
</li>
<li>
<p>The <code>break</code> and <a href="../../lib/Pod/perlfunc.html#item_continue"><code>continue</code></a> keywords from C become in Perl <a href="../../lib/Pod/perlfunc.html#item_last"><code>last</code></a>
and <a href="../../lib/Pod/perlfunc.html#item_next"><code>next</code></a>, respectively. Unlike in C, these do <em>not</em> work within a
<code>do { } while</code> construct. See <a href="../../lib/Pod/perlsyn.html#loop_control">Loop Control in the perlsyn manpage</a>.</p>
</li>
<li>
<p>There's no switch statement. (But it's easy to build one on the fly,
see <a href="../../lib/Pod/perlsyn.html#basic_blocks_and_switch_statements">Basic BLOCKs and Switch Statements in the perlsyn manpage</a>)</p>
</li>
<li>
<p>Variables begin with "$", "@" or "%" in Perl.</p>
</li>
<li>
<p>Comments begin with "#", not "/*" or "//". Perl may interpret C/C++
comments as division operators, unterminated regular expressions or
the defined-or operator.</p>
</li>
<li>
<p>You can't take the address of anything, although a similar operator
in Perl is the backslash, which creates a reference.</p>
</li>
<li>
<p><a href="../../lib/Pod/perlvar.html#item_argv"><code>ARGV</code></a> must be capitalized. <code>$ARGV[0]</code> is C's <code>argv[1]</code>, and <code>argv[0]</code>
ends up in <a href="../../lib/Pod/perlvar.html#item__0"><code>$0</code></a>.</p>
</li>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?