Here is what I use as a "callback" style iterator:
sub Fetch_w_callback{ # Main DBI retrieval mechanism----
my ($sql, $callback) = @_;
my $state = $_[2] ||= {}; # ** This THIRD param is CALL-by-ref **
$debug{SQL} = $sql;
(my $sth=$dbh->prepare($sql))->execute();
$state->{ROW} = 0;
while (my $row = $sth->fetchrow_hashref()){
$debug{ROW} = $state->{ROW}++;
last if $callback->($row,$state) < 0; # Return negative to quit
}
$sth->finish;
}
#....
#Call sequence ... (In this case, used with CGI ...
Fetch_w_callback( $sql,
sub{ # This sub gets called once per row returned....
my ($dbRow,$LocalState) = @_;
push @{$LocalState->{RAWDATA}},$dbRow ; # Raw data is availa
+ble later
$RawDataOnly and return $render_helper->(@_); # Called ONCE
+ per row, on Rawdata.
if ($LocalState->{ROW} == 1 ){ # No Rows yet
$fieldname[0] eq "*" and @fieldname = keys %$dbRow;
print Tr( {-class=>"tableHeader"},
map ({td(b($_))} @fieldname), $extrafields-
+>()) . "\n";
$_ = lc($_) for @fieldname; # DBI apparently only LC's
}
print Tr( map( {td($render_helper->($dbRow->{$_},$_,$dbRow)
+)} @fieldname),$extrafields->(@_) ) . "\n";
}
, $state);
"XML is like violence: if it doesn't solve your problem, use more."