in reply to Re^2: Ordering Template cards
in thread Ordering Template cards
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Ordering Template cards
by LanX (Saint) on Jan 26, 2021 at 12:32 UTC | |
by haukex (Archbishop) on Jan 26, 2021 at 12:36 UTC | |
by LanX (Saint) on Jan 26, 2021 at 12:41 UTC | |
|
Re^4: Ordering Template cards
by Bod (Parson) on Jan 28, 2021 at 01:07 UTC |