From: Ton Voon Date: Fri, 14 May 2010 21:25:02 +0000 (+0000) Subject: Conversion of eval => try (part 1) X-Git-Tag: v0.08122~57^2~19 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=20674fcd94984d0d2be09ecda7f3c22715468936;p=dbsrgits%2FDBIx-Class.git Conversion of eval => try (part 1) --- diff --git a/Makefile.PL b/Makefile.PL index 3203baf..133007f 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -50,6 +50,7 @@ my $runtime_requires = { 'Data::Dumper::Concise' => '1.000', 'Scope::Guard' => '0.03', 'Context::Preserve' => '0.01', + 'Try::Tiny' => '0.04', }; # this is so we can order requires alphabetically diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index 1a50606..7aba895 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -11,6 +11,7 @@ use DBIx::Class::Optional::Dependencies; use vars qw($VERSION); use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/; use DBIx::Class::StartupCheck; +use Try::Tiny; sub mk_classdata { shift->mk_classaccessor(@_); @@ -42,8 +43,14 @@ sub MODIFY_CODE_ATTRIBUTES { sub _attr_cache { my $self = shift; my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {}; - my $rest = eval { $self->next::method }; - return $@ ? $cache : { %$cache, %$rest }; + my $rest; + my $exception; + try { + $rest = $self->next::method; + } catch { + $exception = 1; + }; + return $exception ? $cache : { %$cache, %$rest }; } 1; diff --git a/lib/DBIx/Class/Schema/Versioned.pm b/lib/DBIx/Class/Schema/Versioned.pm index aa4f702..fe6c694 100644 --- a/lib/DBIx/Class/Schema/Versioned.pm +++ b/lib/DBIx/Class/Schema/Versioned.pm @@ -503,8 +503,9 @@ sub get_db_version my ($self, $rs) = @_; my $vtable = $self->{vschema}->resultset('Table'); - my $version = eval { - $vtable->search({}, { order_by => { -desc => 'installed' }, rows => 1 } ) + my $version; + try { + $version = $vtable->search({}, { order_by => { -desc => 'installed' }, rows => 1 } ) ->get_column ('version') ->next; }; @@ -723,10 +724,14 @@ sub _source_exists { my ($self, $rs) = @_; - my $c = eval { - $rs->search({ 1, 0 })->count; + my $c; + my $exception; + try { + $c = $rs->search({ 1, 0 })->count; + } catch { + $exception=1; }; - return 0 if $@ || !defined $c; + return 0 if $exception || !defined $c; return 1; } diff --git a/lib/DBIx/Class/Storage.pm b/lib/DBIx/Class/Storage.pm index 0def315..08ea155 100644 --- a/lib/DBIx/Class/Storage.pm +++ b/lib/DBIx/Class/Storage.pm @@ -158,16 +158,15 @@ For example, }; my $rs; - eval { + try { $rs = $schema->txn_do($coderef); - }; - - if ($@) { # Transaction failed + } catch { + # Transaction failed die "something terrible has happened!" # if ($@ =~ /Rollback failed/); # Rollback failed deal_with_failed_transaction(); - } + }; In a nested transaction (calling txn_do() from within a txn_do() coderef) only the outermost transaction will issue a L, and txn_do() can be @@ -197,7 +196,7 @@ sub txn_do { my $wantarray = wantarray; # Need to save this since the context # inside the eval{} block is independent # of the context that called txn_do() - eval { + try { # Need to differentiate between scalar/list context to allow for # returning a list in scalar context to get the size of the list @@ -212,16 +211,12 @@ sub txn_do { $coderef->(@args); } $self->txn_commit; - }; - - if ($@) { + } catch { my $error = $@; - eval { + try { $self->txn_rollback; - }; - - if ($@) { + } catch { my $rollback_error = $@; my $exception_class = "DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION"; $self->throw_exception($error) # propagate nested rollback @@ -230,9 +225,8 @@ sub txn_do { $self->throw_exception( "Transaction aborted: $error. Rollback failed: ${rollback_error}" ); - } else { - $self->throw_exception($error); # txn failed but rollback succeeded } + $self->throw_exception($error); # txn failed but rollback succeeded } return $wantarray ? @return_values : $return_value; diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 4214463..98c9f91 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -15,6 +15,7 @@ use Scalar::Util(); use List::Util(); use Data::Dumper::Concise(); use Sub::Name (); +use Try::Tiny; use File::Path (); @@ -157,8 +158,7 @@ sub DESTROY { # some databases need this to stop spewing warnings if (my $dbh = $self->_dbh) { - local $@; - eval { + try { %{ $dbh->{CachedKids} } = (); $dbh->disconnect; };