my $fetch = sub { my ($fh) = @_; return if eof $fh; $_[1] = scalar <$fh>; return 1; }; ... while ($next->(my $item)) { print "$item\n"; }