August 1999 Return to the Perl Hardware Store Slide #32

Solution with Iterators

        sub make_iterator {
          my ($file, $key) = @_;
          my $fh = do { local *FH }; 
          open $fh, $file or return;
          my $iterator =  
            sub {
              my $line;
              while ($line = <$fh>) {
                return $line if index $line, $key >= 0;
              }
              return;
            };
          return $iterator;
        }


Next Copyright © 1999 M-J. Dominus