嵌套"if",缺少右侧卷曲或方括号

5 perl if-statement

我目前有以下内容:

elsif ($line =~ /^(\s*)(if|elif|else)\s*(.+)*\s*:\s*$/) {
    # Multiline If
    # Print the If/Elif condition
    if ($2 eq "if"){
        print "$1$2 ($3){\n";
    } 
    elsif ($2 eq "elif"){
        print "$1elsif ($3){\n";
    }
    elsif ($2 eq "else"){
        print "$1$2 $3{\n";
    }
    # Add the space before the word "if"/"elif"/"else" to the stack
    push(@indentation_stack, $1);   

}
Run Code Online (Sandbox Code Playgroud)

我收到了规定的错误,但我不确定原因.在最后elsif,如果我在语句\之前添加一个,代码不会产生错误.{print

IE:

elsif ($2 eq "else"){
        print "$1$2 $3\{\n";
    }
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释为什么会发生这种情况吗?

谢谢你的帮助!

ike*_*ami 7

整蛊!问题是以下是哈希查找的开始:

$3{
Run Code Online (Sandbox Code Playgroud)

你想要相当于

$3 . "{"
Run Code Online (Sandbox Code Playgroud)

可以写成

"${3}{"
Run Code Online (Sandbox Code Playgroud)

在这种情况下,以下工作原因是因为它\不可能是变量的一部分:

"$3\{"
Run Code Online (Sandbox Code Playgroud)

但是这个技巧并不总能被使用.例如,考虑一下

$foo . "bar"
Run Code Online (Sandbox Code Playgroud)

如果你试试

"$foo\bar"
Run Code Online (Sandbox Code Playgroud)

你会发现你得到了

$foo . chr(0x08) . "ar"
Run Code Online (Sandbox Code Playgroud)

因为"\b"返回"铃"字符.这让你失望

"${foo}bar"
Run Code Online (Sandbox Code Playgroud)