当我尝试使用嵌套的 given/when 语句时,为什么会出现“Bad name after test3'”错误?

unc*_*eat -3 perl switch-statement

使用 Perl v5.10 我试图嵌套given语句 - 即在满足given初始when情况之一时调用语句。例子:

my $value = "test3";
my $subvalue = "subtest2";
my $content .= "\nvalue:";

given($value) {
    when ('test1') { $content .= "test1" }
    when ('test2 ) { $content .= "test2" }
    when ('test3') {
        given($subvalue) {
            when ('subtest1') { $content .= "subtest1" }
            when ('subtest2') { $content .= "subtest2" }
            when ('subtest3') { $content .= "subtest3" }
            when ('subtest4') { $content .= "subtest4" }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

test3' 后的名称错误,位于 ....

是否可以given在 Perl v5.10 中嵌套语句?

yst*_*sth 5

从错误中,我怀疑您正在运行的代码不是您向我们展示的代码,并且在某处缺少 end- '。Perl 将'之前的 test3 视为结束引号和test3'限定标识符的开头('是旧的 perl4 说法::),但没有标识符的其余部分。

$ echo 'Bad name after '|splain
Bad name after  (#1)
    (F) You started to name a symbol by using a package prefix, and then
    didn't finish the symbol.  In particular, you can't interpolate outside
    of quotes, so

        $var = 'myvar';
        $sym = mypack::$var;

    is not the same as

        $var = 'myvar';
        $sym = "mypack::$var";
Run Code Online (Sandbox Code Playgroud)

splain/use diagnostics/perldoc perldiag 可以成为您破译奇怪错误的朋友。