← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 19:05:14 2015
Reported on Fri Jul 31 19:08:10 2015

Filename/var/www/foswiki11/lib/Foswiki/LineIterator.pm
StatementsExecuted 8 statements in 346µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11122µs37µsFoswiki::LineIterator::::BEGIN@14Foswiki::LineIterator::BEGIN@14
11110µs15µsFoswiki::LineIterator::::BEGIN@15Foswiki::LineIterator::BEGIN@15
1114µs4µsFoswiki::LineIterator::::BEGIN@17Foswiki::LineIterator::BEGIN@17
0000s0sFoswiki::LineIterator::::hasNextFoswiki::LineIterator::hasNext
0000s0sFoswiki::LineIterator::::newFoswiki::LineIterator::new
0000s0sFoswiki::LineIterator::::nextFoswiki::LineIterator::next
0000s0sFoswiki::LineIterator::::resetFoswiki::LineIterator::reset
Call graph for these subroutines as a Graphviz dot language file.
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
8Iterator over the lines read from a file handle.
9
10=cut
11
12package Foswiki::LineIterator;
13
14237µs252µ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
use strict;
# spent 37µs making 1 call to Foswiki::LineIterator::BEGIN@14 # spent 15µs making 1 call to strict::import
15225µs221µ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
use warnings;
# spent 15µs making 1 call to Foswiki::LineIterator::BEGIN@15 # spent 6µs making 1 call to warnings::import
16
172270µs14µs
# spent 4µs within Foswiki::LineIterator::BEGIN@17 which was called: # once (4µs+0s) by Foswiki::logger at line 17
use Foswiki::Iterator ();
# spent 4µs making 1 call to Foswiki::LineIterator::BEGIN@17
18111µsour @ISA = ('Foswiki::Iterator');
19
20=begin TML
21
22---++ new( $fh )
23
24Create a new iterator over the given file handle.
25
26=cut
27
28sub 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
48Returns false when the iterator is exhausted.
49
50<verbatim>
51my $it = new Foswiki::ListIterator(\@list);
52while ($it->hasNext()) {
53 ...
54</verbatim>
55
56=cut
57
58sub hasNext {
59 my $this = shift;
60 return defined( $this->{nextLine} );
61}
62
63=begin TML
64
65---++ next() -> $data
66
67Return the next line in the file.
68
69The iterator object can be customised to pre- and post-process entries from
70the list before returning them. This is done by setting two fields in the
71iterator 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
79For example,
80<verbatim>
81my $it = new Foswiki::LineIterator("/etc/passwd");
82$it->{filter} = sub { $_[0] =~ /^.*?:/; return $1; };
83$it->{process} = sub { return "User $_[0]"; };
84while ($it->hasNext()) {
85 my $x = $it->next();
86 print "$x\n";
87}
88</verbatim>
89
90=cut
91
92sub 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
114sub reset {
115 my ($this) = @_;
116
117 return;
118}
119
12013µs1;
121__END__