mt2k has asked for the wisdom of the Perl Monks concerning the following question:

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 :)