Frequently Ignored Advice
Practicing Perl Programmers should take note of the following:
Remember that many operations behave differently in list context than they do in a scalar one, or that a list and an array are not the same thing. For instance:
($x) = (4, 5, 6); # List context; $x is set to 4 $x = (4, 5, 6); # Scalar context; $x is set to 6 @a = (4, 5, 6); $x = @a; # Scalar context; $x is set to 3 (the array length)
Avoid barewords if you can, especially all lowercase ones. You can’t tell just by looking at it whether a word is a function or a bareword string. By using quotes on strings and parentheses around function call arguments, you won’t ever get them confused. In fact, the pragma
use strictat the beginning of your program makes barewords a compile-time error—probably a good thing.You can’t tell just by looking which built-in functions are unary operators (like
chopandchdir), which are list operators (likeprintandunlink), and which are argumentless (liketime). You’ll want to learn them by reading Chapter 27. As always, use parentheses if you aren’t sure—or even if you aren’t sure you’re sure. Note also that user-defined subroutines are by default list operators, but they can be declared as unary operators with a prototype of($)or argumentless with a prototype of().People have a hard time remembering that some functions default to
$_,@ARGV, or whatever, while others do not. Take the time to learn which are which or avoid default arguments.<FH>is not the name of ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access