This is patch.2b1f to perl5.002beta1.
[p5sagit/p5-mst-13.2.git] / pod / perltrap.pod
CommitLineData
a0d0e21e 1=head1 NAME
2
3perltrap - Perl traps for the unwary
4
5=head1 DESCRIPTION
6
7The biggest trap of all is forgetting to use the B<-w> switch;
8see L<perlrun>. Making your entire program runnable under
9
10 use strict;
11
12can help make your program more bullet-proof, but sometimes
13it's too annoying for quick throw-away programs.
14
15=head2 Awk Traps
16
17Accustomed B<awk> users should take special note of the following:
18
19=over 4
20
21=item *
22
23The English module, loaded via
24
25 use English;
26
27allows you to refer to special variables (like $RS) as
28though they were in B<awk>; see L<perlvar> for details.
29
30=item *
31
32Semicolons are required after all simple statements in Perl (except
33at the end of a block). Newline is not a statement delimiter.
34
35=item *
36
37Curly brackets are required on C<if>s and C<while>s.
38
39=item *
40
41Variables begin with "$" or "@" in Perl.
42
43=item *
44
45Arrays index from 0. Likewise string positions in substr() and
46index().
47
48=item *
49
50You have to decide whether your array has numeric or string indices.
51
52=item *
53
54Associative array values do not spring into existence upon mere
55reference.
56
57=item *
58
59You have to decide whether you want to use string or numeric
60comparisons.
61
62=item *
63
64Reading an input line does not split it for you. You get to split it
65yourself to an array. And split() operator has different
66arguments.
67
68=item *
69
70The current input line is normally in $_, not $0. It generally does
71not have the newline stripped. ($0 is the name of the program
72executed.) See L<perlvar>.
73
74=item *
75
76$<I<digit>> does not refer to fields--it refers to substrings matched by
77the last match pattern.
78
79=item *
80
81The print() statement does not add field and record separators unless
82you set C<$,> and C<$.>. You can set $OFS and $ORS if you're using
83the English module.
84
85=item *
86
87You must open your files before you print to them.
88
89=item *
90
91The range operator is "..", not comma. The comma operator works as in
92C.
93
94=item *
95
96The match operator is "=~", not "~". ("~" is the one's complement
97operator, as in C.)
98
99=item *
100
101The exponentiation operator is "**", not "^". "^" is the XOR
102operator, as in C. (You know, one could get the feeling that B<awk> is
103basically incompatible with C.)
104
105=item *
106
107The concatenation operator is ".", not the null string. (Using the
108null string would render C</pat/ /pat/> unparsable, since the third slash
109would be interpreted as a division operator--the tokener is in fact
110slightly context sensitive for operators like "/", "?", and ">".
111And in fact, "." itself can be the beginning of a number.)
112
113=item *
114
115The C<next>, C<exit>, and C<continue> keywords work differently.
116
117=item *
118
119
120The following variables work differently:
121
122 Awk Perl
123 ARGC $#ARGV or scalar @ARGV
124 ARGV[0] $0
125 FILENAME $ARGV
126 FNR $. - something
127 FS (whatever you like)
128 NF $#Fld, or some such
129 NR $.
130 OFMT $#
131 OFS $,
132 ORS $\
133 RLENGTH length($&)
134 RS $/
135 RSTART length($`)
136 SUBSEP $;
137
138=item *
139
140You cannot set $RS to a pattern, only a string.
141
142=item *
143
144When in doubt, run the B<awk> construct through B<a2p> and see what it
145gives you.
146
147=back
148
149=head2 C Traps
150
151Cerebral C programmers should take note of the following:
152
153=over 4
154
155=item *
156
157Curly brackets are required on C<if>'s and C<while>'s.
158
159=item *
160
161You must use C<elsif> rather than C<else if>.
162
163=item *
164
165The C<break> and C<continue> keywords from C become in
166Perl C<last> and C<next>, respectively.
167Unlike in C, these do I<NOT> work within a C<do { } while> construct.
168
169=item *
170
171There's no switch statement. (But it's easy to build one on the fly.)
172
173=item *
174
175Variables begin with "$" or "@" in Perl.
176
177=item *
178
179printf() does not implement the "*" format for interpolating
180field widths, but it's trivial to use interpolation of double-quoted
181strings to achieve the same effect.
182
183=item *
184
185Comments begin with "#", not "/*".
186
187=item *
188
189You can't take the address of anything, although a similar operator
190in Perl 5 is the backslash, which creates a reference.
191
192=item *
193
4633a7c4 194C<ARGV> must be capitalized. C<$ARGV[0]> is C's C<argv[1]>, and C<argv[0]>
195ends up in C<$0>.
a0d0e21e 196
197=item *
198
199System calls such as link(), unlink(), rename(), etc. return nonzero for
200success, not 0.
201
202=item *
203
204Signal handlers deal with signal names, not numbers. Use C<kill -l>
205to find their names on your system.
206
207=back
208
209=head2 Sed Traps
210
211Seasoned B<sed> programmers should take note of the following:
212
213=over 4
214
215=item *
216
217Backreferences in substitutions use "$" rather than "\".
218
219=item *
220
221The pattern matching metacharacters "(", ")", and "|" do not have backslashes
222in front.
223
224=item *
225
226The range operator is C<...>, rather than comma.
227
228=back
229
230=head2 Shell Traps
231
232Sharp shell programmers should take note of the following:
233
234=over 4
235
236=item *
237
238The backtick operator does variable interpretation without regard to
239the presence of single quotes in the command.
240
241=item *
242
243The backtick operator does no translation of the return value, unlike B<csh>.
244
245=item *
246
247Shells (especially B<csh>) do several levels of substitution on each
248command line. Perl does substitution only in certain constructs
249such as double quotes, backticks, angle brackets, and search patterns.
250
251=item *
252
253Shells interpret scripts a little bit at a time. Perl compiles the
254entire program before executing it (except for C<BEGIN> blocks, which
255execute at compile time).
256
257=item *
258
259The arguments are available via @ARGV, not $1, $2, etc.
260
261=item *
262
263The environment is not automatically made available as separate scalar
264variables.
265
266=back
267
268=head2 Perl Traps
269
270Practicing Perl Programmers should take note of the following:
271
272=over 4
273
274=item *
275
276Remember that many operations behave differently in a list
277context than they do in a scalar one. See L<perldata> for details.
278
279=item *
280
281Avoid barewords if you can, especially all lower-case ones.
282You can't tell just by looking at it whether a bareword is
283a function or a string. By using quotes on strings and
284parens on function calls, you won't ever get them confused.
285
286=item *
287
288You cannot discern from mere inspection which built-ins
289are unary operators (like chop() and chdir())
290and which are list operators (like print() and unlink()).
291(User-defined subroutines can B<only> be list operators, never
292unary ones.) See L<perlop>.
293
294=item *
295
748a9306 296People have a hard time remembering that some functions
a0d0e21e 297default to $_, or @ARGV, or whatever, but that others which
298you might expect to do not.
299
300=item *
301
748a9306 302The <FH> construct is not the name of the filehandle, it is a readline
303operation on that handle. The data read is only assigned to $_ if the
304file read is the sole condition in a while loop:
305
306 while (<FH>) { }
307 while ($_ = <FH>) { }..
308 <FH>; # data discarded!
309
310=item *
311
a0d0e21e 312Remember not to use "C<=>" when you need "C<=~>";
313these two constructs are quite different:
314
315 $x = /foo/;
316 $x =~ /foo/;
317
318=item *
319
320The C<do {}> construct isn't a real loop that you can use
321loop control on.
322
323=item *
324
325Use my() for local variables whenever you can get away with
326it (but see L<perlform> for where you can't).
327Using local() actually gives a local value to a global
328variable, which leaves you open to unforeseen side-effects
329of dynamic scoping.
330
331=back
332
333=head2 Perl4 Traps
334
335Penitent Perl 4 Programmers should take note of the following
336incompatible changes that occurred between release 4 and release 5:
337
338=over 4
339
340=item *
341
342C<@> now always interpolates an array in double-quotish strings. Some programs
343may now need to use backslash to protect any C<@> that shouldn't interpolate.
344
345=item *
748a9306 346
a0d0e21e 347Barewords that used to look like strings to Perl will now look like subroutine
348calls if a subroutine by that name is defined before the compiler sees them.
349For example:
350
351 sub SeeYa { die "Hasta la vista, baby!" }
748a9306 352 $SIG{'QUIT'} = SeeYa;
a0d0e21e 353
354In Perl 4, that set the signal handler; in Perl 5, it actually calls the
355function! You may use the B<-w> switch to find such places.
356
357=item *
358
359Symbols starting with C<_> are no longer forced into package C<main>, except
360for $_ itself (and @_, etc.).
361
362=item *
363
364C<s'$lhs'$rhs'> now does no interpolation on either side. It used to
365interpolate C<$lhs> but not C<$rhs>.
366
367=item *
368
369The second and third arguments of splice() are now evaluated in scalar
370context (as the book says) rather than list context.
371
372=item *
373
374These are now semantic errors because of precedence:
375
376 shift @list + 20;
377 $n = keys %map + 20;
378
379Because if that were to work, then this couldn't:
380
381 sleep $dormancy + 20;
382
383=item *
384
4633a7c4 385The precedence of assignment operators is now the same as the precedence
386of assignment. Perl 4 mistakenly gave them the precedence of the associated
387operator. So you now must parenthesize them in expressions like
388
389 /foo/ ? ($a += 2) : ($a -= 2);
390
391Otherwise
392
393 /foo/ ? $a += 2 : $a -= 2;
394
395would be erroneously parsed as
396
397 (/foo/ ? $a += 2 : $a) -= 2;
398
399On the other hand,
400
401 $a += /foo/ ? 1 : 2;
402
403now works as a C programmer would expect.
404
405=item *
406
a0d0e21e 407C<open FOO || die> is now incorrect. You need parens around the filehandle.
408While temporarily supported, using such a construct will
409generate a non-fatal (but non-suppressible) warning.
410
411=item *
412
413The elements of argument lists for formats are now evaluated in list
414context. This means you can interpolate list values now.
415
416=item *
417
418You can't do a C<goto> into a block that is optimized away. Darn.
419
420=item *
421
422It is no longer syntactically legal to use whitespace as the name
423of a variable, or as a delimiter for any kind of quote construct.
424Double darn.
425
426=item *
427
428The caller() function now returns a false value in a scalar context if there
429is no caller. This lets library files determine if they're being required.
430
431=item *
432
433C<m//g> now attaches its state to the searched string rather than the
434regular expression.
435
436=item *
437
438C<reverse> is no longer allowed as the name of a sort subroutine.
439
440=item *
441
442B<taintperl> is no longer a separate executable. There is now a B<-T>
443switch to turn on tainting when it isn't turned on automatically.
444
445=item *
446
447Double-quoted strings may no longer end with an unescaped C<$> or C<@>.
448
449=item *
450
451The archaic C<while/if> BLOCK BLOCK syntax is no longer supported.
452
453
454=item *
455
456Negative array subscripts now count from the end of the array.
457
458=item *
459
460The comma operator in a scalar context is now guaranteed to give a
461scalar context to its arguments.
462
463=item *
464
465The C<**> operator now binds more tightly than unary minus.
466It was documented to work this way before, but didn't.
467
468=item *
469
470Setting C<$#array> lower now discards array elements.
471
472=item *
473
474delete() is not guaranteed to return the old value for tie()d arrays,
475since this capability may be onerous for some modules to implement.
476
477=item *
478
748a9306 479The construct "this is $$x" used to interpolate the pid at that
480point, but now tries to dereference $x. C<$$> by itself still
481works fine, however.
482
483=item *
484
a0d0e21e 485Some error messages will be different.
486
487=item *
488
489Some bugs may have been inadvertently removed.
490
491=back