From: Peter Rabbitson Date: Thu, 22 Mar 2012 08:06:04 +0000 (+0100) Subject: Replace all use of wantarray tri-calls with explicit return or preserve_context X-Git-Tag: v0.08197~74 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1abccf54e92eefc27a8afb1db3878462b348157c;p=dbsrgits%2FDBIx-Class.git Replace all use of wantarray tri-calls with explicit return or preserve_context --- diff --git a/lib/DBIx/Class/Ordered.pm b/lib/DBIx/Class/Ordered.pm index 05da117..d5ebc55 100644 --- a/lib/DBIx/Class/Ordered.pm +++ b/lib/DBIx/Class/Ordered.pm @@ -581,19 +581,11 @@ sub delete { $self->move_last; - my @res; - if (not defined wantarray) { - $self->next::method( @_ ); - } - elsif (wantarray) { - @res = $self->next::method( @_ ); - } - else { - $res[0] = $self->next::method( @_ ); - } + $self->next::method( @_ ); $guard->commit; - return wantarray ? @res : $res[0]; + + return $self; } # add the current position/group to the things we track old values for diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index d9fa57a..7a43db2 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -12,6 +12,7 @@ use DBIx::Class::Exception; use Scalar::Util qw/refaddr weaken reftype blessed/; use List::Util qw/first/; use Sub::Name 'subname'; +use Context::Preserve 'preserve_context'; use Try::Tiny; use overload (); use Data::Compare (); # no imports!!! guard against insane architecture @@ -1235,10 +1236,7 @@ sub _connect { my ($old_connect_via, $dbh); - if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) { - $old_connect_via = $DBI::connect_via; - $DBI::connect_via = 'connect'; - } + local $DBI::connect_via = 'connect' if $INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}; try { if(ref $info[0] eq 'CODE') { @@ -1300,9 +1298,6 @@ sub _connect { } catch { $self->throw_exception("DBI Connection failed: $_") - } - finally { - $DBI::connect_via = $old_connect_via if $old_connect_via; }; $self->_dbh_autocommit($dbh->{AutoCommit}); @@ -2748,18 +2743,12 @@ sub deployment_statements { data => $schema, ); - my @ret; - if (wantarray) { - @ret = $tr->translate; - } - else { - $ret[0] = $tr->translate; - } - - $self->throw_exception( 'Unable to produce deployment statements: ' . $tr->error) - unless (@ret && defined $ret[0]); - - return wantarray ? @ret : $ret[0]; + return preserve_context { + $tr->translate + } after => sub { + $self->throw_exception( 'Unable to produce deployment statements: ' . $tr->error) + unless defined $_[0]; + }; } # FIXME deploy() currently does not accurately report sql errors diff --git a/lib/DBIx/Class/Storage/DBI/Replicated.pm b/lib/DBIx/Class/Storage/DBI/Replicated.pm index 37954b4..51fab90 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated.pm @@ -15,8 +15,8 @@ use MooseX::Types::Moose qw/ClassName HashRef Object/; use Scalar::Util 'reftype'; use Hash::Merge; use List::Util qw/min max reduce/; +use Context::Preserve 'preserve_context'; use Try::Tiny; -use namespace::clean; use namespace::clean -except => 'meta'; @@ -478,24 +478,19 @@ around connect_info => sub { $self->_master_connect_info_opts(\%opts); - my @res; - if (wantarray) { - @res = $self->$next($info, @extra); - } else { - $res[0] = $self->$next($info, @extra); - } - - # Make sure master is blessed into the correct class and apply role to it. - my $master = $self->master; - $master->_determine_driver; - Moose::Meta::Class->initialize(ref $master); - - DBIx::Class::Storage::DBI::Replicated::WithDSN->meta->apply($master); + return preserve_context { + $self->$next($info, @extra); + } after => sub { + # Make sure master is blessed into the correct class and apply role to it. + my $master = $self->master; + $master->_determine_driver; + Moose::Meta::Class->initialize(ref $master); - # link pool back to master - $self->pool->master($master); + DBIx::Class::Storage::DBI::Replicated::WithDSN->meta->apply($master); - wantarray ? @res : $res[0]; + # link pool back to master + $self->pool->master($master); + }; }; =head1 METHODS @@ -676,41 +671,22 @@ inserted something and need to get a resultset including it, etc. =cut sub execute_reliably { - my ($self, $coderef, @args) = @_; + my $self = shift; + my $coderef = shift; unless( ref $coderef eq 'CODE') { $self->throw_exception('Second argument must be a coderef'); } - ##Get copy of master storage - my $master = $self->master; + ## replace the current read handler for the remainder of the scope + local $self->{read_handler} = $self->master; - ##Get whatever the current read hander is - my $current = $self->read_handler; - - ##Set the read handler to master - $self->read_handler($master); - - ## do whatever the caller needs - my @result; - my $want_array = wantarray; - - try { - if($want_array) { - @result = $coderef->(@args); - } elsif(defined $want_array) { - ($result[0]) = ($coderef->(@args)); - } else { - $coderef->(@args); - } + my $args = \@_; + return try { + $coderef->(@$args); } catch { $self->throw_exception("coderef returned an error: $_"); - } finally { - ##Reset to the original state - $self->read_handler($current); }; - - return wantarray ? @result : $result[0]; } =head2 set_reliable_storage