data/TWiki/TWikiLineIteratorDotPm.txt,v
changeset 0 414e01d06fd5
equal deleted inserted replaced
-1:000000000000 0:414e01d06fd5
       
     1 head	1.1;
       
     2 access;
       
     3 symbols;
       
     4 locks; strict;
       
     5 comment	@# @;
       
     6 
       
     7 
       
     8 1.1
       
     9 date	2008.01.22.03.21.24;	author TWikiContributor;	state Exp;
       
    10 branches;
       
    11 next	;
       
    12 
       
    13 
       
    14 desc
       
    15 @buildrelease
       
    16 @
       
    17 
       
    18 
       
    19 1.1
       
    20 log
       
    21 @buildrelease
       
    22 @
       
    23 text
       
    24 @---+ Package =TWiki::LineIterator=
       
    25 
       
    26 Iterator over the lines in a file
       
    27 
       
    28 
       
    29 %TOC%
       
    30 
       
    31 ---++ new( $file )
       
    32 
       
    33 Create a new iterator over the given file. if the file cannot be opened, then
       
    34 there will be no elements in the iterator.
       
    35 
       
    36 
       
    37 ---++ hasNext() -> $boolean
       
    38 
       
    39 Returns false when the iterator is exhausted.
       
    40 
       
    41 <verbatim>
       
    42 my $it = new TWiki::ListIterator(\@@list);
       
    43 while ($it->hasNext()) {
       
    44    ...
       
    45 </verbatim>
       
    46 
       
    47 
       
    48 ---++ next() -> $data
       
    49 
       
    50 Return the next line in the file.
       
    51 
       
    52 The iterator object can be customised to pre- and post-process entries from
       
    53 the list before returning them. This is done by setting two fields in the
       
    54 iterator object:
       
    55 
       
    56    * ={filter}= can be defined to be a sub that filters each entry. The entry
       
    57      will be ignored (next() will not return it) if the filter returns false.
       
    58    * ={process}= can be defined to be a sub to process each entry before it
       
    59      is returned by next. The value returned from next is the value returned
       
    60      by the process function.
       
    61 
       
    62 For example,
       
    63 <verbatim>
       
    64 my $it = new TWiki::LineIterator("/etc/passwd");
       
    65 $it->{filter} = sub { $_[0] =~ /^.*?:/; return $1; };
       
    66 $it->{process} = sub { return "User $_[0]"; };
       
    67 while ($it->hasNext()) {
       
    68     my $x = $it->next();
       
    69     print "$x\n";
       
    70 }
       
    71 </verbatim>
       
    72 
       
    73 @