in reply to Testing error handling that calls "die"
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';
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Testing error handling that calls "die"
by davies (Monsignor) on Dec 27, 2011 at 10:44 UTC | |
by moritz (Cardinal) on Dec 27, 2011 at 11:11 UTC |