← Index
NYTProf Performance Profile   « line view »
For ./view
  Run on Fri Jul 31 18:42:36 2015
Reported on Fri Jul 31 18:48:15 2015

Filename/var/www/foswikidev/core/lib/Foswiki/LineIterator.pm
StatementsExecuted 9 statements in 379µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11114µs27µsFoswiki::LineIterator::::BEGIN@14Foswiki::LineIterator::BEGIN@14
1119µs13µsFoswiki::LineIterator::::BEGIN@15Foswiki::LineIterator::BEGIN@15
1117µs7µsFoswiki::LineIterator::::BEGIN@17Foswiki::LineIterator::BEGIN@17
1114µs4µsFoswiki::LineIterator::::BEGIN@20Foswiki::LineIterator::BEGIN@20
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
14227µs240µ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
use strict;
# spent 27µs making 1 call to Foswiki::LineIterator::BEGIN@14 # spent 13µs making 1 call to strict::import
15228µs217µ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
use warnings;
# spent 13µs making 1 call to Foswiki::LineIterator::BEGIN@15 # spent 4µs making 1 call to warnings::import
16
17252µs17µ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
use Foswiki::Iterator ();
# spent 7µs making 1 call to Foswiki::LineIterator::BEGIN@17
1819µsour @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
BEGIN {
2115µs if ( $Foswiki::cfg{UseLocale} ) {
22 require locale;
23 import locale();
24 }
251255µs14µs}
# spent 4µs making 1 call to Foswiki::LineIterator::BEGIN@20
26
27=begin TML
28
29---++ new( $fh )
30
31Create a new iterator over the given file handle.
32
33=cut
34
35sub 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
55Returns false when the iterator is exhausted.
56
57<verbatim>
58my $it = new Foswiki::ListIterator(\@list);
59while ($it->hasNext()) {
60 ...
61</verbatim>
62
63=cut
64
65sub hasNext {
66 my $this = shift;
67 return defined( $this->{nextLine} );
68}
69
70=begin TML
71
72---++ next() -> $data
73
74Return the next line in the file.
75
76The iterator object can be customised to pre- and post-process entries from
77the list before returning them. This is done by setting two fields in the
78iterator 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
86For example,
87<verbatim>
88my $it = new Foswiki::LineIterator("/etc/passwd");
89$it->{filter} = sub { $_[0] =~ m/^.*?:/; return $1; };
90$it->{process} = sub { return "User $_[0]"; };
91while ($it->hasNext()) {
92 my $x = $it->next();
93 print "$x\n";
94}
95</verbatim>
96
97=cut
98
99sub 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
121sub reset {
122 my ($this) = @_;
123
124 return;
125}
126
12713µs1;
128__END__