Ltf4an Ltf4an - 1 year ago 71
Perl Question

Why does calling eval without a block terminate this Perl program?

The following program stops at the 2nd call to eval. Is that expected? I read perldoc -f eval and am surprised that the output of print "2: ..." is not displayed.

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";

Answer Source

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.

… so die is called and then the string it returns is evaluated.

… except it doesn't return a string. It makes the program die so eval is never called.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download