1PERLTRAP(1)            Perl Programmers Reference Guide            PERLTRAP(1)
2
3
4

NAME

6       perltrap - Perl traps for the unwary
7

DESCRIPTION

9       The biggest trap of all is forgetting to "use warnings" or use the -w
10       switch; see warnings and "-w" in perlrun. The second biggest trap is
11       not making your entire program runnable under "use strict".  The third
12       biggest trap is not reading the list of changes in this version of
13       Perl; see perldelta.
14
15   Awk Traps
16       Accustomed awk users should take special note of the following:
17
18       •   A Perl program executes only once, not once for each input line.
19           You can do an implicit loop with "-n" or "-p".
20
21       •   The English module, loaded via
22
23               use English;
24
25           allows you to refer to special variables (like $/) with names (like
26           $RS), as though they were in awk; see perlvar for details.
27
28       •   Semicolons are required after all simple statements in Perl (except
29           at the end of a block).  Newline is not a statement delimiter.
30
31       •   Curly brackets are required on "if"s and "while"s.
32
33       •   Variables begin with "$", "@" or "%" in Perl.
34
35       •   Arrays index from 0.  Likewise string positions in substr() and
36           index().
37
38       •   You have to decide whether your array has numeric or string
39           indices.
40
41       •   Hash values do not spring into existence upon mere reference.
42
43       •   You have to decide whether you want to use string or numeric
44           comparisons.
45
46       •   Reading an input line does not split it for you.  You get to split
47           it to an array yourself.  And the split() operator has different
48           arguments than awk's.
49
50       •   The current input line is normally in $_, not $0.  It generally
51           does not have the newline stripped.  ($0 is the name of the program
52           executed.)  See perlvar.
53
54       •   $<digit> does not refer to fields--it refers to substrings matched
55           by the last match pattern.
56
57       •   The print() statement does not add field and record separators
58           unless you set $, and "$\".  You can set $OFS and $ORS if you're
59           using the English module.
60
61       •   You must open your files before you print to them.
62
63       •   The range operator is "..", not comma.  The comma operator works as
64           in C.
65
66       •   The match operator is "=~", not "~".  ("~" is the one's complement
67           operator, as in C.)
68
69       •   The exponentiation operator is "**", not "^".  "^" is the XOR
70           operator, as in C.  (You know, one could get the feeling that awk
71           is basically incompatible with C.)
72
73       •   The concatenation operator is ".", not the null string.  (Using the
74           null string would render "/pat/ /pat/" unparsable, because the
75           third slash would be interpreted as a division operator--the
76           tokenizer is in fact slightly context sensitive for operators like
77           "/", "?", and ">".  And in fact, "." itself can be the beginning of
78           a number.)
79
80       •   The "next", "exit", and "continue" keywords work differently.
81
82       •   The following variables work differently:
83
84                 Awk       Perl
85                 ARGC      scalar @ARGV (compare with $#ARGV)
86                 ARGV[0]   $0
87                 FILENAME  $ARGV
88                 FNR       $. - something
89                 FS        (whatever you like)
90                 NF        $#Fld, or some such
91                 NR        $.
92                 OFMT      $#
93                 OFS       $,
94                 ORS       $\
95                 RLENGTH   length($&)
96                 RS        $/
97                 RSTART    length($`)
98                 SUBSEP    $;
99
100       •   You cannot set $RS to a pattern, only a string.
101
102       •   When in doubt, run the awk construct through a2p and see what it
103           gives you.
104
105   C/C++ Traps
106       Cerebral C and C++ programmers should take note of the following:
107
108       •   Curly brackets are required on "if"'s and "while"'s.
109
110       •   You must use "elsif" rather than "else if".
111
112       •   The "break" and "continue" keywords from C become in Perl "last"
113           and "next", respectively.  Unlike in C, these do not work within a
114           "do { } while" construct.  See "Loop Control" in perlsyn.
115
116       •   The switch statement is called "given"/"when" and only available in
117           perl 5.10 or newer.  See "Switch Statements" in perlsyn.
118
119       •   Variables begin with "$", "@" or "%" in Perl.
120
121       •   Comments begin with "#", not "/*" or "//".  Perl may interpret
122           C/C++ comments as division operators, unterminated regular
123           expressions or the defined-or operator.
124
125       •   You can't take the address of anything, although a similar operator
126           in Perl is the backslash, which creates a reference.
127
128       •   "ARGV" must be capitalized.  $ARGV[0] is C's "argv[1]", and
129           "argv[0]" ends up in $0.
130
131       •   System calls such as link(), unlink(), rename(), etc. return
132           nonzero for success, not 0. (system(), however, returns zero for
133           success.)
134
135       •   Signal handlers deal with signal names, not numbers.  Use "kill -l"
136           to find their names on your system.
137
138   JavaScript Traps
139       Judicious JavaScript programmers should take note of the following:
140
141       •   In Perl, binary "+" is always addition.  "$string1 + $string2"
142           converts both strings to numbers and then adds them.  To
143           concatenate two strings, use the "." operator.
144
145       •   The "+" unary operator doesn't do anything in Perl.  It exists to
146           avoid syntactic ambiguities.
147
148       •   Unlike "for...in", Perl's "for" (also spelled "foreach") does not
149           allow the left-hand side to be an arbitrary expression.  It must be
150           a variable:
151
152              for my $variable (keys %hash) {
153                   ...
154              }
155
156           Furthermore, don't forget the "keys" in there, as "foreach my $kv
157           (%hash) {}" iterates over the keys and values, and is generally not
158           useful ($kv would be a key, then a value, and so on).
159
160       •   To iterate over the indices of an array, use "foreach my $i (0 ..
161           $#array) {}".  "foreach my $v (@array) {}" iterates over the
162           values.
163
164       •   Perl requires braces following "if", "while", "foreach", etc.
165
166       •   In Perl, "else if" is spelled "elsif".
167
168       •   "? :" has higher precedence than assignment.  In JavaScript, one
169           can write:
170
171               condition ? do_something() : variable = 3
172
173           and the variable is only assigned if the condition is false.  In
174           Perl, you need parentheses:
175
176               $condition ? do_something() : ($variable = 3);
177
178           Or just use "if".
179
180       •   Perl requires semicolons to separate statements.
181
182       •   Variables declared with "my" only affect code after the
183           declaration.  You cannot write "$x = 1; my $x;" and expect the
184           first assignment to affect the same variable.  It will instead
185           assign to an $x declared previously in an outer scope, or to a
186           global variable.
187
188           Note also that the variable is not visible until the following
189           statement.  This means that in "my $x = 1 + $x" the second $x
190           refers to one declared previously.
191
192       •   "my" variables are scoped to the current block, not to the current
193           function.  If you write "{my $x;} $x;", the second $x does not
194           refer to the one declared inside the block.
195
196       •   An object's members cannot be made accessible as variables.  The
197           closest Perl equivalent to "with(object) { method() }" is "for",
198           which can alias $_ to the object:
199
200               for ($object) {
201                   $_->method;
202               }
203
204       •   The object or class on which a method is called is passed as one of
205           the method's arguments, not as a separate "this" value.
206
207   Sed Traps
208       Seasoned sed programmers should take note of the following:
209
210       •   A Perl program executes only once, not once for each input line.
211           You can do an implicit loop with "-n" or "-p".
212
213       •   Backreferences in substitutions use "$" rather than "\".
214
215       •   The pattern matching metacharacters "(", ")", and "|" do not have
216           backslashes in front.
217
218       •   The range operator is "...", rather than comma.
219
220   Shell Traps
221       Sharp shell programmers should take note of the following:
222
223       •   The backtick operator does variable interpolation without regard to
224           the presence of single quotes in the command.
225
226       •   The backtick operator does no translation of the return value,
227           unlike csh.
228
229       •   Shells (especially csh) do several levels of substitution on each
230           command line.  Perl does substitution in only certain constructs
231           such as double quotes, backticks, angle brackets, and search
232           patterns.
233
234       •   Shells interpret scripts a little bit at a time.  Perl compiles the
235           entire program before executing it (except for "BEGIN" blocks,
236           which execute at compile time).
237
238       •   The arguments are available via @ARGV, not $1, $2, etc.
239
240       •   The environment is not automatically made available as separate
241           scalar variables.
242
243       •   The shell's "test" uses "=", "!=", "<" etc for string comparisons
244           and "-eq", "-ne", "-lt" etc for numeric comparisons. This is the
245           reverse of Perl, which uses "eq", "ne", "lt" for string
246           comparisons, and "==", "!=" "<" etc for numeric comparisons.
247
248   Perl Traps
249       Practicing Perl Programmers should take note of the following:
250
251       •   Remember that many operations behave differently in a list context
252           than they do in a scalar one.  See perldata for details.
253
254       •   Avoid barewords if you can, especially all lowercase ones.  You
255           can't tell by just looking at it whether a bareword is a function
256           or a string.  By using quotes on strings and parentheses on
257           function calls, you won't ever get them confused.
258
259       •   You cannot discern from mere inspection which builtins are unary
260           operators (like chop() and chdir()) and which are list operators
261           (like print() and unlink()).  (Unless prototyped, user-defined
262           subroutines can only be list operators, never unary ones.)  See
263           perlop and perlsub.
264
265       •   People have a hard time remembering that some functions default to
266           $_, or @ARGV, or whatever, but that others which you might expect
267           to do not.
268
269       •   The <FH> construct is not the name of the filehandle, it is a
270           readline operation on that handle.  The data read is assigned to $_
271           only if the file read is the sole condition in a while loop:
272
273               while (<FH>)      { }
274               while (defined($_ = <FH>)) { }..
275               <FH>;  # data discarded!
276
277       •   Remember not to use "=" when you need "=~"; these two constructs
278           are quite different:
279
280               $x =  /foo/;
281               $x =~ /foo/;
282
283       •   The "do {}" construct isn't a real loop that you can use loop
284           control on.
285
286       •   Use my() for local variables whenever you can get away with it (but
287           see perlform for where you can't).  Using local() actually gives a
288           local value to a global variable, which leaves you open to
289           unforeseen side-effects of dynamic scoping.
290
291       •   If you localize an exported variable in a module, its exported
292           value will not change.  The local name becomes an alias to a new
293           value but the external name is still an alias for the original.
294
295       As always, if any of these are ever officially declared as bugs,
296       they'll be fixed and removed.
297
298
299
300perl v5.38.2                      2023-11-30                       PERLTRAP(1)
Impressum