3 perltrap - Perl traps for the unwary
7 The biggest trap of all is forgetting to use the B<-w> switch;
8 see L<perlrun>. Making your entire program runnable under
12 can help make your program more bullet-proof, but sometimes
13 it's too annoying for quick throw-away programs.
17 Accustomed B<awk> users should take special note of the following:
23 The English module, loaded via
27 allows you to refer to special variables (like $RS) as
28 though they were in B<awk>; see L<perlvar> for details.
32 Semicolons are required after all simple statements in Perl (except
33 at the end of a block). Newline is not a statement delimiter.
37 Curly brackets are required on C<if>s and C<while>s.
41 Variables begin with "$" or "@" in Perl.
45 Arrays index from 0. Likewise string positions in substr() and
50 You have to decide whether your array has numeric or string indices.
54 Associative array values do not spring into existence upon mere
59 You have to decide whether you want to use string or numeric
64 Reading an input line does not split it for you. You get to split it
65 yourself to an array. And split() operator has different
70 The current input line is normally in $_, not $0. It generally does
71 not have the newline stripped. ($0 is the name of the program
72 executed.) See L<perlvar>.
76 $<I<digit>> does not refer to fields--it refers to substrings matched by
77 the last match pattern.
81 The print() statement does not add field and record separators unless
82 you set C<$,> and C<$.>. You can set $OFS and $ORS if you're using
87 You must open your files before you print to them.
91 The range operator is "..", not comma. The comma operator works as in
96 The match operator is "=~", not "~". ("~" is the one's complement
101 The exponentiation operator is "**", not "^". "^" is the XOR
102 operator, as in C. (You know, one could get the feeling that B<awk> is
103 basically incompatible with C.)
107 The concatenation operator is ".", not the null string. (Using the
108 null string would render C</pat/ /pat/> unparsable, since the third slash
109 would be interpreted as a division operator--the tokener is in fact
110 slightly context sensitive for operators like "/", "?", and ">".
111 And in fact, "." itself can be the beginning of a number.)
115 The C<next>, C<exit>, and C<continue> keywords work differently.
120 The following variables work differently:
123 ARGC $#ARGV or scalar @ARGV
127 FS (whatever you like)
128 NF $#Fld, or some such
140 You cannot set $RS to a pattern, only a string.
144 When in doubt, run the B<awk> construct through B<a2p> and see what it
151 Cerebral C programmers should take note of the following:
157 Curly brackets are required on C<if>'s and C<while>'s.
161 You must use C<elsif> rather than C<else if>.
165 The C<break> and C<continue> keywords from C become in
166 Perl C<last> and C<next>, respectively.
167 Unlike in C, these do I<NOT> work within a C<do { } while> construct.
171 There's no switch statement. (But it's easy to build one on the fly.)
175 Variables begin with "$" or "@" in Perl.
179 printf() does not implement the "*" format for interpolating
180 field widths, but it's trivial to use interpolation of double-quoted
181 strings to achieve the same effect.
185 Comments begin with "#", not "/*".
189 You can't take the address of anything, although a similar operator
190 in Perl 5 is the backslash, which creates a reference.
194 C<ARGV> must be capitalized. C<$ARGV[0]> is C's C<argv[1]>, and C<argv[0]>
199 System calls such as link(), unlink(), rename(), etc. return nonzero for
204 Signal handlers deal with signal names, not numbers. Use C<kill -l>
205 to find their names on your system.
211 Seasoned B<sed> programmers should take note of the following:
217 Backreferences in substitutions use "$" rather than "\".
221 The pattern matching metacharacters "(", ")", and "|" do not have backslashes
226 The range operator is C<...>, rather than comma.
232 Sharp shell programmers should take note of the following:
238 The backtick operator does variable interpretation without regard to
239 the presence of single quotes in the command.
243 The backtick operator does no translation of the return value, unlike B<csh>.
247 Shells (especially B<csh>) do several levels of substitution on each
248 command line. Perl does substitution only in certain constructs
249 such as double quotes, backticks, angle brackets, and search patterns.
253 Shells interpret scripts a little bit at a time. Perl compiles the
254 entire program before executing it (except for C<BEGIN> blocks, which
255 execute at compile time).
259 The arguments are available via @ARGV, not $1, $2, etc.
263 The environment is not automatically made available as separate scalar
270 Practicing Perl Programmers should take note of the following:
276 Remember that many operations behave differently in a list
277 context than they do in a scalar one. See L<perldata> for details.
281 Avoid barewords if you can, especially all lower-case ones.
282 You can't tell just by looking at it whether a bareword is
283 a function or a string. By using quotes on strings and
284 parens on function calls, you won't ever get them confused.
288 You cannot discern from mere inspection which built-ins
289 are unary operators (like chop() and chdir())
290 and which are list operators (like print() and unlink()).
291 (User-defined subroutines can B<only> be list operators, never
292 unary ones.) See L<perlop>.
296 People have a hard time remembering that some functions
297 default to $_, or @ARGV, or whatever, but that others which
298 you might expect to do not.
302 The <FH> construct is not the name of the filehandle, it is a readline
303 operation on that handle. The data read is only assigned to $_ if the
304 file read is the sole condition in a while loop:
307 while ($_ = <FH>) { }..
308 <FH>; # data discarded!
312 Remember not to use "C<=>" when you need "C<=~>";
313 these two constructs are quite different:
320 The C<do {}> construct isn't a real loop that you can use
325 Use my() for local variables whenever you can get away with
326 it (but see L<perlform> for where you can't).
327 Using local() actually gives a local value to a global
328 variable, which leaves you open to unforeseen side-effects
335 Penitent Perl 4 Programmers should take note of the following
336 incompatible changes that occurred between release 4 and release 5:
342 C<@> now always interpolates an array in double-quotish strings. Some programs
343 may now need to use backslash to protect any C<@> that shouldn't interpolate.
347 Barewords that used to look like strings to Perl will now look like subroutine
348 calls if a subroutine by that name is defined before the compiler sees them.
351 sub SeeYa { die "Hasta la vista, baby!" }
352 $SIG{'QUIT'} = SeeYa;
354 In Perl 4, that set the signal handler; in Perl 5, it actually calls the
355 function! You may use the B<-w> switch to find such places.
359 Symbols starting with C<_> are no longer forced into package C<main>, except
360 for $_ itself (and @_, etc.).
364 C<s'$lhs'$rhs'> now does no interpolation on either side. It used to
365 interpolate C<$lhs> but not C<$rhs>.
369 The second and third arguments of splice() are now evaluated in scalar
370 context (as the book says) rather than list context.
374 These are now semantic errors because of precedence:
379 Because if that were to work, then this couldn't:
381 sleep $dormancy + 20;
385 The precedence of assignment operators is now the same as the precedence
386 of assignment. Perl 4 mistakenly gave them the precedence of the associated
387 operator. So you now must parenthesize them in expressions like
389 /foo/ ? ($a += 2) : ($a -= 2);
393 /foo/ ? $a += 2 : $a -= 2;
395 would be erroneously parsed as
397 (/foo/ ? $a += 2 : $a) -= 2;
403 now works as a C programmer would expect.
407 C<open FOO || die> is now incorrect. You need parens around the filehandle.
408 While temporarily supported, using such a construct will
409 generate a non-fatal (but non-suppressible) warning.
413 The elements of argument lists for formats are now evaluated in list
414 context. This means you can interpolate list values now.
418 You can't do a C<goto> into a block that is optimized away. Darn.
422 It is no longer syntactically legal to use whitespace as the name
423 of a variable, or as a delimiter for any kind of quote construct.
428 The caller() function now returns a false value in a scalar context if there
429 is no caller. This lets library files determine if they're being required.
433 C<m//g> now attaches its state to the searched string rather than the
438 C<reverse> is no longer allowed as the name of a sort subroutine.
442 B<taintperl> is no longer a separate executable. There is now a B<-T>
443 switch to turn on tainting when it isn't turned on automatically.
447 Double-quoted strings may no longer end with an unescaped C<$> or C<@>.
451 The archaic C<while/if> BLOCK BLOCK syntax is no longer supported.
456 Negative array subscripts now count from the end of the array.
460 The comma operator in a scalar context is now guaranteed to give a
461 scalar context to its arguments.
465 The C<**> operator now binds more tightly than unary minus.
466 It was documented to work this way before, but didn't.
470 Setting C<$#array> lower now discards array elements.
474 delete() is not guaranteed to return the old value for tie()d arrays,
475 since this capability may be onerous for some modules to implement.
479 The construct "this is $$x" used to interpolate the pid at that
480 point, but now tries to dereference $x. C<$$> by itself still
485 Some error messages will be different.
489 Some bugs may have been inadvertently removed.