lib/Template/Iterator.pm

filter: Statementstmt 96.7% 88 / 91 Branchbran 88.8% 32 / 36 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 22 / 22 Podpod 100% 13 / 13 totaltotal 92.7% 165 / 178 CC 22 SCAR 12.2
CC
22
Cov
93%
new
7.0
get_all
6.0
get_next
4.2
CRAP 3.4
« lib/Template/Grammar.pm ↑ Summary lib/Template/Plugin/Filter.pm »
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-2022 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 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 32 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 32 ▶use Template::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 32 ▶use Template::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 32 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 32 ▶use constant ODD => 'odd';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 32 ▶use constant EVEN => 'even';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 32 our $VERSION = '3.106';
35 32 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Iterator::DEBUG 32 0 32
36 32 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 279 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
51 279 my $class = shift;
52 279 ▶ my $data = shift || [ ];
Condition100%2/2
shift() || []
l!l
24237
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
53 279 ▶ my $params = shift || { };
Condition50%1/2
shift() || {}
l!l
0279
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
54  
55 279 ▶ if (ref $data eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 3 276 279
elsif (builtin::blessed($data) and $data->can('as_list')) { } 2 274 276
elsif (ref $data ne 'ARRAY') { } 4 270 274
56 # map a hash into a list of { key => ???, value => ??? } hashes,
57 # one for each key, sorted by keys
58 $data = [
59 3 map { { key => $_, value => $data->{ $_ } } }
60 sort keys %$data
61 ];
62 }
63 ▶ elsif (blessed($data) && $data->can('as_list')) {
Condition66%2/3
builtin::blessed($data) and $data->can("as_list")
!ll&&!rl&&r
27402
MC/DC50%1/2
builtin::blessed($data) and $data->can("as_list")
builtin::blessed($data) $data->can("as_list")
Truth table66%2/3
A and B
A= builtin::blessed($data)B= $data->can("as_list")
ABresult
0X0
100
111
64 2 $data = $data->as_list();
65 }
66 elsif (ref $data ne 'ARRAY') {
67 # coerce any non-list data into an array reference
68 4 $data = [ $data ] ;
69 }
70  
71 bless {
72 279 _DATA => $data,
73 _ERROR => '',
74 }, $class;
75 }
76  
77  
78 #========================================================================
79 # ----- PUBLIC OBJECT METHODS -----
80 #========================================================================
81  
82 #------------------------------------------------------------------------
83 # get_first()
84 #
85 # Initialises the object for iterating through the target data set. The
86 # first record is returned, if defined, along with the STATUS_OK value.
87 # If there is no target data, or the data is an empty set, then undef
88 # is returned with the STATUS_DONE value.
89 #------------------------------------------------------------------------
90  
91 281 ▶sub get_first {
Subroutine get_first: called CC=3 SCAR=11.0
Pod documented
92 281 my $self = shift;
93 281 my $data = $self->{ _DATA };
94  
95 281 $self->{ _DATASET } = $self->{ _DATA };
96 281 my $size = scalar @$data;
97 281 my $index = 0;
98  
99 281 ▶ return (undef, Template::Constants::STATUS_DONE) unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 41 240 281
100  
101 # initialise various counters, flags, etc.
102 240 ▶ @$self{ qw( SIZE MAX INDEX COUNT FIRST LAST ) }
Branch: 1 branch
BranchTrueFalseTotal
$size > 1 ? : 164 76 240
103 = ( $size, $size - 1, $index, 1, 1, $size > 1 ? 0 : 1, undef );
104 240 @$self{ qw( PREV NEXT ) } = ( undef, $self->{ _DATASET }->[ $index + 1 ]);
105  
106 240 return $self->{ _DATASET }->[ $index ];
107 }
108  
109  
110  
111 #------------------------------------------------------------------------
112 # get_next()
113 #
114 # Called repeatedly to access successive elements in the data set.
115 # Should only be called after calling get_first() or a warning will
116 # be raised and (undef, STATUS_DONE) returned.
117 #------------------------------------------------------------------------
118  
119 667 ▶sub get_next {
Subroutine get_next: called CC=4 SCAR=14.3
Pod documented
120 667 my ( $max, $index ) = @{ $_[0] }{qw( MAX INDEX )};
121  
122 # warn about incorrect usage
123 667 ▶ if ( !defined $index ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $index) { } 0 667 667
elsif ($index >= $max) { } 226 441 667
124 0 my ( $pack, $file, $line ) = caller();
125 0 warn("iterator get_next() called before get_first() at $file line $line\n");
126 0 return ( undef, Template::Constants::STATUS_DONE ); ## RETURN ##
127 }
128  
129 # if there's still some data to go...
130 elsif ( $index >= $max ) {
131 226 return ( undef, Template::Constants::STATUS_DONE ); ## RETURN ##
132 }
133  
134 441 my $self = shift;
135 441 my $dataset = $self->{_DATASET};
136  
137 441 $index++;
138  
139 # update counters and flags
140 441 ▶ @$self{qw( INDEX COUNT FIRST LAST PREV NEXT )} = (
Branch: 1 branch
BranchTrueFalseTotal
$index == $max ? : 153 288 441
141 $index, # INDEX
142 $index + 1, # COUNT
143 0, # FIRST
144 $index == $max ? 1 : 0, # LAST
145 @$dataset[ $index - 1, $index + 1 ] # PREV, NEXT
146 );
147  
148 441 return $dataset->[ $index ]; ## RETURN ##
149 }
150  
151  
152 #------------------------------------------------------------------------
153 # get_all()
154 #
155 # Method which returns all remaining items in the iterator as a Perl list
156 # reference. May be called at any time in the life-cycle of the iterator.
157 # The get_first() method will be called automatically if necessary, and
158 # then subsequent get_next() calls are made, storing each returned
159 # result until the list is exhausted.
160 #------------------------------------------------------------------------
161  
162 8 ▶sub get_all {
Subroutine get_all: called CC=6 SCAR=17.9
Pod documented
163 8 my $self = shift;
164 8 my ($max, $index) = @$self{ qw( MAX INDEX ) };
165 8 my @data;
166  
167 # handle cases where get_first() has yet to be called.
168 8 ▶ unless (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 3 5 8
169 3 my ($first, $status) = $self->get_first;
170  
171 # refresh $max and $index, after get_first updates MAX and INDEX
172 3 ($max, $index) = @$self{ qw( MAX INDEX ) };
173  
174 # empty lists are handled here.
175 3 ▶ if ($status && $status == Template::Constants::STATUS_DONE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status and $status == 3) 1 2 3
Condition66%2/3
$status and $status == 3
!ll&&!rl&&r
201
MC/DC50%1/2
$status and $status == 3
$status $status == 3
Truth table66%2/3
A and B
A= $statusB= $status == 3
ABresult
0X0
100
111
176 1 return (undef, Template::Constants::STATUS_DONE); ## RETURN ##
177 }
178  
179 2 push @data, $first;
180  
181 ## if there's nothing left in the iterator, return the single value.
182 2 ▶ unless ($index < $max) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($index < $max) 1 1 2
183 1 return \@data;
184 }
185 }
186  
187 # if there's still some data to go...
188 6 ▶ if ($index < $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $max) { } 3 3 6
189 3 $index++;
190 3 push @data, @{ $self->{ _DATASET } } [ $index..$max ];
191  
192 # update counters and flags
193 3 @$self{ qw( INDEX COUNT FIRST LAST ) }
194 = ( $max, $max + 1, 0, 1 );
195  
196 3 return \@data; ## RETURN ##
197 }
198 else {
199 3 return (undef, Template::Constants::STATUS_DONE); ## RETURN ##
200 }
201 }
202  
203 10 ▶sub odd {
Subroutine odd: called CC=2 SCAR=6.9
Pod documented
204 10 ▶ shift->{ COUNT } % 2 ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'COUNT'} % 2 ? : 5 5 10
205 }
206  
207 10 ▶sub even {
Subroutine even: called CC=2 SCAR=6.9
Pod documented
208 10 ▶ shift->{ COUNT } % 2 ? 0 : 1
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'COUNT'} % 2 ? : 5 5 10
209 }
210  
211 10 ▶sub parity {
Subroutine parity: called CC=2 SCAR=6.9
Pod documented
212 10 ▶ shift->{ COUNT } % 2 ? ODD : EVEN;
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{'COUNT'} % 2 ? : 5 5 10
213 }
214  
215 16 ▶sub index {
Subroutine index: called CC=1 SCAR=0.0
Pod documented
216 16 return $_[0]->{INDEX};
217 }
218  
219 18 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
220 18 return $_[0]->{COUNT};
221 }
222  
223 8 ▶sub number { # This is here for backward compatibility per sub AUTOLOAD
Subroutine number: called CC=1 SCAR=0.0
Pod documented
224 8 return $_[0]->{COUNT};
225 }
226  
227 15 ▶sub first {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
228 15 return $_[0]->{FIRST};
229 }
230  
231 35 ▶sub last {
Subroutine last: called CC=1 SCAR=0.0
Pod documented
232 35 return $_[0]->{LAST};
233 }
234  
235 14 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
236 14 return $_[0]->{SIZE};
237 }
238  
239 #------------------------------------------------------------------------
240 # AUTOLOAD
241 #
242 # Provides access to internal fields (e.g. prev, next, etc)
243 #------------------------------------------------------------------------
244  
245 18 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
246 18 my $self = shift;
247 18 my $item = $AUTOLOAD;
248 18 $item =~ s/.*:://;
249 18 ▶ return if $item eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "DESTROY" 0 18 18
250  
251 # alias NUMBER to COUNT for backwards compatibility
252 18 ▶ $item = 'COUNT' if CORE::index(uc $item,'NUMBER') > -1;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::index(uc $item, "NUMBER") != -1 0 18 18
253  
254 18 return $self->{ uc $item };
255 }
256  
257 32 1;
258  
259 __END__
« lib/Template/Grammar.pm ↑ Summary lib/Template/Plugin/Filter.pm »