y $cards = $dbh->prepare("SELECT Card_idCard FROM CardList WHERE User_
+idUser = $user_number AND visible = 1 ORDER BY metric");
But you are right to point out this undefined behaviour. However, when written as you suggest, it always returns true (I haven't completely got my head around why) and the loop doesn't end. It needs to be written like this:
my $card;
while (($card) = $cards->fetchrow_array)
| [reply] [d/l] [select] |
I haven't completely got my head around why
In while (my ($card) = $cards->fetchrow_array), the boolean condition being evaluated is the return value of the list assignment ()=..., and as per Assignment Operators, "a list assignment in scalar context returns the number of elements produced by the expression on the right hand side of the assignment."
However, like LanX, I can't reproduce this, and you'd have to show an SSCCE that does. Are you sure you didn't accidentally write fetchrow_arrayref instead? That method returns undef if no more rows remain - and as per the above, it's one value being assigned, so the return value of the list assignment is 1 (true). For fetchrow_arrayref, the correct loop condition is while (my $card = $cards->fetchrow_arrayref), since that's a scalar assignment and it'll return undef (false).
use warnings;
use strict;
use Data::Dumper;
use DBI;
my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:", undef, undef,
{ RaiseError=>1, AutoCommit=>1 } );
$dbh->do(q{ CREATE TABLE foobar ( foo TEXT, bar TEXT ) });
my $in = $dbh->prepare(q{INSERT INTO foobar (foo,bar) VALUES (?,?)});
$in->execute('a','b');
$in->execute('c','d');
my $cards = $dbh->prepare('SELECT * FROM foobar');
$cards->execute;
while (my ($card) = $cards->fetchrow_array) {
# OR
#while (my $card = $cards->fetchrow_arrayref) {
# BUT NOT
#while (my ($card) = $cards->fetchrow_arrayref) {
print Dumper($card);
}
| [reply] [d/l] [select] |
>
while (my $card = $cards->fetchrow_arrayref), since that's a scalar assignment and it'll return undef (false).
Well it could have been designed to detect that it's called in list context and return an empty list when the table is exhausted.
Or at least throw an error.
But I have a strong deja-vu feeling right now, I think I said this before...
DBI.pm is one of the messier parts of Perl's eco system.
| [reply] [d/l] |
| [reply] [d/l] [select] |
my $card_ref = $cards->fetchall_arrayref;
foreach my $card (@$card_ref)
{..}
This does expand the memory used by Perl to include the entire result set. But that is fine if the result set is "small" and the memory used for that result is recycled for further use by Perl. "Small" is a relative term. Nowadays, I would consider a 10,000 line result set as "small".
| [reply] [d/l] |
| [reply] |
> However, when written as you suggest, it always returns true
That's hard to believe, it's a very common construct.
And a little test in the debugger doesn't show any endless loop.
DB<2> while ( my ($x) = () ) {die $x}
DB<3>
| [reply] [d/l] |
Sorry - I was confusing the error I detailed in this post -> Re^3: my $x or my ($x)
Of course you are right about it being a common construct and, as soon as I tried to reproduce the problem it became obvious I was getting rather muddled.
| [reply] |