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();
use strict;
#use warnings;
-use Test::More tests => 13;
+use Test::More tests => 24;
BEGIN { use_ok 'Try::Tiny' };
};
};
+$_ = "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;