1
# See bottom of file for license and copyright information
5
---+ package Foswiki::LineIterator
6
*implements* Foswiki::Iterator
8
Iterator over the lines read from a file handle.
12
package Foswiki::LineIterator;
13
use base 'Foswiki::Iterator';
21
Create a new iterator over the given file handle.
26
my ( $class, $fh ) = @_;
31
Foswiki::LineIterator::next($this);
32
$this->{process} = undef;
33
$this->{filter} = undef;
40
---++ hasNext() -> $boolean
42
Returns false when the iterator is exhausted.
45
my $it = new Foswiki::ListIterator(\@list);
46
while ($it->hasNext()) {
54
return defined( $this->{nextLine} );
61
Return the next line in the file.
63
The iterator object can be customised to pre- and post-process entries from
64
the list before returning them. This is done by setting two fields in the
67
* ={filter}= can be defined to be a sub that filters each entry. The entry
68
will be ignored (next() will not return it) if the filter returns false.
69
* ={process}= can be defined to be a sub to process each entry before it
70
is returned by next. The value returned from next is the value returned
71
by the process function.
75
my $it = new Foswiki::LineIterator("/etc/passwd");
76
$it->{filter} = sub { $_[0] =~ /^.*?:/; return $1; };
77
$it->{process} = sub { return "User $_[0]"; };
78
while ($it->hasNext()) {
88
my $curLine = $this->{nextLine};
91
my $h = $this->{handle};
92
$this->{nextLine} = <$h>;
93
if ( !defined( $this->{nextLine} ) ) {
97
chomp( $this->{nextLine} );
99
last if !$this->{filter};
100
last unless &{ $this->{filter} }( $this->{nextLine} );
102
$curLine = &{ $this->{process} }($curLine) if
103
defined $curLine && $this->{process};
109
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
111
# Copyright (C) 2008-2009 Foswiki Contributors. All Rights Reserved.
112
# Foswiki Contributors are listed in the AUTHORS file in the root
113
# of this distribution. NOTE: Please extend that file, not this notice.
115
# Additional copyrights apply to some or all of the code in this
118
# Copyright (C) 2005-2007 TWiki Contributors. All Rights Reserved.
119
# TWiki Contributors are listed in the AUTHORS file in the root
120
# of this distribution. NOTE: Please extend that file, not this notice.
122
# This program is free software; you can redistribute it and/or
123
# modify it under the terms of the GNU General Public License
124
# as published by the Free Software Foundation; either version 2
125
# of the License, or (at your option) any later version. For
126
# more details read LICENSE in the root of this distribution.
128
# This program is distributed in the hope that it will be useful,
129
# but WITHOUT ANY WARRANTY; without even the implied warranty of
130
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
132
# As per the GPL, removal of this notice is prohibited.