Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Mysterious Code "DBI" - Why is this working?

by graff (Chancellor)
on Jun 09, 2005 at 03:14 UTC ( [id://464940]=note: print w/replies, xml ) Need Help??


in reply to Mysterious Code "DBI" - Why is this working?

Responding to your update: you need to read the DBI man page more carefully ("perldoc DBI"). And/or you need to be more attentive to the "sigils" that you attach to your variable names ("@" for arrays, "%" for hashes, "$" for scalar variables or for a single indexed element within an array or hash).

Here's a relevant snippet from the man page about "fetchrow_array":

If called in a scalar context for a statement handle that has more than one column, it is undefined whether the driver will return the value of the first column or the last. So don't do that. Also, in a scalar context, an "undef" is returned if there are no more rows or if an error occurred. That "undef" can't be distinguished from an "undef" returned because the first field value was NULL. For these reasons you should exercise some caution if you use "fetchrow_array" in a scalar context.

Now, it does seem like your query will return a single value per row, so maybe  while ( my $market = $sth->fetchrow_array ) should work the way you expect. But maybe you shouldn't count on that.

It would suffice to use parens in the assignment, so that everybody is clear that fetchrow_array is being called in a list context:

while ( my ( $market ) = $market_sth->fetchrow_array ) { ... }
The fact that you can get it to work in a scalar context at all is probably not important (except to the extent that this reveals bugs or inconsistencies in your particular DBD module). It's just better not to do it that way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://464940]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found