Oh, sorry. Maybe I misunderstood. Does the code actually work, and you want to know why it works?
$oldrow is the previous row. The function that builds the iterator stores the first row before it creates the iterator function:
sub dbi_query_iterator {
my ($sth, @params) = @_;
$sth->execute(@params) or return;
# get the first row of results NOW
my $row = $sth->fetchrow_arrayref();
return Iterator {
my $action = shift() || 'nextval';
if ($action eq 'exhausted?') {
return ! defined $row;
} elsif ($action eq 'nextval') {
# save the current row of results
my $oldrow = $row;
# get the next row of results
$row = $sth->fetchrow_arrayref;
# return the previous row (before we advanced)
return $oldrow->[0];
}
}
}