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"; }
In reply to Re: How To: Make An Iterator
by kelan
in thread How To: Make An Iterator
by Limbic~Region
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |