http://qs1969.pair.com?node_id=1153468

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.
eval { # code that could die }; if ($@) { # Poor man's catch!

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:

eval { # code that could die; 1 } or do {

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:

#!/usr/bin/perl use warnings; use strict; print $], "\n"; { package My::Obj; sub new { my $class = shift; bless {@_}, $class } sub DESTROY { my $self = shift; eval { 1 } if ! $self->{finished}; } } my $o1 = 'My::Obj'->new(finished => 1); undef $o1; # Exception overlooked. eval { my $o2 = 'My::Obj'->new; die "Exception!"; }; if ($@) { warn "Caught with \$\@: $@"; } # Exception details lost. eval { my $o2 = 'My::Obj'->new; die "Exception!"; 1 } or do { warn "Caught with or: $@"; }; # Same as above. use Try::Tiny; try { my $o3 = 'My::Obj'->new; die "Exception!"; } catch { warn "Caught with Try::Tiny: $_"; };

In 5.20, I'm getting:

5.020001 Caught with $@: Exception! at ./eval.pl line 25. Caught with or: Exception! at ./eval.pl line 34. Caught with Try::Tiny: Exception! at ./eval.pl line 44.

But, in 5.12, the first exception gets overlooked. The second syntax at least notices something went wrong, but the exception is lost anyway.

5.012005 Caught with or: at eval.pl line 36. Caught with Try::Tiny: at eval.pl line 46.
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,