I've come across what is to me an odd behaviour.
I'm not sure as to whether this behaviour is related to perl or DBI, but it sure is strange. For the first time ever, I've come across a line of perl code that requires a person to place the variable $_ on the left side of an operation. (There may be others I'm not thinking of, but those make common sense if they exist.)
I found this out when rob_au gave me a code replacement option for DBI Queries. What he told me was that I could replace serveral lines of my code with only one line (thus eliminating the need for a hash). The line he produced for me turned the code into something close to the following:
my $dbh = DBI->connect( ... );
my $sth = $dbh->prepare ( ... );
$sth->execute ( @placeholders );
push @results, $_ while ($sth->fetchrow_hashref);
So that last line in the code sample is the line in question. That line of code will not work the way a person might believe. You'd think (if you think like me) that $_ is assigned the value of $sth->fetchrow_hashref everytime through the loop. It's not. Or it is, but not in the right way. To make the code work correctly, you must change it to specifically assign the return value of $sth->fetchrow_hashref to $_. So the line becomes:
push @results, $_ while ($_ = $sth->fetchrow_hashref);
What can I say? I don't quite understand the reason behind this behaviour. If anybody has a valid reason as to why, or perhaps a theory, let me know. The curiousity will soon do to me what it did to the cat :)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.