简体   繁体   中英

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;
}

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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM