|Keep It Simple, Stupid|
Bug in eval in pre-5.14by choroba (Bishop)
|on Jan 23, 2016 at 16:45 UTC||Need Help??|
For some reason, the following pattern is still used in code - I've seen it at a recent interview and in an accepted answer at Stack Overflow.
On pre-5.14 Perls, this is a bug. Sometimes, the code inside the eval could die, but the catch wouldn't be triggered, because the $@ could be clobbered - see perl5140delta for details. If you want to use modules from other people, there's no way how to prevent the bug. If the foreign code calls eval in an object's destructor, you're doomed.
Fortunately, it's rather easy to improve the pattern:
That way, you can really detect the failure in the "try" block, even if the exception could get lost.
Here's a simple example that demonstrates the problem. Note that Try::Tiny doesn't provide more than the "or do" pattern:
In 5.20, I'm getting:
But, in 5.12, the first exception gets overlooked. The second syntax at least notices something went wrong, but the exception is lost anyway.