while (my $ar = $sth->fetch()) { my $item = $ar->[0]; ... } #### sub make_sth_iterator { my ($sth) = @_; return sub { my $ar = $sth->fetch(); return $ar ? $ar->[0] : (); }; } my $iter = make_sth_iter($sth); # Parens around $item required. Without, # them an $item with value "" or "0" # will exit the loop prematurely. while (my ($item) = $iter->()) { ... }