in reply to eval: Why use the 'Zombie error' idiom?
The pattern I am using in the WARC test suite is to set a lexical before entering the eval and to change its value after the code that is expected to throw an exception, like so:
{ my $fail = 0; eval {new WARC::Fields (WARC_Type => 'warcinfo', BOGUS => undef); $fail = 1;}; ok($fail == 0 && $@ =~ m/key without value/, 'reject construction with missing value'); $fail = 0; eval {new WARC::Fields (WARC_Type => 'warcinfo', '::Bogus' => 1); $fail = 1;}; ok($fail == 0 && $@ =~ m/invalid field name/, 'reject construction with field name with actual leading colon' +); # more tests elided... }
This depends on what you are trying to verify; in the code above, the expected result is "execution does not continue past the bad call", but the same approach can be used to ensure that execution reaches the end of an eval block.
Back on topic, 'Zombie error' is an example of defensive programming: if the error handling do is reached, the eval must not have reached the end, because the last expression is a true constant, so there should be an error in $@. If $@ is empty, at least two things are wrong: the eval did not complete, and no reasonable error was produced.
|
|---|