$error = $@;
}
+ # set up a scope guard to invoke the finally block at the end
+ my $guard = $finally && bless \$finally, "Try::Tiny::ScopeGuard";
+
# at this point $failed contains a true value if the eval died, even if some
# destructor overwrote $@ as the eval was unwinding.
if ( $failed ) {
# This works like given($error), but is backwards compatible and
# sets $_ in the dynamic scope for the body of C<$catch>
for ($error) {
- my $catch_return = $catch->($error);
-
- # Finally blocks run after all other blocks so it is executed here
- $finally->() if ( $finally );
-
- #And return whatever catch returned
- return $catch_return;
+ return $catch->($error);
}
# in case when() was used without an explicit return, the C<for>
return;
} else {
- # Execute finally block once we decided we worked
- $finally->() if ( $finally );
-
# no failure, $@ is back to what it was, everything is fine
return $wantarray ? @ret : $ret[0];
}
);
}
+sub Try::Tiny::ScopeGuard::DESTROY {
+ my $self = shift;
+ $$self->();
+}
+
__PACKAGE__
__END__
use strict;
#use warnings;
-use Test::More tests => 24;
+use Test::More tests => 26;
BEGIN { use_ok 'Try::Tiny' };
is( $@, "magic", '$@ untouched' );
}
-is( scalar(try { "foo", "bar", "gorch" }), "gorch", "scalar context" );
-is_deeply( [ try {qw(foo bar gorch)} ], [qw(foo bar gorch)], "list context" );
+is( scalar(try { "foo", "bar", "gorch" }), "gorch", "scalar context try" );
+is_deeply( [ try {qw(foo bar gorch)} ], [qw(foo bar gorch)], "list context try" );
+
+is( scalar(try { die } catch { "foo", "bar", "gorch" }), "gorch", "scalar context catch" );
+is_deeply( [ try { die } catch {qw(foo bar gorch)} ], [qw(foo bar gorch)], "list context catch" );
+
{
my ($sub) = catch { my $a = $_; };
use strict;
#use warnings;
-use Test::More tests => 7;
+use Test::More tests => 8;
BEGIN { use_ok 'Try::Tiny' };