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