I’m thoroughly confused. Here’s my code:
use strict;
use warnings;
use Test::More;
subtest 'huh?' => sub {
my $i = 0;
eval {
$i++;
} || do {
$i++;
};
is($i, 1, "only execute one branch (i: $i)");
};
&done_testing();
And here’s my test output (when run with ActivePerl 5.12, Mac OS X):
not ok 1 - only execute one branch (i: 2)
# Failed test 'only execute one branch (i: 2)'
# at test.pl line 14.
# got: '2'
# expected: '1'
1..1
# Looks like you failed 1 test of 1.
not ok 1 - huh?
# Failed test 'huh?'
# at test.pl line 15.
1..1
# Looks like you failed 1 test of 1.
What is going on here? I expected only the first branch to run since nothing dies. But it looks like both branches are executed.
$i++returns the value of$ibefore increment. So since it’s 0, then it’s false. Andevalreturns the last result evaluated, which is that false value. It then goes on to try the next block (as it should).If you want it only to take the first branch, you need to make the increment expression “increment, then return”, like this
eval { ++$i }.Now, the best way to see if an
evalfailed is not to return 1 but to do the following:$@( or$English::EVAL_ERROR)test
$@