Skip to content

Commit

Permalink
Fix clobbering $@ in catch block
Browse files Browse the repository at this point in the history
Fixes failing test which was added in previous commit.

t/basic.t ...................... 1/36
   Failed test '$@ untouched'
   at t/basic.t line 222.
          got: 'another magic'
     expected: 'magic'
 Looks like you failed 1 test of 36.
  • Loading branch information
pali committed Jan 14, 2018
1 parent 2817631 commit 4f0f14b
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/Try/Tiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,28 @@ sub try (&;@) {
# This works like given($error), but is backwards compatible and
# sets $_ in the dynamic scope for the body of C<$catch>
for ($error) {
return $catch->($error);
my $failed = not eval {
$@ = $prev_error;
if ( $wantarray ) {
@ret = $catch->($error);
} elsif ( defined $wantarray ) {
$ret[0] = $catch->($error);
} else {
$catch->($error);
};
return 1;
};
$error = $@;
$@ = $prev_error;
if ( $failed ) {
die $error;
} elsif ( $wantarray ) {
return @ret;
} elsif ( defined $wantarray ) {
return $ret[0];
} else {
return;
};
}

# in case when() was used without an explicit return, the C<for>
Expand Down

0 comments on commit 4f0f14b

Please sign in to comment.