Redundant variable names are no excuse not to bind. Nor does SELECT * self-document at all, you have to go look at the following code to see which columns are really being asked for. And this being Perl, which has hashes and excellent text munging, there's no reason you need to be redundant either.
I shall paraphrase some code from chromatic's DBI is ok article on Perl.com:
sub bind_hash {
my $table = shift;
my %results;
my $sql = do {
local $" = ", ";
"SELECT @_ FROM $table";
};
my $sth = $dbh->prepare($sql);
$sth->execute();
$sth->bind_columns(map { \$results{$_} } @_);
return (\%results, sub { $sth->fetch() });
}
# ... later:
my ($res, $fetch) = bind_hash users => qw(name email);
while ($fetch->()) {
print "$res->{name} >$res->{email}>\n";
}
It's not optimal yet, but as you see, a little inventiveness goes a long way.
Makeshifts last the longest.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|