Skip to content

Instantly share code, notes, and snippets.

@bowman
Created February 27, 2012 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bowman/1921245 to your computer and use it in GitHub Desktop.
Save bowman/1921245 to your computer and use it in GitHub Desktop.
DBIx::Connector - wrap _args in a closure instead of an array
diff --git lib/DBIx/Connector.pm lib/DBIx/Connector.pm
index 75f4e7d..ab4807f 100644
--- lib/DBIx/Connector.pm
+++ lib/DBIx/Connector.pm
@@ -10,8 +10,9 @@ our $VERSION = '0.52';
sub new {
my $class = shift;
+ my @args = @_;
bless {
- _args => [@_],
+ _args => sub { (@_) ? @args[@_] : @args },
_svp_depth => 0,
_mode => 'no_ping',
_dond => 1,
@@ -25,19 +26,19 @@ sub _connect {
my $dbh = $self->{_dbh} = do {
if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
local $DBI::connect_via = 'connect'; # Disable Apache::DBI.
- DBI->connect( @{ $self->{_args} } );
+ DBI->connect( $self->{_args}->() );
} else {
- DBI->connect( @{ $self->{_args} } );
+ DBI->connect( $self->{_args}->() );
}
};
# Modify default values.
$dbh->STORE(AutoInactiveDestroy => 1) if DBI->VERSION > 1.613 && (
- @{ $self->{_args} } < 4 || !exists $self->{_args}[3]{AutoInactiveDestroy}
+ $self->{_args}->() < 4 || !exists $self->{_args}->(3)->{AutoInactiveDestroy}
);
- $dbh->STORE(RaiseError => 1) if @{ $self->{_args} } < 4 || (
- !exists $self->{_args}[3]{RaiseError} && !exists $self->{_args}[3]{HandleError}
+ $dbh->STORE(RaiseError => 1) if $self->{_args}->() < 4 || (
+ !exists $self->{_args}->(3)->{RaiseError} && !exists $self->{_args}->(3)->{HandleError}
);
# Where are we?
@@ -45,7 +46,7 @@ sub _connect {
$self->{_tid} = threads->tid if $INC{'threads.pm'};
# Set up the driver and go!
- return $self->driver->_connect($dbh, @{ $self->{_args} });
+ return $self->driver->_connect($dbh, $self->{_args}->());
}
sub driver {
@@ -56,7 +57,7 @@ sub driver {
if (my $dbh = $self->{_dbh}) {
$dbh->{Driver}{Name};
} else {
- (DBI->parse_dsn( $self->{_args}[0]))[1];
+ (DBI->parse_dsn( $self->{_args}->(0)))[1];
}
};
$self->{driver} = DBIx::Connector::Driver->new( $driver );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment