Declaring lexicals within $sth-bind_columns()

Bernhard on 2009-03-05T20:27:57

Lately I have taken on the maintainance of a legacy Perl 5 script. The script heavily uses the DBI function bind_columns() for retrieving data from a relational database. After some refaktoring I ended up with something like this:



my $sth = $dbh->prepare(<<'END_SQL'); SELECT color, food, num_legs FROM pet END_SQL my ( $food, $something_else, $color, $num_legs); $sth->bind_columns( \$color, \$food, $num_legs );



Not being entirely happy with I went one step further and got:



my $sth = $dbh->prepare(<<'END_SQL'); SELECT color, food, num_legs FROM pet END_SQL $sth->bind_columns( \( my ( $color, $food, $num_legs) ) ): my ( $something_else );



This style enforces some structure in the declaration. Is there a way to make this really nice? Installing a module is no option in this context.


Binding to a hash

runrig on 2009-03-05T23:31:37

I like to do:

my @names = @{$sth->{NAME_lc}}; # or _uc
my %hash; $sth->bind_columns(\@hash{@names});

Also, in your code, you don't need the parens around "my(...)", though if you like them you can keep them :-)