Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Try::Tiny

  • Loading branch information...
commit 009622239ee830552c6c4d48b69aff0789acfc31 1 parent 9f70867
@nothingmuch authored
View
1  Makefile.PL
@@ -48,6 +48,7 @@ WriteMakefile(
'MooseX::YAML' => 0.02,
# misc dev shit
+ 'Try::Tiny' => 0,
'Scalar::Util' => 0,
#'Devel::PartialDump' => '0.06',
'namespace::clean' => '0.08',
View
7 bench/shootout.pl
@@ -7,6 +7,7 @@
use Path::Class;
use Storable qw(nstore retrieve);
use Scalar::Util qw(blessed);
+use Try::Tiny;
use KiokuDB;
@@ -38,10 +39,10 @@ sub bench {
$mxsd_sqlite->backend->deploy;
- my $mxsd_mysql = eval { KiokuDB->connect("dbi:mysql:test", serializer => "storable") }; warn $@ if $@;
+ my $mxsd_mysql = try { KiokuDB->connect("dbi:mysql:test", serializer => "storable") } catch { warn @_ };
$mxsd_mysql && $mxsd_mysql->backend->deploy({ add_drop_table => 1, producer_args => { mysql_version => 5 } });
- my $mxsd_pg = eval { KiokuDB->connect("dbi:Pg:dbname=test", serializer => "storable") }; warn $@ if $@;
+ my $mxsd_pg = try { KiokuDB->connect("dbi:Pg:dbname=test", serializer => "storable") } catch { warn $@ };
$mxsd_pg && $mxsd_pg->backend->deploy({ add_drop_table => 1 });
$dir->subdir("mxsd_bdb_dumb")->mkpath;
@@ -66,7 +67,7 @@ sub bench {
my $name = $ENV{KIOKU_COUCHDB_NAME} || "kioku-$$";
my $db = $couch->db($name);
- eval { $db->drop };
+ try { $db->drop };
$db->create;
View
5 bin/kioku
@@ -4,6 +4,7 @@ use strict;
use warnings;
use KiokuDB;
+use Try::Tiny;
unless ( try_run() ) {
if ( $INC{"KiokuDB/Cmd.pm"} ) {
@@ -23,7 +24,7 @@ exit 1;
sub try_run {
- return unless eval { require KiokuDB::Cmd; 1 };
+ return unless try { require KiokuDB::Cmd; 1 };
return unless KiokuDB::Cmd->is_up_to_date;
KiokuDB::Cmd->run;
@@ -31,7 +32,7 @@ sub try_run {
}
sub try_auto_install {
- if (eval {
+ if (try {
require Module::AutoInstall;
require ExtUtils::MakeMaker;
1;
View
31 lib/KiokuDB.pm
@@ -18,6 +18,7 @@ use KiokuDB::Stream::Objects;
use Hash::Util::FieldHash::Compat qw(idhash);
use Carp qw(croak);
+use Try::Tiny;
use namespace::clean -except => [qw(meta SERIAL_IDS)];
@@ -28,7 +29,7 @@ no warnings 'recursion';
our $REQUIRED_CMD_VERSION = "0.02";
sub cmd_is_up_to_date {
require KiokuDB::Cmd;
- eval { KiokuDB::Cmd->VERSION($REQUIRED_CMD_VERSION); 1 };
+ try { KiokuDB::Cmd->VERSION($REQUIRED_CMD_VERSION); 1 };
}
@@ -271,27 +272,19 @@ sub lookup {
my $linker = $self->linker;
- my ( $e, @objects );
+ try {
+ my @objects = $linker->get_or_load_objects(@ids);
- eval {
- local $@;
- eval { @objects = $linker->get_or_load_objects(@ids) };
- $e = $@;
- };
-
- if ( $e ) {
- if ( ref($e) and $e->{missing} ) {
- return;
+ if ( @ids == 1 ) {
+ return $objects[0];
+ } else {
+ return @objects;
}
+ } catch {
+ die $_ unless ref and $_->{missing};
- die $e;
- }
-
- if ( @ids == 1 ) {
- return $objects[0];
- } else {
- return @objects;
- }
+ return;
+ };
}
sub search {
View
14 lib/KiokuDB/Backend.pm
@@ -4,6 +4,7 @@ package KiokuDB::Backend;
use Moose::Role;
use Moose::Util::TypeConstraints;
+use Try::Tiny;
use namespace::clean -except => 'meta';
@@ -12,16 +13,13 @@ coerce ( __PACKAGE__,
my %p = %$_;
my $class = delete $p{class} || die "Can't coerce backend from hash without a 'class' parameter";
- do {
- local $@;
- eval {
- Class::MOP::load_class("KiokuDB::Backend::$class");
- "KiokuDB::Backend::$class"->new(%p);
- }
- } or do {
+ try {
+ Class::MOP::load_class("KiokuDB::Backend::$class");
+ "KiokuDB::Backend::$class"->new(%p);
+ } catch {
Class::MOP::load_class($class);
$class->new(%p);
- }
+ };
},
);
View
60 lib/KiokuDB/Backend/Role/TXN.pm
@@ -4,6 +4,7 @@ package KiokuDB::Backend::Role::TXN;
use Moose::Role;
use Carp qw(croak);
+use Try::Tiny;
use namespace::clean -except => 'meta';
@@ -20,44 +21,31 @@ sub txn_do {
my @txn_args = $self->txn_begin;
- my @result;
-
- my $wantarray = wantarray; # gotta capture, eval { } has its own
-
- my ( $success, $err ) = do {
- local $@;
-
- my $success = eval {
- if ( $wantarray ) {
- @result = $coderef->(@args);
- } elsif( defined $wantarray ) {
- $result[0] = $coderef->(@args);
- } else {
- $coderef->(@args);
- }
-
- $commit && $commit->();
- $self->txn_commit(@txn_args);
-
- 1;
+ try {
+ my @ret;
+
+ if ( wantarray ) {
+ @ret = $coderef->(@args);
+ } elsif ( defined wantarray ) {
+ $ret[0] = $coderef->(@args);
+ } else {
+ $coderef->(@args);
+ }
+
+ $commit->() if $commit;
+ $self->txn_commit(@txn_args);
+
+ return wantarray ? @ret : $ret[0];
+ } catch {
+ my $err = $_;
+
+ try {
+ $self->txn_rollback(@txn_args);
+ $rollback->() if $rollback;
+ } catch {
+ croak "Transaction aborted: $err, rollback failed: $_";
};
- ( $success, $@ );
- };
-
- if ( $success ) {
- return wantarray ? @result : $result[0];
- } else {
- my $rollback_exception = do {
- local $@;
- eval { $self->txn_rollback(@txn_args); $rollback && $rollback->() };
- $@;
- };
-
- if ($rollback_exception) {
- croak "Transaction aborted: $err, rollback failed: $rollback_exception";
- }
-
die $err;
}
}
View
4 lib/KiokuDB/Role/UUIDs.pm
@@ -3,12 +3,14 @@
package KiokuDB::Role::UUIDs;
use Moose::Role;
+use Try::Tiny;
+
use namespace::clean -except => 'meta';
if ( defined &KiokuDB::SERIAL_IDS and KiokuDB::SERIAL_IDS() ) {
with qw(KiokuDB::Role::UUIDs::SerialIDs);
} else {
- my $have_libuuid = do { local $@; eval { require Data::UUID::LibUUID; 1 } };
+ my $have_libuuid = try { require Data::UUID::LibUUID; 1 };
my $backend = $have_libuuid ? "LibUUID" : "DataUUID";
View
7 lib/KiokuDB/Test/Fixture/Concurrency.pm
@@ -4,6 +4,7 @@ use Moose;
use Test::More;
use Test::Exception;
+use Try::Tiny;
use List::Util qw(sum);
use Scope::Guard;
use POSIX qw(_exit :sys_wait_h);
@@ -121,7 +122,7 @@ sub check_consistency {
my ( $counter, @accounts );
attempt: foreach my $attempt ( 1 .. FORKS ) {
- last attempt if eval {
+ last attempt if try {
$self->txn_do(sub {
my $s = $self->new_scope;
@@ -164,7 +165,7 @@ sub run_child {
my ( $self, $child ) = @_;
for ( 1 .. ITER ) {
- eval {
+ try {
$self->txn_do(sub {
my $s = $self->new_scope;
@@ -199,7 +200,7 @@ sub run_child {
my $ok;
attempt: foreach my $attempt ( 1 .. FORKS*2 ) {
- last attempt if eval {
+ last attempt if try {
$self->txn_do(sub {
my $s = $self->new_scope;
View
3  lib/KiokuDB/Test/Fixture/TXN/Nested.pm
@@ -5,6 +5,7 @@ use Moose;
use Test::More;
use Test::Exception;
+use Try::Tiny;
use namespace::clean -except => 'meta';
@@ -39,7 +40,7 @@ sub verify {
my ( $db_entry ) = $self->backend->get( $self->joe );
is( $db_entry->data->{name}, "lalalala", "entry written to DB" );
- eval {
+ try {
$self->txn_do(sub {
$joe->name("oi");
$self->update_ok($joe);
View
19 lib/KiokuDB/Test/Fixture/TypeMap/Default.pm
@@ -8,6 +8,7 @@ use Moose;
use Encode;
use Test::More;
use Test::Moose;
+use Try::Tiny;
use KiokuDB::Test::Person;
use KiokuDB::Test::Employee;
@@ -18,15 +19,15 @@ use namespace::clean -except => 'meta';
use constant required_backend_roles => qw(TypeMap::Default);
use Tie::RefHash;
-use constant HAVE_DATETIME => eval { require DateTime };
-use constant HAVE_DATETIME_FMT => eval { require DateTime::Format::Strptime };
-use constant HAVE_URI => eval { require URI };
-use constant HAVE_URI_WITH_BASE => eval { require URI::WithBase };
-use constant HAVE_AUTHEN_PASSPHRASE => eval { require Authen::Passphrase::SaltedDigest };
-use constant HAVE_PATH_CLASS => eval { require Path::Class };
-use constant HAVE_IXHASH => eval { require Tie::IxHash };
-use constant HAVE_MX_TRAITS => eval { require MooseX::Traits };
-use constant HAVE_MX_OP => eval { require MooseX::Object::Pluggable };
+use constant HAVE_DATETIME => try { require DateTime };
+use constant HAVE_DATETIME_FMT => try { require DateTime::Format::Strptime };
+use constant HAVE_URI => try { require URI };
+use constant HAVE_URI_WITH_BASE => try { require URI::WithBase };
+use constant HAVE_AUTHEN_PASSPHRASE => try { require Authen::Passphrase::SaltedDigest };
+use constant HAVE_PATH_CLASS => try { require Path::Class };
+use constant HAVE_IXHASH => try { require Tie::IxHash };
+use constant HAVE_MX_TRAITS => try { require MooseX::Traits };
+use constant HAVE_MX_OP => try { require MooseX::Object::Pluggable };
{
package Some::Role;
View
11 lib/KiokuDB/TypeMap.pm
@@ -4,6 +4,7 @@ package KiokuDB::TypeMap;
use Moose;
use Carp qw(croak);
+use Try::Tiny;
use KiokuDB::TypeMap::Entry;
use KiokuDB::TypeMap::Entry::Alias;
@@ -50,11 +51,11 @@ sub resolve {
unless ( $loaded{$class}++ ) {
( my $pmfile = $class . ".pm" ) =~ s{::}{/}g;
- my $e = do { local $@; eval { require $pmfile }; $@ };
-
- if ($e) {
- croak $e unless $e =~ /^Can't locate \Q$pmfile\E in \@INC/;
- }
+ try {
+ require $pmfile;
+ } catch {
+ croak $_ unless /^Can't locate \Q$pmfile\E in \@INC/;
+ };
}
# if this is an anonymous class, redo the lookup using a single named
Please sign in to comment.
Something went wrong with that request. Please try again.