Universal Blunders
Putting a comma after the filehandle in a
printstatement. Although it looks extremely regular and pretty to say:print STDOUT, "goodbye", $adj, "world!\n"; # WRONG
this is nonetheless incorrect because of that first comma. What you want instead is the indirect object syntax:
print STDOUT "goodbye", $adj, "world!\n"; # ok
The syntax works this way so that you can say:
print $filehandle "goodbye", $adj, "world!\n";
where
$filehandleis a scalar holding the name of a filehandle at runtime. This is distinct from:print $notafilehandle, "goodbye", $adj, "world!\n";
where
$notafilehandleis simply a string that is part of the list of things to be printed. In that case, you might see something likeGLOB(0xDEADBEEF)on the terminal, because the output went to standard output and the filehandle reference stringified itself.See indirect object in the Glossary.
Using
==instead ofeqand!=instead ofne. The==and!=operators are numeric tests. The other two are string tests. The strings"123"and"123.00"are equal as numbers but not equal as strings. Also, most nonnumeric strings are numerically equal to zero, and some of them, such as"123xyz", probably aren’t what you want in a numeric context. Unless you are dealing with numbers, you almost always want the string-comparison operators instead. Thewarningspragma will tell you when these operators use nonnumeric data.Forgetting the trailing semicolon. Every statement in Perl is terminated by a semicolon or the end of a block. ...
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