Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 :)

In reply to Having to specify $_ by mt2k

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-24 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found