Common Goofs for Novices

The biggest goof of all is forgetting to use warnings, which identifies many errors. The second biggest goof is forgetting to use strict when it’s appropriate. These two pragmas can save you hours of head-banging when your program starts getting bigger. (And it will.) Yet another faux pas is to forget to consult the online perlfaq. Suppose you want to find out if Perl has a round function. You might try searching the FAQ first by searching with perldoc:

% perldoc –q round

Apart from those “metagoofs”, there are several kinds of programming traps. Some traps almost everyone falls into, and other traps you’ll fall into only if you come from a particular culture that does things differently. We’ve separated these out in the following sections.

Universal Blunders

  • Putting a comma after the filehandle in a print statement. 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 $filehandle is a scalar holding the name of a filehandle at runtime. This is distinct from:

    print $notafilehandle, "goodbye", $adj, "world!\n";

    where $notafilehandle is simply a string that is part of the list of things to be printed. In that case, you might see something like GLOB(0xDEADBEEF)

Get Programming Perl, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.