my $errmsg = eval(SubCall($silly));
There are two use cases of eval: eval BLOCK catches exceptions, and eval EXPR takes a string as the source of a Perl program, and executes it.
You've accidentally used the latter, but you should be using the former. If you write eval(SubCall($silly)), the call to SubCall is not influenced by the eval at all, but rather the return value of the SubCall will be used as a string to be interpreted as Perl code.
The correct solution is to just use the BLOCK form of eval:
my $success = eval { SubCall($silly); 1 }; ok !$success && $@ eq $errmsg, 'died with the right error message';
In Perl 6, the eval BLOCK form has been renamed to try, so there's less potential for confusion. It also works as a statement prefix without any block, so you could write
my $success = try SubCall($silly); ok $!.defined && $! eq $errmsg, 'died with the right error message';
In reply to Re: Testing error handling that calls "die"
by moritz
in thread Testing error handling that calls "die"
by davies
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |