Daisuke Maki > Iterator-File-Line > Iterator::File::Line



Annotate this POD

View/Report Bugs
Module Version: 0.00002   Source  


Iterator::File::Line - Iterate A File By Line


  use Iterator::File::Line;

  my $iter = Iterator::File::Line->new(
    filename => $filename,
  my $iter = Iterator::File::Line->new_from_fh( );

  while ( my $line = $iter->next ) {
    # Do something with $line

  # You want to parse TSV-ish content?
  my $iter = Iterator::File::Line->new(
    filename => "data.tsv",
    filter   => sub { return [ split(/\t/, $_[0]) ] }

  while ( my $cols = $iter->next ) {
    print $cols->[0], "\n";


Iterator::File::Line is a simple iterator that iterates over the contents of a file, which must be a regular line-based text file.





  Iterator::File::Line->new( filename => $filename );
  Iterator::File::Line->new( fh => $open_file_handle );
  Iterator::File::Line->new_from_filename( filename => $filename );
  Iterator::File::Line->new_from_fh( fh => $open_file_handle );
    filename => $filename,
    filter   => \&sub,
    chomp    => $boolean,
    encoding => 'cp932',
    eol      => 'LF', # see PerlIO::eol

Creates a new iterator instance. new_from_file requires a filename argument, while new_from_Fh requires a fh argument. new() accepts either.

If a filter argument is given, then that coderef is used to filter the incoming line. You can, for example, use this to deconstruct a CSV/TSV content. However, do note that since this is a simple line based iterator, it won't work for multi-line CSV values.

If chomp is specified, the line's ending new line is chomped. By default this is on.

If eol is specified, the line's ending new line is normalized to that value. This is done via PerlIO::eol

If encoding is specified, the incoming data is decoded into perl's native unicode. This is done via PerlIO::encoding

Please be careful with eol and encoding, as it will change the Perl IO layer associated with that file handle. You are better off not sharing the filehandle somewhere else in your code.


Returns the next chunk. If you specified a filter, this may be a structure. otherwise it's the next line


Rewinds the iterator to the beginning of the buffer


Copyright (c) 2008 Daisuke Maki <daisuke@endeworks.jp>


This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html

syntax highlighting: