Avoiding Common Oversights in Perl - dummies

Avoiding Common Oversights in Perl

By Paul Hoffman

Entering a typo or two during the course of writing a Perl program is not uncommon. But when you attempt to run a program containing a text-entry slip-up, Perl usually becomes confused and tells you so by reporting an error. The natural reaction for most people, even those with years of programming experience, is to get worried or angry or both when an error message pops up.

Don’t panic. Take a deep breath. Take another slow, deep breath. Seriously, you can’t get to the root of the problem if you’re all tense and bothered. No matter how many years you program, you always end up finding some errors in the code you’re written.

So, now that you are (hopefully!) a bit calmer, you can start to appreciate the fact that Perl has more helpful error messages than almost any other programming language. The messages aren’t always right on the money, but they can get you pretty close to the spot where the problem lies with minimal searching on your part.

Perl has myriad error messages, but a few definitely crop up more than others owing to some common typos that everyone seems to make. The following errors result from minor text-entry goofs that you can easily avoid.

Forgetting a semicolon

Probably the most common error message you see when programming in Perl looks something like this:

# syntax error, near “open”
File ‘counter1.pl’; Line 10
# Execution aborted due to compilation errors.

You can look and look at Line 10, the one with the open statement, and you won’t see anything wrong with it. The trick here is to examine the statement that comes before the open statement and see whether it ends with a semicolon. (Perl knows that a statement ends only when it encounters a semicolon.) In this case, the error is caused by a missing semicolon at the end of Line 7 of the program:

$TheFile = “sample.txt”

Forgetting a quotation mark

The following sort of error message can be extremely frustrating if you don’t know of a quick fix:

# Bare word found where operator expected, near
# “open(INFILE, $TheFile) or die “The”
# (Might be a runaway multi-line ” string starting on
# line 7)
File ‘counter1.pl’; Line 10

This error is similar to forgetting a semicolon; instead, it’s a quotation mark that’s accidentally omitted:

$TheFile = “sample.txt;

In this case, Perl did a good job of guessing what is wrong, suggesting that a runaway multi-line ” string on Line 7 is the problem, which is precisely right.

Entering one parenthesis too many or too few

When you have loads of opening and closing parentheses in a program, it’s easy to slip an extra one in by accident. If that’s the case, you may see a message from Perl that reads something like this:

# syntax error, near “) eq”
File ‘counter1.pl’; Line 38
# syntax error, near “}”
File ‘counter1.pl’; Line 42

Here, Perl can’t determine where the error is exactly, but it actually got it right on the first guess: Line 38 contains an extra right parenthesis:

if(substr($TheLine, $CharPos, 1)) eq ” “)

Having one parenthesis too few in a Perl program can cause harder-to-find problems:

# Can’t use constant item as left arg of implicit —>,
# near “1 }”
File ‘counter1.pl’; Line 39
# Scalar found where operator expected, near “$CharPos”
File ‘counter1.pl’; Line 40
# (Missing semicolon on previous line?)
# syntax error, near “$CharPos “
File ‘counter1.pl’; Line 40

Yarp! All this was produced because the last parenthesis on Line 38 is missing:

if(substr($TheLine, $CharPos, 1) eq ” “

Here is another good lesson in hunting down typing errors: Start where Perl says it found an error. If you don’t find the error there, go up a line or two and see if the problem started earlier.

A final word of advice: Trust Perl to find the simple typos for you (where it can), and remember that it’s giving you all the help it can, which is more than you can say for many programming languages.