Filename | /var/www/foswiki11/lib/Foswiki/LineIterator.pm |
Statements | Executed 8 statements in 346µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 22µs | 37µs | BEGIN@14 | Foswiki::LineIterator::
1 | 1 | 1 | 10µs | 15µs | BEGIN@15 | Foswiki::LineIterator::
1 | 1 | 1 | 4µs | 4µs | BEGIN@17 | Foswiki::LineIterator::
0 | 0 | 0 | 0s | 0s | hasNext | Foswiki::LineIterator::
0 | 0 | 0 | 0s | 0s | new | Foswiki::LineIterator::
0 | 0 | 0 | 0s | 0s | next | Foswiki::LineIterator::
0 | 0 | 0 | 0s | 0s | reset | Foswiki::LineIterator::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
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 | |||||
14 | 2 | 37µs | 2 | 52µs | # spent 37µs (22+15) within Foswiki::LineIterator::BEGIN@14 which was called:
# once (22µs+15µs) by Foswiki::logger at line 14 # spent 37µs making 1 call to Foswiki::LineIterator::BEGIN@14
# spent 15µs making 1 call to strict::import |
15 | 2 | 25µs | 2 | 21µs | # spent 15µs (10+6) within Foswiki::LineIterator::BEGIN@15 which was called:
# once (10µs+6µs) by Foswiki::logger at line 15 # spent 15µs making 1 call to Foswiki::LineIterator::BEGIN@15
# spent 6µs making 1 call to warnings::import |
16 | |||||
17 | 2 | 270µs | 1 | 4µs | # spent 4µs within Foswiki::LineIterator::BEGIN@17 which was called:
# once (4µs+0s) by Foswiki::logger at line 17 # spent 4µs making 1 call to Foswiki::LineIterator::BEGIN@17 |
18 | 1 | 11µs | our @ISA = ('Foswiki::Iterator'); | ||
19 | |||||
20 | =begin TML | ||||
21 | |||||
22 | ---++ new( $fh ) | ||||
23 | |||||
24 | Create a new iterator over the given file handle. | ||||
25 | |||||
26 | =cut | ||||
27 | |||||
28 | sub new { | ||||
29 | my ( $class, $fh ) = @_; | ||||
30 | my $this = bless( | ||||
31 | { | ||||
32 | nextLine => undef, | ||||
33 | handle => $fh, | ||||
34 | }, | ||||
35 | $class | ||||
36 | ); | ||||
37 | Foswiki::LineIterator::next($this); | ||||
38 | $this->{process} = undef; | ||||
39 | $this->{filter} = undef; | ||||
40 | |||||
41 | return $this; | ||||
42 | } | ||||
43 | |||||
44 | =begin TML | ||||
45 | |||||
46 | ---++ hasNext() -> $boolean | ||||
47 | |||||
48 | Returns false when the iterator is exhausted. | ||||
49 | |||||
50 | <verbatim> | ||||
51 | my $it = new Foswiki::ListIterator(\@list); | ||||
52 | while ($it->hasNext()) { | ||||
53 | ... | ||||
54 | </verbatim> | ||||
55 | |||||
56 | =cut | ||||
57 | |||||
58 | sub hasNext { | ||||
59 | my $this = shift; | ||||
60 | return defined( $this->{nextLine} ); | ||||
61 | } | ||||
62 | |||||
63 | =begin TML | ||||
64 | |||||
65 | ---++ next() -> $data | ||||
66 | |||||
67 | Return the next line in the file. | ||||
68 | |||||
69 | The iterator object can be customised to pre- and post-process entries from | ||||
70 | the list before returning them. This is done by setting two fields in the | ||||
71 | iterator object: | ||||
72 | |||||
73 | * ={filter}= can be defined to be a sub that filters each entry. The entry | ||||
74 | will be ignored (next() will not return it) if the filter returns false. | ||||
75 | * ={process}= can be defined to be a sub to process each entry before it | ||||
76 | is returned by next. The value returned from next is the value returned | ||||
77 | by the process function. | ||||
78 | |||||
79 | For example, | ||||
80 | <verbatim> | ||||
81 | my $it = new Foswiki::LineIterator("/etc/passwd"); | ||||
82 | $it->{filter} = sub { $_[0] =~ /^.*?:/; return $1; }; | ||||
83 | $it->{process} = sub { return "User $_[0]"; }; | ||||
84 | while ($it->hasNext()) { | ||||
85 | my $x = $it->next(); | ||||
86 | print "$x\n"; | ||||
87 | } | ||||
88 | </verbatim> | ||||
89 | |||||
90 | =cut | ||||
91 | |||||
92 | sub next { | ||||
93 | my ($this) = @_; | ||||
94 | my $curLine = $this->{nextLine}; | ||||
95 | local $/ = "\n"; | ||||
96 | while (1) { | ||||
97 | my $h = $this->{handle}; | ||||
98 | $this->{nextLine} = <$h>; | ||||
99 | if ( !defined( $this->{nextLine} ) ) { | ||||
100 | last; | ||||
101 | } | ||||
102 | else { | ||||
103 | chomp( $this->{nextLine} ); | ||||
104 | } | ||||
105 | last if !$this->{filter}; | ||||
106 | last unless &{ $this->{filter} }( $this->{nextLine} ); | ||||
107 | } | ||||
108 | $curLine = &{ $this->{process} }($curLine) | ||||
109 | if defined $curLine && $this->{process}; | ||||
110 | return $curLine; | ||||
111 | } | ||||
112 | |||||
113 | # See Foswiki::Iterator for a description of the general iterator contract | ||||
114 | sub reset { | ||||
115 | my ($this) = @_; | ||||
116 | |||||
117 | return; | ||||
118 | } | ||||
119 | |||||
120 | 1 | 3µs | 1; | ||
121 | __END__ |