sub get {
my $self = shift;
my $columns = ref $_[0] eq 'ARRAY' ? join(', ', @{ shift() }) : '*';
my $db_table = shift || croak "I really need to know DB/Table name";
my $field = @_ == 2 ? shift : 'id';
my $value = shift || croak "I really need key value";
my ($db, $table) = ($db_table =~ /^.+\..+$/) ? split(/\./, $db_table) : ($self->default_db(), $db_table);
my $dbh = $self->dbh($db);
my $sth = $dbh->prepare_cached("SELECT $columns FROM $table WHERE $field = ?", undef, 2);
$sth->execute($value);
my $resp = $sth->rows() ? $sth->fetchrow_hashref() : undef;
return $resp;
}
####
$object->get('tablename', 10);
$object->get('dbname.tablename', 10);
$object->get([qw/id foo bar/], 'tablename', 10);
$object->get('tablename', 'key_field', 10);
$object->get([qw/id foo bar/], 'dbname.tablename', 'key_field', 10);
##
##
$object->get(
fields => [qw/id foo bar/],
database => 'dbname',
table => 'tablename',
key_field => 'key_field',
key_value => 10,
);
##
##
sub test1
{
my $arg1 = shift;
# but if you wanna print $arg2 then use '' instead of undef otherwise you get warning
# and you do have use strict; use warnings; at the top right? :)
my $arg2 = @_ >= 2 || ref $_[0] ne 'ARRAY' ? shift : undef;
my $argrest = shift;
print "arg1=$arg1, arg2=$arg2, argrest=@$argrest\n\n";
}
test1('arg1', [qw/foo bar/]);
test1('arg1', \@otherargs);
test1('arg1', 'arg2');
test1('arg1', 'arg2', [qw/foo bar/]);