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= $state->{STH} = $dbh->prepare($sql))->execute(); $state->{ROW} = $state->{QUIT} = 0; while (my $row = $sth->fetchrow_hashref()){ $debug{ROW} = $state->{ROW}++; #last if ($callback->($row,$state) || 0) < 0; # Return negative to quit $callback->($row,$state); last if $state->{QUIT}; # Call-ee asked us to stop. } $sth->finish; }