Filename | /usr/lib/x86_64-linux-gnu/perl5/5.20/Template/Iterator.pm |
Statements | Executed 49 statements in 1.39ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 21µs | 33µs | BEGIN@24 | Template::Iterator::
1 | 1 | 1 | 15µs | 54µs | BEGIN@27 | Template::Iterator::
1 | 1 | 1 | 12µs | 69µs | BEGIN@26 | Template::Iterator::
1 | 1 | 1 | 11µs | 32µs | BEGIN@29 | Template::Iterator::
1 | 1 | 1 | 11µs | 20µs | BEGIN@25 | Template::Iterator::
1 | 1 | 1 | 11µs | 12µs | new | Template::Iterator::
1 | 1 | 1 | 11µs | 41µs | BEGIN@31 | Template::Iterator::
2 | 1 | 1 | 10µs | 10µs | get_next | Template::Iterator::
1 | 1 | 1 | 10µs | 10µs | get_first | Template::Iterator::
1 | 1 | 1 | 9µs | 9µs | BEGIN@28 | Template::Iterator::
1 | 1 | 1 | 7µs | 22µs | BEGIN@32 | Template::Iterator::
0 | 0 | 0 | 0s | 0s | AUTOLOAD | Template::Iterator::
0 | 0 | 0 | 0s | 0s | _dump | Template::Iterator::
0 | 0 | 0 | 0s | 0s | even | Template::Iterator::
0 | 0 | 0 | 0s | 0s | get_all | Template::Iterator::
0 | 0 | 0 | 0s | 0s | odd | Template::Iterator::
0 | 0 | 0 | 0s | 0s | parity | Template::Iterator::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | #============================================================= -*-Perl-*- | ||||
2 | # | ||||
3 | # Template::Iterator | ||||
4 | # | ||||
5 | # DESCRIPTION | ||||
6 | # | ||||
7 | # Module defining an iterator class which is used by the FOREACH | ||||
8 | # directive for iterating through data sets. This may be | ||||
9 | # sub-classed to define more specific iterator types. | ||||
10 | # | ||||
11 | # AUTHOR | ||||
12 | # Andy Wardley <abw@wardley.org> | ||||
13 | # | ||||
14 | # COPYRIGHT | ||||
15 | # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. | ||||
16 | # | ||||
17 | # This module is free software; you can redistribute it and/or | ||||
18 | # modify it under the same terms as Perl itself. | ||||
19 | # | ||||
20 | #============================================================================ | ||||
21 | |||||
22 | package Template::Iterator; | ||||
23 | |||||
24 | 2 | 50µs | 2 | 46µs | # spent 33µs (21+12) within Template::Iterator::BEGIN@24 which was called:
# once (21µs+12µs) by Template::Config::load at line 24 # spent 33µs making 1 call to Template::Iterator::BEGIN@24
# spent 12µs making 1 call to strict::import |
25 | 2 | 43µs | 2 | 30µs | # spent 20µs (11+10) within Template::Iterator::BEGIN@25 which was called:
# once (11µs+10µs) by Template::Config::load at line 25 # spent 20µs making 1 call to Template::Iterator::BEGIN@25
# spent 10µs making 1 call to warnings::import |
26 | 2 | 94µs | 2 | 126µs | # spent 69µs (12+57) within Template::Iterator::BEGIN@26 which was called:
# once (12µs+57µs) by Template::Config::load at line 26 # spent 69µs making 1 call to Template::Iterator::BEGIN@26
# spent 57µs making 1 call to base::import |
27 | 2 | 79µs | 2 | 93µs | # spent 54µs (15+39) within Template::Iterator::BEGIN@27 which was called:
# once (15µs+39µs) by Template::Config::load at line 27 # spent 54µs making 1 call to Template::Iterator::BEGIN@27
# spent 39µs making 1 call to Exporter::import |
28 | 2 | 32µs | 1 | 9µs | # spent 9µs within Template::Iterator::BEGIN@28 which was called:
# once (9µs+0s) by Template::Config::load at line 28 # spent 9µs making 1 call to Template::Iterator::BEGIN@28 |
29 | 2 | 56µs | 2 | 53µs | # spent 32µs (11+21) within Template::Iterator::BEGIN@29 which was called:
# once (11µs+21µs) by Template::Config::load at line 29 # spent 32µs making 1 call to Template::Iterator::BEGIN@29
# spent 21µs making 1 call to Exporter::import |
30 | |||||
31 | 2 | 64µs | 2 | 71µs | # spent 41µs (11+30) within Template::Iterator::BEGIN@31 which was called:
# once (11µs+30µs) by Template::Config::load at line 31 # spent 41µs making 1 call to Template::Iterator::BEGIN@31
# spent 30µs making 1 call to constant::import |
32 | 2 | 929µs | 2 | 36µs | # spent 22µs (7+15) within Template::Iterator::BEGIN@32 which was called:
# once (7µs+15µs) by Template::Config::load at line 32 # spent 22µs making 1 call to Template::Iterator::BEGIN@32
# spent 15µs making 1 call to constant::import |
33 | |||||
34 | 1 | 300ns | our $VERSION = 2.68; | ||
35 | 1 | 300ns | our $DEBUG = 0 unless defined $DEBUG; | ||
36 | 1 | 100ns | our $AUTOLOAD; | ||
37 | |||||
38 | #======================================================================== | ||||
39 | # ----- CLASS METHODS ----- | ||||
40 | #======================================================================== | ||||
41 | |||||
42 | #------------------------------------------------------------------------ | ||||
43 | # new(\@target, \%options) | ||||
44 | # | ||||
45 | # Constructor method which creates and returns a reference to a new | ||||
46 | # Template::Iterator object. A reference to the target data (array | ||||
47 | # or hash) may be passed for the object to iterate through. | ||||
48 | #------------------------------------------------------------------------ | ||||
49 | |||||
50 | # spent 12µs (11+900ns) within Template::Iterator::new which was called:
# once (11µs+900ns) by Template::Config::iterator at line 177 of Template/Config.pm | ||||
51 | 1 | 500ns | my $class = shift; | ||
52 | 1 | 300ns | my $data = shift || [ ]; | ||
53 | 1 | 500ns | my $params = shift || { }; | ||
54 | |||||
55 | 1 | 6µs | 1 | 900ns | if (ref $data eq 'HASH') { # spent 900ns making 1 call to Scalar::Util::blessed |
56 | # map a hash into a list of { key => ???, value => ??? } hashes, | ||||
57 | # one for each key, sorted by keys | ||||
58 | $data = [ map { { key => $_, value => $data->{ $_ } } } | ||||
59 | sort keys %$data ]; | ||||
60 | } | ||||
61 | elsif (blessed($data) && $data->can('as_list')) { | ||||
62 | $data = $data->as_list(); | ||||
63 | } | ||||
64 | elsif (ref $data ne 'ARRAY') { | ||||
65 | # coerce any non-list data into an array reference | ||||
66 | $data = [ $data ] ; | ||||
67 | } | ||||
68 | |||||
69 | bless { | ||||
70 | 1 | 6µs | _DATA => $data, | ||
71 | _ERROR => '', | ||||
72 | }, $class; | ||||
73 | } | ||||
74 | |||||
75 | |||||
76 | #======================================================================== | ||||
77 | # ----- PUBLIC OBJECT METHODS ----- | ||||
78 | #======================================================================== | ||||
79 | |||||
80 | #------------------------------------------------------------------------ | ||||
81 | # get_first() | ||||
82 | # | ||||
83 | # Initialises the object for iterating through the target data set. The | ||||
84 | # first record is returned, if defined, along with the STATUS_OK value. | ||||
85 | # If there is no target data, or the data is an empty set, then undef | ||||
86 | # is returned with the STATUS_DONE value. | ||||
87 | #------------------------------------------------------------------------ | ||||
88 | |||||
89 | # spent 10µs within Template::Iterator::get_first which was called:
# once (10µs+0s) by Template::Document::__ANON__[/home/vagrant/kohaclone/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt:203] at line 197 of koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt | ||||
90 | 1 | 200ns | my $self = shift; | ||
91 | 1 | 400ns | my $data = $self->{ _DATA }; | ||
92 | |||||
93 | 1 | 400ns | $self->{ _DATASET } = $self->{ _DATA }; | ||
94 | 1 | 500ns | my $size = scalar @$data; | ||
95 | 1 | 100ns | my $index = 0; | ||
96 | |||||
97 | 1 | 100ns | return (undef, Template::Constants::STATUS_DONE) unless $size; | ||
98 | |||||
99 | # initialise various counters, flags, etc. | ||||
100 | 1 | 5µs | @$self{ qw( SIZE MAX INDEX COUNT FIRST LAST ) } | ||
101 | = ( $size, $size - 1, $index, 1, 1, $size > 1 ? 0 : 1, undef ); | ||||
102 | 1 | 2µs | @$self{ qw( PREV NEXT ) } = ( undef, $self->{ _DATASET }->[ $index + 1 ]); | ||
103 | |||||
104 | 1 | 5µs | return $self->{ _DATASET }->[ $index ]; | ||
105 | } | ||||
106 | |||||
- - | |||||
109 | #------------------------------------------------------------------------ | ||||
110 | # get_next() | ||||
111 | # | ||||
112 | # Called repeatedly to access successive elements in the data set. | ||||
113 | # Should only be called after calling get_first() or a warning will | ||||
114 | # be raised and (undef, STATUS_DONE) returned. | ||||
115 | #------------------------------------------------------------------------ | ||||
116 | |||||
117 | # spent 10µs within Template::Iterator::get_next which was called 2 times, avg 5µs/call:
# 2 times (10µs+0s) by Template::Document::__ANON__[/home/vagrant/kohaclone/koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt:203] at line 186 of koha-tmpl/intranet-tmpl/prog/en/modules/intranet-main.tt, avg 5µs/call | ||||
118 | 2 | 600ns | my $self = shift; | ||
119 | 2 | 1µs | my ($max, $index) = @$self{ qw( MAX INDEX ) }; | ||
120 | 2 | 800ns | my $data = $self->{ _DATASET }; | ||
121 | |||||
122 | # warn about incorrect usage | ||||
123 | 2 | 400ns | unless (defined $index) { | ||
124 | my ($pack, $file, $line) = caller(); | ||||
125 | warn("iterator get_next() called before get_first() at $file line $line\n"); | ||||
126 | return (undef, Template::Constants::STATUS_DONE); ## RETURN ## | ||||
127 | } | ||||
128 | |||||
129 | # if there's still some data to go... | ||||
130 | 2 | 800ns | if ($index < $max) { | ||
131 | # update counters and flags | ||||
132 | 1 | 100ns | $index++; | ||
133 | 1 | 1µs | @$self{ qw( INDEX COUNT FIRST LAST ) } | ||
134 | = ( $index, $index + 1, 0, $index == $max ? 1 : 0 ); | ||||
135 | 1 | 2µs | @$self{ qw( PREV NEXT ) } = @$data[ $index - 1, $index + 1 ]; | ||
136 | 1 | 5µs | return $data->[ $index ]; ## RETURN ## | ||
137 | } | ||||
138 | else { | ||||
139 | 1 | 2µs | return (undef, Template::Constants::STATUS_DONE); ## RETURN ## | ||
140 | } | ||||
141 | } | ||||
142 | |||||
143 | |||||
144 | #------------------------------------------------------------------------ | ||||
145 | # get_all() | ||||
146 | # | ||||
147 | # Method which returns all remaining items in the iterator as a Perl list | ||||
148 | # reference. May be called at any time in the life-cycle of the iterator. | ||||
149 | # The get_first() method will be called automatically if necessary, and | ||||
150 | # then subsequent get_next() calls are made, storing each returned | ||||
151 | # result until the list is exhausted. | ||||
152 | #------------------------------------------------------------------------ | ||||
153 | |||||
154 | sub get_all { | ||||
155 | my $self = shift; | ||||
156 | my ($max, $index) = @$self{ qw( MAX INDEX ) }; | ||||
157 | my @data; | ||||
158 | |||||
159 | # handle cases where get_first() has yet to be called. | ||||
160 | unless (defined $index) { | ||||
161 | my ($first, $status) = $self->get_first; | ||||
162 | |||||
163 | # refresh $max and $index, after get_first updates MAX and INDEX | ||||
164 | ($max, $index) = @$self{ qw( MAX INDEX ) }; | ||||
165 | |||||
166 | # empty lists are handled here. | ||||
167 | if ($status && $status == Template::Constants::STATUS_DONE) { | ||||
168 | return (undef, Template::Constants::STATUS_DONE); ## RETURN ## | ||||
169 | } | ||||
170 | |||||
171 | push @data, $first; | ||||
172 | |||||
173 | ## if there's nothing left in the iterator, return the single value. | ||||
174 | unless ($index < $max) { | ||||
175 | return \@data; | ||||
176 | } | ||||
177 | } | ||||
178 | |||||
179 | # if there's still some data to go... | ||||
180 | if ($index < $max) { | ||||
181 | $index++; | ||||
182 | push @data, @{ $self->{ _DATASET } } [ $index..$max ]; | ||||
183 | |||||
184 | # update counters and flags | ||||
185 | @$self{ qw( INDEX COUNT FIRST LAST ) } | ||||
186 | = ( $max, $max + 1, 0, 1 ); | ||||
187 | |||||
188 | return \@data; ## RETURN ## | ||||
189 | } | ||||
190 | else { | ||||
191 | return (undef, Template::Constants::STATUS_DONE); ## RETURN ## | ||||
192 | } | ||||
193 | } | ||||
194 | |||||
195 | sub odd { | ||||
196 | shift->{ COUNT } % 2 ? 1 : 0 | ||||
197 | } | ||||
198 | |||||
199 | sub even { | ||||
200 | shift->{ COUNT } % 2 ? 0 : 1 | ||||
201 | } | ||||
202 | |||||
203 | sub parity { | ||||
204 | shift->{ COUNT } % 2 ? ODD : EVEN; | ||||
205 | } | ||||
206 | |||||
207 | |||||
208 | #------------------------------------------------------------------------ | ||||
209 | # AUTOLOAD | ||||
210 | # | ||||
211 | # Provides access to internal fields (e.g. size, first, last, max, etc) | ||||
212 | #------------------------------------------------------------------------ | ||||
213 | |||||
214 | sub AUTOLOAD { | ||||
215 | my $self = shift; | ||||
216 | my $item = $AUTOLOAD; | ||||
217 | $item =~ s/.*:://; | ||||
218 | return if $item eq 'DESTROY'; | ||||
219 | |||||
220 | # alias NUMBER to COUNT for backwards compatability | ||||
221 | $item = 'COUNT' if $item =~ /NUMBER/i; | ||||
222 | |||||
223 | return $self->{ uc $item }; | ||||
224 | } | ||||
225 | |||||
226 | |||||
227 | #======================================================================== | ||||
228 | # ----- PRIVATE DEBUG METHODS ----- | ||||
229 | #======================================================================== | ||||
230 | |||||
231 | #------------------------------------------------------------------------ | ||||
232 | # _dump() | ||||
233 | # | ||||
234 | # Debug method which returns a string detailing the internal state of | ||||
235 | # the iterator object. | ||||
236 | #------------------------------------------------------------------------ | ||||
237 | |||||
238 | sub _dump { | ||||
239 | my $self = shift; | ||||
240 | join('', | ||||
241 | " Data: ", $self->{ _DATA }, "\n", | ||||
242 | " Index: ", $self->{ INDEX }, "\n", | ||||
243 | "Number: ", $self->{ NUMBER }, "\n", | ||||
244 | " Max: ", $self->{ MAX }, "\n", | ||||
245 | " Size: ", $self->{ SIZE }, "\n", | ||||
246 | " First: ", $self->{ FIRST }, "\n", | ||||
247 | " Last: ", $self->{ LAST }, "\n", | ||||
248 | "\n" | ||||
249 | ); | ||||
250 | } | ||||
251 | |||||
252 | |||||
253 | 1 | 4µs | 1; | ||
254 | |||||
255 | __END__ |