1
# See bottom of file for license and copyright information
5
---+ package Foswiki::ListIterator
6
*implements* Foswiki::Iterator
8
Iterator over a perl list
12
package Foswiki::ListIterator;
13
use base 'Foswiki::Iterator';
21
Create a new iterator over the given list. Designed primarily for operations
22
over fully defined lists of object references. The list is not damaged in
28
my ( $class, $list ) = @_;
44
---++ hasNext() -> $boolean
46
Returns false when the iterator is exhausted.
49
my $it = new Foswiki::ListIterator(\@list);
50
while ($it->hasNext()) {
58
return 1 if $this->{next};
61
if ( $this->{list} && $this->{index} < scalar( @{ $this->{list} } ) ) {
62
$n = $this->{list}->[ $this->{index}++ ];
67
} while ( $this->{filter} && !&{ $this->{filter} }($n) );
76
Return the next entry in the list.
78
The iterator object can be customised to pre- and post-process entries from
79
the list before returning them. This is done by setting two fields in the
82
* ={filter}= can be defined to be a sub that filters each entry. The entry
83
will be ignored (next() will not return it) if the filter returns false.
84
* ={process}= can be defined to be a sub to process each entry before it
85
is returned by next. The value returned from next is the value returned
86
by the process function.
90
my @list = ( 1, 2, 3 );
92
my $it = new Foswiki::ListIterator(\@list);
93
$it->{filter} = sub { return $_[0] != 2 };
94
$it->{process} = sub { return $_[0] + 1 };
95
while ($it->hasNext()) {
110
my $n = $this->{next};
111
$this->{next} = undef;
112
$n = &{ $this->{process} }($n) if $this->{process};
118
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
120
# Copyright (C) 2008-2009 Foswiki Contributors. All Rights Reserved.
121
# Foswiki Contributors are listed in the AUTHORS file in the root
122
# of this distribution. NOTE: Please extend that file, not this notice.
124
# Additional copyrights apply to some or all of the code in this
127
# Copyright (C) 2000-2007 TWiki Contributors. All Rights Reserved.
128
# TWiki Contributors are listed in the AUTHORS file in the root
129
# of this distribution. NOTE: Please extend that file, not this notice.
131
# This program is free software; you can redistribute it and/or
132
# modify it under the terms of the GNU General Public License
133
# as published by the Free Software Foundation; either version 2
134
# of the License, or (at your option) any later version. For
135
# more details read LICENSE in the root of this distribution.
137
# This program is distributed in the hope that it will be useful,
138
# but WITHOUT ANY WARRANTY; without even the implied warranty of
139
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
141
# As per the GPL, removal of this notice is prohibited.