Stories
Slash Boxes
Comments
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)

Bernhard
  Bernhard.Schmalhofer@gmx.de
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 irc.perl.org
github: http://github.com/bschmalhofer [github.com]
LinkedIn:http://www.linkedin.com/in/bernhardschmalhofer [linkedin.com]
XING: https://www.xing.com/profile/Bernhard_Schmalhofer [xing.com]

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
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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
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 :-)