为什么在没有块的情况下调用eval会终止这个Perl程序?

时间:2016-11-19 12:54:42

标签: perl eval

以下程序在第二次调用eval时停止。这是预期的吗?我读了perldoc -f eval,并且对于没有显示打印“2:...”的输出感到惊讶。

eval {die("The curly braces seem to rescue me! Life moves on")};
print "1: $@\n";
eval die("Program actually terminates here! Subsequent prints are not shown");
print "2: $@\n";

1 个答案:

答案 0 :(得分:5)

perldoc -f eval

eval EXPR
eval BLOCK
eval    In the first form, often referred to as a "string eval", the
        return value of EXPR is parsed and executed as if it were a little
        Perl program.

...所以调用die然后评估它返回的字符串。

...除了它不返回字符串。它使程序死亡,因此永远不会调用eval