From: Jesse Luehrs Date: Wed, 27 Apr 2011 22:34:43 +0000 (-0500) Subject: clarify the state of $_ in finally blocks X-Git-Tag: Try-Tiny-0.10~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=658a90e5f2046519cb4b691ab2905c4a48a2901e;p=p5sagit%2FTry-Tiny.git clarify the state of $_ in finally blocks --- diff --git a/lib/Try/Tiny.pm b/lib/Try/Tiny.pm index 795272e..17bcf80 100644 --- a/lib/Try/Tiny.pm +++ b/lib/Try/Tiny.pm @@ -289,8 +289,10 @@ you to locate cleanup code which cannot be done via C e.g. closing a fi handle. When invoked, the finally block is passed the error that was caught. If no -error was caught, it is passed nothing. In other words, the following code -does just what you would expect: +error was caught, it is passed nothing. (Note that the finally block does not +localize C<$_> with the error, since unlike in a catch block, there is no way +to know if C<$_ == undef> implies that there were no errors.) In other words, +the following code does just what you would expect: try { die_sometimes(); diff --git a/t/finally.t b/t/finally.t index 80af9e5..abfb9cb 100644 --- a/t/finally.t +++ b/t/finally.t @@ -3,7 +3,7 @@ use strict; #use warnings; -use Test::More tests => 13; +use Test::More tests => 24; BEGIN { use_ok 'Try::Tiny' }; @@ -74,4 +74,31 @@ try { }; }; +$_ = "foo"; +try { + is($_, "foo", "not localized in try"); +} +catch { +} +finally { + is(scalar(@_), 0, "nothing in \@_ (finally)"); + is($_, "foo", "\$_ not localized (finally)"); +}; +is($_, "foo", "same afterwards"); + +$_ = "foo"; +try { + is($_, "foo", "not localized in try"); + die "bar\n"; +} +catch { + is($_[0], "bar\n", "error in \@_ (catch)"); + is($_, "bar\n", "error in \$_ (catch)"); +} +finally { + is(scalar(@_), 1, "error in \@_ (finally)"); + is($_[0], "bar\n", "error in \@_ (finally)"); + is($_, "foo", "\$_ not localized (finally)"); +}; +is($_, "foo", "same afterwards"); 1;