3 perltrap - Perl traps for the unwary
7 The biggest trap of all is forgetting to C<use warnings> or use the B<-w>
8 switch; see L<perllexwarn> and L<perlrun>. The second biggest trap is not
9 making your entire program runnable under C<use strict>. The third biggest
10 trap is not reading the list of changes in this version of Perl; see
15 Accustomed B<awk> users should take special note of the following:
21 A Perl program executes only once, not once for each input line. You can
22 do an implicit loop with C<-n> or C<-p>.
26 The English module, loaded via
30 allows you to refer to special variables (like C<$/>) with names (like
31 $RS), as though they were in B<awk>; see L<perlvar> for details.
35 Semicolons are required after all simple statements in Perl (except
36 at the end of a block). Newline is not a statement delimiter.
40 Curly brackets are required on C<if>s and C<while>s.
44 Variables begin with "$", "@" or "%" in Perl.
48 Arrays index from 0. Likewise string positions in substr() and
53 You have to decide whether your array has numeric or string indices.
57 Hash values do not spring into existence upon mere reference.
61 You have to decide whether you want to use string or numeric
66 Reading an input line does not split it for you. You get to split it
67 to an array yourself. And the split() operator has different
68 arguments than B<awk>'s.
72 The current input line is normally in $_, not $0. It generally does
73 not have the newline stripped. ($0 is the name of the program
74 executed.) See L<perlvar>.
78 $<I<digit>> does not refer to fields--it refers to substrings matched
79 by the last match pattern.
83 The print() statement does not add field and record separators unless
84 you set C<$,> and C<$\>. You can set $OFS and $ORS if you're using
89 You must open your files before you print to them.
93 The range operator is "..", not comma. The comma operator works as in
98 The match operator is "=~", not "~". ("~" is the one's complement
103 The exponentiation operator is "**", not "^". "^" is the XOR
104 operator, as in C. (You know, one could get the feeling that B<awk> is
105 basically incompatible with C.)
109 The concatenation operator is ".", not the null string. (Using the
110 null string would render C</pat/ /pat/> unparsable, because the third slash
111 would be interpreted as a division operator--the tokenizer is in fact
112 slightly context sensitive for operators like "/", "?", and ">".
113 And in fact, "." itself can be the beginning of a number.)
117 The C<next>, C<exit>, and C<continue> keywords work differently.
122 The following variables work differently:
125 ARGC scalar @ARGV (compare with $#ARGV)
129 FS (whatever you like)
130 NF $#Fld, or some such
142 You cannot set $RS to a pattern, only a string.
146 When in doubt, run the B<awk> construct through B<a2p> and see what it
153 Cerebral C and C++ programmers should take note of the following:
159 Curly brackets are required on C<if>'s and C<while>'s.
163 You must use C<elsif> rather than C<else if>.
167 The C<break> and C<continue> keywords from C become in Perl C<last>
168 and C<next>, respectively. Unlike in C, these do I<not> work within a
169 C<do { } while> construct. See L<perlsyn/"Loop Control">.
173 The switch statement is called C<given/when> and only available in
174 perl 5.10 or newer. See L<perlsyn/"Switch Statements">.
178 Variables begin with "$", "@" or "%" in Perl.
182 Comments begin with "#", not "/*" or "//". Perl may interpret C/C++
183 comments as division operators, unterminated regular expressions or
184 the defined-or operator.
188 You can't take the address of anything, although a similar operator
189 in Perl is the backslash, which creates a reference.
193 C<ARGV> must be capitalized. C<$ARGV[0]> is C's C<argv[1]>, and C<argv[0]>
198 System calls such as link(), unlink(), rename(), etc. return nonzero for
199 success, not 0. (system(), however, returns zero for success.)
203 Signal handlers deal with signal names, not numbers. Use C<kill -l>
204 to find their names on your system.
210 Seasoned B<sed> programmers should take note of the following:
216 A Perl program executes only once, not once for each input line. You can
217 do an implicit loop with C<-n> or C<-p>.
221 Backreferences in substitutions use "$" rather than "\".
225 The pattern matching metacharacters "(", ")", and "|" do not have backslashes
230 The range operator is C<...>, rather than comma.
236 Sharp shell programmers should take note of the following:
242 The backtick operator does variable interpolation without regard to
243 the presence of single quotes in the command.
247 The backtick operator does no translation of the return value, unlike B<csh>.
251 Shells (especially B<csh>) do several levels of substitution on each
252 command line. Perl does substitution in only certain constructs
253 such as double quotes, backticks, angle brackets, and search patterns.
257 Shells interpret scripts a little bit at a time. Perl compiles the
258 entire program before executing it (except for C<BEGIN> blocks, which
259 execute at compile time).
263 The arguments are available via @ARGV, not $1, $2, etc.
267 The environment is not automatically made available as separate scalar
272 The shell's C<test> uses "=", "!=", "<" etc for string comparisons and "-eq",
273 "-ne", "-lt" etc for numeric comparisons. This is the reverse of Perl, which
274 uses C<eq>, C<ne>, C<lt> for string comparisons, and C<==>, C<!=> C<< < >> etc
275 for numeric comparisons.
281 Practicing Perl Programmers should take note of the following:
287 Remember that many operations behave differently in a list
288 context than they do in a scalar one. See L<perldata> for details.
292 Avoid barewords if you can, especially all lowercase ones.
293 You can't tell by just looking at it whether a bareword is
294 a function or a string. By using quotes on strings and
295 parentheses on function calls, you won't ever get them confused.
299 You cannot discern from mere inspection which builtins
300 are unary operators (like chop() and chdir())
301 and which are list operators (like print() and unlink()).
302 (Unless prototyped, user-defined subroutines can B<only> be list
303 operators, never unary ones.) See L<perlop> and L<perlsub>.
307 People have a hard time remembering that some functions
308 default to $_, or @ARGV, or whatever, but that others which
309 you might expect to do not.
313 The <FH> construct is not the name of the filehandle, it is a readline
314 operation on that handle. The data read is assigned to $_ only if the
315 file read is the sole condition in a while loop:
318 while (defined($_ = <FH>)) { }..
319 <FH>; # data discarded!
323 Remember not to use C<=> when you need C<=~>;
324 these two constructs are quite different:
331 The C<do {}> construct isn't a real loop that you can use
336 Use C<my()> for local variables whenever you can get away with
337 it (but see L<perlform> for where you can't).
338 Using C<local()> actually gives a local value to a global
339 variable, which leaves you open to unforeseen side-effects
344 If you localize an exported variable in a module, its exported value will
345 not change. The local name becomes an alias to a new value but the
346 external name is still an alias for the original.
350 As always, if any of these are ever officially declared as bugs,
351 they'll be fixed and removed.