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,
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bug in eval in pre-5.14
by Anonymous Monk on Jan 23, 2016 at 22:58 UTC | |
Re: Bug in eval in pre-5.14
by zwon (Abbot) on Jan 25, 2016 at 22:44 UTC | |
by choroba (Cardinal) on Jul 14, 2016 at 15:57 UTC | |
Re: Bug in eval in pre-5.28
by haukex (Archbishop) on Apr 07, 2021 at 11:51 UTC | |
Re: Bug in eval in pre-5.14
by $h4X4_|=73}{ (Monk) on Jun 06, 2016 at 13:32 UTC | |
Re: Bug in eval in pre-5.14
by Anonymous Monk on Feb 10, 2016 at 17:29 UTC | |
Re: Bug in eval in pre-5.14
by $h4X4_|=73}{ (Monk) on Aug 13, 2016 at 15:27 UTC | |
by choroba (Cardinal) on Aug 13, 2016 at 17:59 UTC | |
by $h4X4_|=73}{ (Monk) on Aug 13, 2016 at 22:25 UTC | |
by choroba (Cardinal) on Aug 13, 2016 at 23:11 UTC | |
by $h4X4_|=73}{ (Monk) on Aug 14, 2016 at 10:16 UTC | |
| |
Re: Bug in eval in pre-5.14
by Anonymous Monk on Aug 14, 2016 at 20:22 UTC |