1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/data/TWiki/TWikiLineIteratorDotPm.txt Sat Jan 26 15:50:53 2008 +0100
1.3 @@ -0,0 +1,49 @@
1.4 +---+ Package =TWiki::LineIterator=
1.5 +
1.6 +Iterator over the lines in a file
1.7 +
1.8 +
1.9 +%TOC%
1.10 +
1.11 +---++ new( $file )
1.12 +
1.13 +Create a new iterator over the given file. if the file cannot be opened, then
1.14 +there will be no elements in the iterator.
1.15 +
1.16 +
1.17 +---++ hasNext() -> $boolean
1.18 +
1.19 +Returns false when the iterator is exhausted.
1.20 +
1.21 +<verbatim>
1.22 +my $it = new TWiki::ListIterator(\@list);
1.23 +while ($it->hasNext()) {
1.24 + ...
1.25 +</verbatim>
1.26 +
1.27 +
1.28 +---++ next() -> $data
1.29 +
1.30 +Return the next line in the file.
1.31 +
1.32 +The iterator object can be customised to pre- and post-process entries from
1.33 +the list before returning them. This is done by setting two fields in the
1.34 +iterator object:
1.35 +
1.36 + * ={filter}= can be defined to be a sub that filters each entry. The entry
1.37 + will be ignored (next() will not return it) if the filter returns false.
1.38 + * ={process}= can be defined to be a sub to process each entry before it
1.39 + is returned by next. The value returned from next is the value returned
1.40 + by the process function.
1.41 +
1.42 +For example,
1.43 +<verbatim>
1.44 +my $it = new TWiki::LineIterator("/etc/passwd");
1.45 +$it->{filter} = sub { $_[0] =~ /^.*?:/; return $1; };
1.46 +$it->{process} = sub { return "User $_[0]"; };
1.47 +while ($it->hasNext()) {
1.48 + my $x = $it->next();
1.49 + print "$x\n";
1.50 +}
1.51 +</verbatim>
1.52 +