From: Yuval Kogman Date: Fri, 11 Sep 2009 17:57:13 +0000 (+0900) Subject: Document and test for prev value of $@ in catch X-Git-Tag: Try-Tiny-0.03~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ac4f5f9f46a61b6ef40a44cf9dae590caae4443f;p=p5sagit%2FTry-Tiny.git Document and test for prev value of $@ in catch Since catch is invoked after the delocalization the previous value of $@ (whether meaningful or not) should still be available. --- diff --git a/lib/Try/Tiny.pm b/lib/Try/Tiny.pm index e3fdf8d..1c45fa9 100644 --- a/lib/Try/Tiny.pm +++ b/lib/Try/Tiny.pm @@ -167,6 +167,10 @@ is the same as sub { ... } +Inside the catch block the previous value of C<$@> is still available for use. +This value may or may not be meaningful depending on what happenned before the +C, but it might be a good idea to preserve it in an error stack. + =back =head1 BACKGROUND diff --git a/t/basic.t b/t/basic.t index 7f1471c..1d257da 100644 --- a/t/basic.t +++ b/t/basic.t @@ -3,7 +3,7 @@ use strict; #use warnings; -use Test::More tests => 21; +use Test::More tests => 22; BEGIN { use_ok 'Try::Tiny' }; @@ -129,7 +129,7 @@ sub Evil::new { bless { }, $_[0] } } { - my $caught; + my ( $caught, $prev ); { local $@; @@ -144,8 +144,10 @@ sub Evil::new { bless { }, $_[0] } } } catch { $caught = $_; + $prev = $@; } } is_deeply( $caught, { prev => "bar\n" }, 'previous value of $@ available for capture' ); + is( $prev, "bar\n", 'previous value of $@ also available in catch block' ); }