Perl语法编译错误

Dan*_*ony 2 syntax perl associative-array compiler-errors

这是我正在编写的程序的一些代码,语法错误位于第3行.

if($header gt $word{
    $wordlist{$word} = $header;
    $header = $word;
    return;
}
Run Code Online (Sandbox Code Playgroud)

Lee*_*eor 20

简而言之 - 你在第一行错过了一个右括号

实际上这很有趣,因为你希望Perl用正确的位置指出你的错误信息.然而,由于运气不好,这似乎就像一个完全有效的代码的开始,只是碰巧做了别的事情而不是你的意图.

Perl实际上认为你查找一个调用的哈希%word(使用$word{...}赋值的值作为键).
所以,如果您已经这样做,这将是一个非常有效的代码:

if ($header gt $word{
    $wordlist{$word} = $header         # removed the ;
                                }) {   # closed the condition
    $header = $word;
    return;
}
Run Code Online (Sandbox Code Playgroud)

Perl只有在到达第二行的末尾并且看到它时才会感到困惑 ;

  • 好答案.`use strict`使得Perl对`$ word {`构造的思考更加清晰. (3认同)