http://qs321.pair.com?node_id=451349


in reply to How To: Make An Iterator

Here's another take on the first example that only returns one line at a time until all filehandles are exhausted and returns undef when that happens.

sub gen_fh_iterator { my ( $current, @fhs ) = @_; return sub { my $line; while ( ! defined $line && @fhs ) { $line = <$current>; $current = shift @fhs if not defined $line; } return $line; }; } my $file_iter = gen_fh_iterator( @fhs ); while ( my $line = $file_iter->() ) { print "$line\n"; }