Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Bernhard (4508)

https://www.xing ... hard_Schmalhofer

A freelance software engineer with experience in webapplications, databases and bioinformatics.

A contributor to Parrot and the person behind Pipp.

A physicist who worked with third sound in Helium-III and CCD X-ray detectors.

barney on
github: []
LinkedIn: []
XING: []

Journal of Bernhard (4508)

Thursday March 05, 2009
03:27 PM

Declaring lexicals within $sth-bind_columns()

[ #38598 ]
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
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
$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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • 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 :-)