next up previous contents
Next: Logic Errors Up: Debugging Perl Previous: Syntax Errors

Common Syntax Errors

One very common error is to use elseif instead of the correct elsif keyword. As you program, you'll find that you consistently make certain kinds of errors. This is okay. Everyone has his or her own little quirks. Mine is that I keep using the assignment operator instead of the equality operator. Just remember what your particular blind spot is. When errors occur, check for your personal common errors first.

This section shows some common syntax errors and the error messages that are generated as a result. First, the error message are shown and then the script that generated it. After the script, we'll see why that particular message was generated.

Scalar found where operator expected at test.pl line 2, near "$bar"

        (Missing semicolon on previous line?)

$foo = { }    # this line is missing a semi-colon.

$bar = 5;

Perl sees the anonymous hash on the first line and is expecting either an operator or the semicolon to follow it. The scalar variable that it finds, $bar, does not fit the syntax of an expression because two variables can't be right after each other. In this case, even though the error message indicates line 2, the problem is in line 1.

Bare word found where operator expected at 
    test.pl line 2, near "print("This"

  (Might be a runaway multi-line "" string starting on line 1)

syntax error at test.pl line 2, near "print("This is "

String found where operator expected at test.pl line 3, near 

"print(""
  (Might be a runaway multi-line "" string starting on line 2)
        (Missing semicolon on previous line?)

Bare word found where operator expected at 
    test.pl line 3, near "print("This"

String found where operator expected at test.pl line 3, at end of 
line
        (Missing operator before ");

?)

Can't find string terminator '"' anywhere before EOF at test.pl 
line 3.


print("This is a test.\n);    # this line is missing a ending 
quote.

print("This is a test.\n");

print("This is a test.\n");

In this example, a missing end quote has generated 12 lines of error messages! You really need to look only at the last one in order to find out that the problem is a missing string terminator. While the last error message describes the problem, it does not tell you where the problem is. For that piece of information, you need to look at the first line where it tells you to look at line two. Of course, by this time you already know that if the error message says line 2, the error is probably in line 1.

Can't call method "a" in empty package "test" at test.pl line 1.


print(This is a test.\n);    # this line is missing a beginning quote.

The error being generated here is very cryptic and has little to do with the actual problem. In order to understand why the message mentions methods and packages, you need to understand the different, arcane ways you can invoke methods when programming with objects. You probably need to add a beginning quote if you ever see this error message.

This list of syntax errors could go on for quite a while, but you probably understand the basic concepts:

Errors are not always located on the line mentioned in the error message. Errors frequently have nothing to do with the error message displayed.


next up previous contents
Next: Logic Errors Up: Debugging Perl Previous: Syntax Errors
dave@cs.cf.ac.uk