r64178 - in /branches/upstream/libtry-tiny-perl/current: Changes MANIFEST META.yml Makefile.PL SIGNATURE lib/Try/Tiny.pm t/basic.t t/finally.t
ansgar at users.alioth.debian.org
ansgar at users.alioth.debian.org
Sat Oct 23 11:39:52 UTC 2010
Author: ansgar
Date: Sat Oct 23 11:39:42 2010
New Revision: 64178
URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=64178
Log:
[svn-upgrade] new version libtry-tiny-perl (0.07)
Removed:
branches/upstream/libtry-tiny-perl/current/SIGNATURE
Modified:
branches/upstream/libtry-tiny-perl/current/Changes
branches/upstream/libtry-tiny-perl/current/MANIFEST
branches/upstream/libtry-tiny-perl/current/META.yml
branches/upstream/libtry-tiny-perl/current/Makefile.PL
branches/upstream/libtry-tiny-perl/current/lib/Try/Tiny.pm
branches/upstream/libtry-tiny-perl/current/t/basic.t
branches/upstream/libtry-tiny-perl/current/t/finally.t
Modified: branches/upstream/libtry-tiny-perl/current/Changes
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtry-tiny-perl/current/Changes?rev=64178&op=diff
==============================================================================
--- branches/upstream/libtry-tiny-perl/current/Changes (original)
+++ branches/upstream/libtry-tiny-perl/current/Changes Sat Oct 23 11:39:42 2010
@@ -1,3 +1,7 @@
+0.07
+ - allow multiple finally blocks
+ - pass the error, if any, to finally blocks when called
+
0.06
- in t/given_when.t use a plan instead of done_testing for more backwards
compatibility
Modified: branches/upstream/libtry-tiny-perl/current/MANIFEST
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtry-tiny-perl/current/MANIFEST?rev=64178&op=diff
==============================================================================
--- branches/upstream/libtry-tiny-perl/current/MANIFEST (original)
+++ branches/upstream/libtry-tiny-perl/current/MANIFEST Sat Oct 23 11:39:42 2010
@@ -8,4 +8,3 @@
t/given_when.t
t/when.t
META.yml Module meta-data (added by MakeMaker)
-SIGNATURE Public-key signature (added by MakeMaker)
Modified: branches/upstream/libtry-tiny-perl/current/META.yml
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtry-tiny-perl/current/META.yml?rev=64178&op=diff
==============================================================================
--- branches/upstream/libtry-tiny-perl/current/META.yml (original)
+++ branches/upstream/libtry-tiny-perl/current/META.yml Sat Oct 23 11:39:42 2010
@@ -1,6 +1,6 @@
--- #YAML:1.0
name: Try-Tiny
-version: 0.06
+version: 0.07
abstract: ~
author: []
license: unknown
Modified: branches/upstream/libtry-tiny-perl/current/Makefile.PL
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtry-tiny-perl/current/Makefile.PL?rev=64178&op=diff
==============================================================================
--- branches/upstream/libtry-tiny-perl/current/Makefile.PL (original)
+++ branches/upstream/libtry-tiny-perl/current/Makefile.PL Sat Oct 23 11:39:42 2010
@@ -8,7 +8,6 @@
NAME => 'Try::Tiny',
VERSION_FROM => 'lib/Try/Tiny.pm',
INSTALLDIRS => 'site',
- SIGN => 1,
PL_FILES => { },
PREREQ_PM => {
'Test::More' => 0,
Modified: branches/upstream/libtry-tiny-perl/current/lib/Try/Tiny.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtry-tiny-perl/current/lib/Try/Tiny.pm?rev=64178&op=diff
==============================================================================
--- branches/upstream/libtry-tiny-perl/current/lib/Try/Tiny.pm (original)
+++ branches/upstream/libtry-tiny-perl/current/lib/Try/Tiny.pm Sat Oct 23 11:39:42 2010
@@ -10,7 +10,7 @@
@ISA = qw(Exporter);
}
-$VERSION = "0.06";
+$VERSION = "0.07";
$VERSION = eval $VERSION;
@@ -29,7 +29,7 @@
# to $failed
my $wantarray = wantarray;
- my ( $catch, $finally );
+ my ( $catch, @finally );
# find labeled blocks in the argument list.
# catch and finally tag the blocks by blessing a scalar reference to them.
@@ -41,7 +41,7 @@
if ( $ref eq 'Try::Tiny::Catch' ) {
$catch = ${$code_ref};
} elsif ( $ref eq 'Try::Tiny::Finally' ) {
- $finally = ${$code_ref};
+ push @finally, ${$code_ref};
} else {
use Carp;
confess("Unknown code ref type given '${ref}'. Check your usage & try again");
@@ -85,7 +85,9 @@
}
# set up a scope guard to invoke the finally block at the end
- my $guard = $finally && bless \$finally, "Try::Tiny::ScopeGuard";
+ my @guards =
+ map { Try::Tiny::ScopeGuard->_new($_, $failed ? $error : ()) }
+ @finally;
# at this point $failed contains a true value if the eval died, even if some
# destructor overwrote $@ as the eval was unwinding.
@@ -127,9 +129,19 @@
);
}
-sub Try::Tiny::ScopeGuard::DESTROY {
- my $self = shift;
- $$self->();
+{
+ package Try::Tiny::ScopeGuard;
+
+ sub _new {
+ shift;
+ bless [ @_ ];
+ }
+
+ sub DESTROY {
+ my @guts = @{ shift() };
+ my $code = shift @guts;
+ $code->(@guts);
+ }
}
__PACKAGE__
@@ -192,7 +204,8 @@
try { die 'foo' } catch { warn "Got a die: $_" } finally { $x = 'bar' };
Finally blocks are always executed making them suitable for cleanup code
-which cannot be handled using local.
+which cannot be handled using local. You can add as many finally blocks to a
+given try block as you like.
=head1 EXPORTS
@@ -266,6 +279,22 @@
executed in the event of a successful C<try> or if C<catch> is run. This allows
you to locate cleanup code which cannot be done via C<local()> e.g. closing a file
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:
+
+ try {
+ die_sometimes();
+ } catch {
+ # ...code run in case of error
+ } finally {
+ if (@_) {
+ print "The try block died with: @_\n";
+ } else {
+ print "The try block ran without error.\n";
+ }
+ };
B<You must always do your own error handling in the finally block>. C<Try::Tiny> will
not do anything about handling possible errors coming from code located in these
@@ -394,11 +423,21 @@
=item *
-C<@_> is not available, you need to name your args:
+C<@_> is not available within the C<try> block, so you need to copy your
+arglist. In case you want to work with argument values directly via C<@_>
+aliasing (i.e. allow C<$_[1] = "foo">), you need to pass C<@_> by reference:
sub foo {
my ( $self, @args ) = @_;
try { $self->bar(@args) }
+ }
+
+or
+
+ sub bar_in_place {
+ my $self = shift;
+ my $args = \@_;
+ try { $_ = $self->bar($_) for @$args }
}
=item *
Modified: branches/upstream/libtry-tiny-perl/current/t/basic.t
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtry-tiny-perl/current/t/basic.t?rev=64178&op=diff
==============================================================================
--- branches/upstream/libtry-tiny-perl/current/t/basic.t (original)
+++ branches/upstream/libtry-tiny-perl/current/t/basic.t Sat Oct 23 11:39:42 2010
@@ -82,6 +82,9 @@
{
my ($sub) = catch { my $a = $_; };
is(ref($sub), 'Try::Tiny::Catch', 'Checking catch subroutine scalar reference is correctly blessed');
+}
+
+{
my ($sub) = finally { my $a = $_; };
is(ref($sub), 'Try::Tiny::Finally', 'Checking finally subroutine scalar reference is correctly blessed');
}
Modified: branches/upstream/libtry-tiny-perl/current/t/finally.t
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libtry-tiny-perl/current/t/finally.t?rev=64178&op=diff
==============================================================================
--- branches/upstream/libtry-tiny-perl/current/t/finally.t (original)
+++ branches/upstream/libtry-tiny-perl/current/t/finally.t Sat Oct 23 11:39:42 2010
@@ -3,7 +3,7 @@
use strict;
#use warnings;
-use Test::More tests => 8;
+use Test::More tests => 12;
BEGIN { use_ok 'Try::Tiny' };
@@ -38,5 +38,28 @@
pass('Moved into finally block when try throws an exception and we have no catch block');
};
+try {
+ die('Die');
+} finally {
+ pass('First finally clause run');
+} finally {
+ pass('Second finally clause run');
+};
+
+try {
+ # do not die
+} finally {
+ if (@_) {
+ fail("errors reported: @_");
+ } else {
+ pass("no error reported") ;
+ }
+};
+
+try {
+ die("Die\n");
+} finally {
+ is_deeply(\@_, [ "Die\n" ], "finally got passed the exception");
+};
1;
More information about the Pkg-perl-cvs-commits
mailing list