|
Practicing Perl Programmers should take note of the following:
- Remember that many operations behave differently in a list context than they do in a
scalar one. See perldata
for details.
- Avoid barewords if you can, especially all lowercase ones. You can't tell by just looking
at it whether a bareword is a function or a string. By using quotes on strings and
parentheses on function calls, you won't ever get them confused.
- You cannot discern from mere inspection which builtins are unary operators (like chop()
and chdir()) and which are list operators (like print() and unlink()). (Unless prototyped,
user-defined subroutines can only be list operators, never unary ones.) See perlop and perlsub.
- People have a hard time remembering that some functions default to $_, or @ARGV, or
whatever, but that others which you might expect to do not.
-
The <FH> construct is not the name of the filehandle, it is a readline operation on
that handle. The data read is assigned to $_ only if the file read is the sole condition in
a while loop:
while (<FH>) { }
while (defined($_ = <FH>)) { }..
<FH>; # data discarded!
|
|
-
Remember not to use = when you need =~; these two constructs
are quite different:
- The
do {} construct isn't a real loop that you can use loop control on.
- Use
my() for local variables whenever you can get away with it (but see perlform for where you can't).
Using local() actually gives a local value to a global variable, which leaves
you open to unforeseen side-effects of dynamic scoping.
- If you localize an exported variable in a module, its exported value will not change. The
local name becomes an alias to a new value but the external name is still an alias for the
original.
|
|