data/TWiki/TWikiListIteratorDotPm.txt,v
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
     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.26;	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::ListIterator=
    25 
    26 Iterator over a list
    27 
    28 
    29 %TOC%
    30 
    31 ---++ new(\@@list)
    32 
    33 Create a new iterator over the given list. Designed primarily for operations
    34 over fully defined lists of object references. The list is not damaged in
    35 any way.
    36 
    37 
    38 ---++ hasNext() -> $boolean
    39 
    40 Returns false when the iterator is exhausted.
    41 
    42 <verbatim>
    43 my $it = new TWiki::ListIterator(\@@list);
    44 while ($it->hasNext()) {
    45    ...
    46 </verbatim>
    47 
    48 
    49 ---++ next() -> $data
    50 
    51 Return the next entry in the list.
    52 
    53 The iterator object can be customised to pre- and post-process entries from
    54 the list before returning them. This is done by setting two fields in the
    55 iterator object:
    56 
    57    * ={filter}= can be defined to be a sub that filters each entry. The entry
    58      will be ignored (next() will not return it) if the filter returns false.
    59    * ={process}= can be defined to be a sub to process each entry before it
    60      is returned by next. The value returned from next is the value returned
    61      by the process function.
    62 
    63 For example,
    64 <verbatim>
    65 my @@list = ( 1, 2, 3 );
    66 
    67 my $it = new TWiki::ListIterator(\@@list);
    68 $it->{filter} = sub { return $_[0] != 2 };
    69 $it->{process} = sub { return $_[0] + 1 };
    70 while ($it->hasNext()) {
    71     my $x = $it->next();
    72     print "$x, ";
    73 }
    74 </verbatim>
    75 will print
    76 <verbatim>
    77 2, 4
    78 </verbatim>
    79 
    80 @