note
japhy
Oh, sorry. Maybe I misunderstood. Does the code actually work, and you want to know why it works?
<p>
$oldrow is the previous row. The function that builds the iterator stores the first row before it creates the iterator function:
<c>
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];
}
}
}
</c>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-1936">
<hr/>
Jeff <tt><font color="#0000ff">[japhy]</font></tt> Pinyan,
[id://371157|P.L., P.M., P.O.D, X.S.]:
<a href="http://japhy.perlmonk.org/modules/">Perl</a>,
<a href="http://japhy.perlmonk.org/modules/Regexp-Parser/">regex</a>,
and <a href="http://lists.perl.org/showlist.cgi?name=perl5-porters"><tt>perl</tt></a>
<a href="http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=hacker">hacker</a>
<br>
<i>How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ <b>Meister Eckhart</b></i>
</div></div>
602642
602791