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]; } } }