Daniel Anthony
Daniel Anthony

Reputation: 63

Perl syntax compile error

Here is a bit of code from a program I'm writing, the syntax error lays in the 3rd line.

if($header gt $word{
    $wordlist{$word} = $header;
    $header = $word;
    return;
}

Upvotes: 2

Views: 161

Answers (1)

Leeor
Leeor

Reputation: 19706

In short - you're missing a closing parenthesis on the first line

It's quite funny actually because you'd expect Perl to point you to the right location with its error message. However, due to a stroke of bad luck it seems just like the beginning of a perfectly valid code that just happens to do something else than what you intended.

Perl actually thinks you look up a hash called %word (using $word{...} with the value of the assignment evaluated as key).
So, this would have been a perfectly valid code if you'd have done this:

if ($header gt $word{
    $wordlist{$word} = $header         # removed the ;
                                }) {   # closed the condition
    $header = $word;
    return;
}

Perl is only confused once it reaches the end of the second line and sees the ;

Upvotes: 20

Related Questions