~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/LineIterator.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# See bottom of file for license and copyright information
 
2
 
 
3
=begin TML
 
4
 
 
5
---+ package Foswiki::LineIterator
 
6
*implements* Foswiki::Iterator
 
7
 
 
8
Iterator over the lines read from a file handle.
 
9
 
 
10
=cut
 
11
 
 
12
package Foswiki::LineIterator;
 
13
use base 'Foswiki::Iterator';
 
14
 
 
15
use strict;
 
16
 
 
17
=begin TML
 
18
 
 
19
---++ new( $fh )
 
20
 
 
21
Create a new iterator over the given file handle.
 
22
 
 
23
=cut
 
24
 
 
25
sub new {
 
26
    my ( $class, $fh ) = @_;
 
27
    my $this = bless( {
 
28
        nextLine => undef,
 
29
        handle => $fh,
 
30
    }, $class );
 
31
    Foswiki::LineIterator::next($this);
 
32
    $this->{process} = undef;
 
33
    $this->{filter}  = undef;
 
34
 
 
35
    return $this;
 
36
}
 
37
 
 
38
=begin TML
 
39
 
 
40
---++ hasNext() -> $boolean
 
41
 
 
42
Returns false when the iterator is exhausted.
 
43
 
 
44
<verbatim>
 
45
my $it = new Foswiki::ListIterator(\@list);
 
46
while ($it->hasNext()) {
 
47
   ...
 
48
</verbatim>
 
49
 
 
50
=cut
 
51
 
 
52
sub hasNext {
 
53
    my $this = shift;
 
54
    return defined( $this->{nextLine} );
 
55
}
 
56
 
 
57
=begin TML
 
58
 
 
59
---++ next() -> $data
 
60
 
 
61
Return the next line in the file.
 
62
 
 
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
 
65
iterator object:
 
66
 
 
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.
 
72
 
 
73
For example,
 
74
<verbatim>
 
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()) {
 
79
    my $x = $it->next();
 
80
    print "$x\n";
 
81
}
 
82
</verbatim>
 
83
 
 
84
=cut
 
85
 
 
86
sub next {
 
87
    my ($this)  = @_;
 
88
    my $curLine = $this->{nextLine};
 
89
    local $/ = "\n";
 
90
    while (1) {
 
91
        my $h = $this->{handle};
 
92
        $this->{nextLine} = <$h>;
 
93
        if ( !defined( $this->{nextLine} ) ) {
 
94
            last;
 
95
        }
 
96
        else {
 
97
            chomp( $this->{nextLine} );
 
98
        }
 
99
        last if !$this->{filter};
 
100
        last unless &{ $this->{filter} }( $this->{nextLine} );
 
101
    }
 
102
    $curLine = &{ $this->{process} }($curLine) if
 
103
      defined $curLine && $this->{process};
 
104
    return $curLine;
 
105
}
 
106
 
 
107
1;
 
108
__DATA__
 
109
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
110
#
 
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.
 
114
#
 
115
# Additional copyrights apply to some or all of the code in this
 
116
# file as follows:
 
117
#
 
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.
 
121
#
 
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.
 
127
#
 
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.
 
131
#
 
132
# As per the GPL, removal of this notice is prohibited.