equal
deleted
inserted
replaced
|
1 ---+ Package =TWiki::LineIterator= |
|
2 |
|
3 Iterator over the lines in a file |
|
4 |
|
5 |
|
6 %TOC% |
|
7 |
|
8 ---++ new( $file ) |
|
9 |
|
10 Create a new iterator over the given file. if the file cannot be opened, then |
|
11 there will be no elements in the iterator. |
|
12 |
|
13 |
|
14 ---++ hasNext() -> $boolean |
|
15 |
|
16 Returns false when the iterator is exhausted. |
|
17 |
|
18 <verbatim> |
|
19 my $it = new TWiki::ListIterator(\@list); |
|
20 while ($it->hasNext()) { |
|
21 ... |
|
22 </verbatim> |
|
23 |
|
24 |
|
25 ---++ next() -> $data |
|
26 |
|
27 Return the next line in the file. |
|
28 |
|
29 The iterator object can be customised to pre- and post-process entries from |
|
30 the list before returning them. This is done by setting two fields in the |
|
31 iterator object: |
|
32 |
|
33 * ={filter}= can be defined to be a sub that filters each entry. The entry |
|
34 will be ignored (next() will not return it) if the filter returns false. |
|
35 * ={process}= can be defined to be a sub to process each entry before it |
|
36 is returned by next. The value returned from next is the value returned |
|
37 by the process function. |
|
38 |
|
39 For example, |
|
40 <verbatim> |
|
41 my $it = new TWiki::LineIterator("/etc/passwd"); |
|
42 $it->{filter} = sub { $_[0] =~ /^.*?:/; return $1; }; |
|
43 $it->{process} = sub { return "User $_[0]"; }; |
|
44 while ($it->hasNext()) { |
|
45 my $x = $it->next(); |
|
46 print "$x\n"; |
|
47 } |
|
48 </verbatim> |
|
49 |