如何在Perl字符串中转义类似代码的东西?

xai*_*ain 5 printing perl heredoc

$ I = 1; while($ i <3){print <<"EOT"; def px $ i = new E(user)if(!px $ i.hasErrors()){println"$ {px $ i.name}/$ {px $ i.empr.to} OK"}

EOT

    $i++;
}
Run Code Online (Sandbox Code Playgroud)

产生错误:

如果没有borrar.pl第3行的包或对象引用,则无法调用方法"px".

如何"逃脱"if?

谢谢.

Sea*_*ean 6

很难说这段代码应该完成什么,但也许你想要将println语句中的外部美元符号保留在最终输出中?

println "\${px$i.name} / \${px$i.empr.to} OK"
Run Code Online (Sandbox Code Playgroud)

有了这个改变,我看到的无错误输出是:

def px1 = new E(user) 
if (!px1.hasErrors()) {
        println "${px1.name} / ${px1.empr.to} OK"
}

def px2 = new E(user) 
if (!px2.hasErrors()) {
        println "${px2.name} / ${px2.empr.to} OK"
}
Run Code Online (Sandbox Code Playgroud)