line |
stmt |
bran |
cond |
sub |
pod |
time |
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 |
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
|
29
|
|
|
29
|
|
425
|
use strict; |
|
29
|
|
|
|
|
32
|
|
|
29
|
|
|
|
|
752
|
|
25
|
29
|
|
|
29
|
|
91
|
use warnings; |
|
29
|
|
|
|
|
31
|
|
|
29
|
|
|
|
|
805
|
|
26
|
29
|
|
|
29
|
|
89
|
use base 'Template::Base'; |
|
29
|
|
|
|
|
32
|
|
|
29
|
|
|
|
|
2016
|
|
27
|
29
|
|
|
29
|
|
106
|
use Template::Constants; |
|
29
|
|
|
|
|
35
|
|
|
29
|
|
|
|
|
1016
|
|
28
|
29
|
|
|
29
|
|
94
|
use Template::Exception; |
|
29
|
|
|
|
|
46
|
|
|
29
|
|
|
|
|
542
|
|
29
|
29
|
|
|
29
|
|
100
|
use Scalar::Util qw(blessed); |
|
29
|
|
|
|
|
33
|
|
|
29
|
|
|
|
|
1182
|
|
30
|
|
|
|
|
|
|
|
31
|
29
|
|
|
29
|
|
96
|
use constant ODD => 'odd'; |
|
29
|
|
|
|
|
31
|
|
|
29
|
|
|
|
|
1700
|
|
32
|
29
|
|
|
29
|
|
96
|
use constant EVEN => 'even'; |
|
29
|
|
|
|
|
37
|
|
|
29
|
|
|
|
|
20730
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
our $VERSION = 2.68; |
35
|
|
|
|
|
|
|
our $DEBUG = 0 unless defined $DEBUG; |
36
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub new { |
51
|
179
|
|
|
179
|
1
|
199
|
my $class = shift; |
52
|
179
|
|
100
|
|
|
320
|
my $data = shift || [ ]; |
53
|
179
|
|
50
|
|
|
589
|
my $params = shift || { }; |
54
|
|
|
|
|
|
|
|
55
|
179
|
100
|
66
|
|
|
878
|
if (ref $data eq 'HASH') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# map a hash into a list of { key => ???, value => ??? } hashes, |
57
|
|
|
|
|
|
|
# one for each key, sorted by keys |
58
|
3
|
|
|
|
|
19
|
$data = [ map { { key => $_, value => $data->{ $_ } } } |
|
8
|
|
|
|
|
24
|
|
59
|
|
|
|
|
|
|
sort keys %$data ]; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
elsif (blessed($data) && $data->can('as_list')) { |
62
|
2
|
|
|
|
|
6
|
$data = $data->as_list(); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
elsif (ref $data ne 'ARRAY') { |
65
|
|
|
|
|
|
|
# coerce any non-list data into an array reference |
66
|
4
|
|
|
|
|
6
|
$data = [ $data ] ; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
bless { |
70
|
179
|
|
|
|
|
879
|
_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
|
|
|
|
|
|
|
sub get_first { |
90
|
181
|
|
|
181
|
1
|
464
|
my $self = shift; |
91
|
181
|
|
|
|
|
188
|
my $data = $self->{ _DATA }; |
92
|
|
|
|
|
|
|
|
93
|
181
|
|
|
|
|
200
|
$self->{ _DATASET } = $self->{ _DATA }; |
94
|
181
|
|
|
|
|
175
|
my $size = scalar @$data; |
95
|
181
|
|
|
|
|
136
|
my $index = 0; |
96
|
|
|
|
|
|
|
|
97
|
181
|
100
|
|
|
|
275
|
return (undef, Template::Constants::STATUS_DONE) unless $size; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# initialise various counters, flags, etc. |
100
|
176
|
100
|
|
|
|
857
|
@$self{ qw( SIZE MAX INDEX COUNT FIRST LAST ) } |
101
|
|
|
|
|
|
|
= ( $size, $size - 1, $index, 1, 1, $size > 1 ? 0 : 1, undef ); |
102
|
176
|
|
|
|
|
381
|
@$self{ qw( PREV NEXT ) } = ( undef, $self->{ _DATASET }->[ $index + 1 ]); |
103
|
|
|
|
|
|
|
|
104
|
176
|
|
|
|
|
301
|
return $self->{ _DATASET }->[ $index ]; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub get_next { |
118
|
604
|
|
|
604
|
1
|
5909
|
my $self = shift; |
119
|
604
|
|
|
|
|
612
|
my ($max, $index) = @$self{ qw( MAX INDEX ) }; |
120
|
604
|
|
|
|
|
490
|
my $data = $self->{ _DATASET }; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# warn about incorrect usage |
123
|
604
|
50
|
|
|
|
792
|
unless (defined $index) { |
124
|
0
|
|
|
|
|
0
|
my ($pack, $file, $line) = caller(); |
125
|
0
|
|
|
|
|
0
|
warn("iterator get_next() called before get_first() at $file line $line\n"); |
126
|
0
|
|
|
|
|
0
|
return (undef, Template::Constants::STATUS_DONE); ## RETURN ## |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# if there's still some data to go... |
130
|
604
|
100
|
|
|
|
758
|
if ($index < $max) { |
131
|
|
|
|
|
|
|
# update counters and flags |
132
|
442
|
|
|
|
|
280
|
$index++; |
133
|
442
|
100
|
|
|
|
761
|
@$self{ qw( INDEX COUNT FIRST LAST ) } |
134
|
|
|
|
|
|
|
= ( $index, $index + 1, 0, $index == $max ? 1 : 0 ); |
135
|
442
|
|
|
|
|
615
|
@$self{ qw( PREV NEXT ) } = @$data[ $index - 1, $index + 1 ]; |
136
|
442
|
|
|
|
|
725
|
return $data->[ $index ]; ## RETURN ## |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
else { |
139
|
162
|
|
|
|
|
342
|
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
|
8
|
|
|
8
|
1
|
13
|
my $self = shift; |
156
|
8
|
|
|
|
|
8
|
my ($max, $index) = @$self{ qw( MAX INDEX ) }; |
157
|
8
|
|
|
|
|
4
|
my @data; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# handle cases where get_first() has yet to be called. |
160
|
8
|
100
|
|
|
|
12
|
unless (defined $index) { |
161
|
3
|
|
|
|
|
5
|
my ($first, $status) = $self->get_first; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# refresh $max and $index, after get_first updates MAX and INDEX |
164
|
3
|
|
|
|
|
5
|
($max, $index) = @$self{ qw( MAX INDEX ) }; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
# empty lists are handled here. |
167
|
3
|
100
|
66
|
|
|
70
|
if ($status && $status == Template::Constants::STATUS_DONE) { |
168
|
1
|
|
|
|
|
2
|
return (undef, Template::Constants::STATUS_DONE); ## RETURN ## |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
2
|
|
|
|
|
3
|
push @data, $first; |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
## if there's nothing left in the iterator, return the single value. |
174
|
2
|
100
|
|
|
|
4
|
unless ($index < $max) { |
175
|
1
|
|
|
|
|
2
|
return \@data; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# if there's still some data to go... |
180
|
6
|
100
|
|
|
|
7
|
if ($index < $max) { |
181
|
3
|
|
|
|
|
4
|
$index++; |
182
|
3
|
|
|
|
|
5
|
push @data, @{ $self->{ _DATASET } } [ $index..$max ]; |
|
3
|
|
|
|
|
5
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# update counters and flags |
185
|
3
|
|
|
|
|
5
|
@$self{ qw( INDEX COUNT FIRST LAST ) } |
186
|
|
|
|
|
|
|
= ( $max, $max + 1, 0, 1 ); |
187
|
|
|
|
|
|
|
|
188
|
3
|
|
|
|
|
7
|
return \@data; ## RETURN ## |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
else { |
191
|
3
|
|
|
|
|
6
|
return (undef, Template::Constants::STATUS_DONE); ## RETURN ## |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub odd { |
196
|
10
|
100
|
|
10
|
1
|
98
|
shift->{ COUNT } % 2 ? 1 : 0 |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub even { |
200
|
10
|
100
|
|
10
|
1
|
86
|
shift->{ COUNT } % 2 ? 0 : 1 |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub parity { |
204
|
10
|
100
|
|
10
|
1
|
84
|
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
|
301
|
|
|
301
|
|
3534
|
my $self = shift; |
216
|
301
|
|
|
|
|
258
|
my $item = $AUTOLOAD; |
217
|
301
|
|
|
|
|
933
|
$item =~ s/.*:://; |
218
|
301
|
100
|
|
|
|
968
|
return if $item eq 'DESTROY'; |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# alias NUMBER to COUNT for backwards compatibility |
221
|
122
|
100
|
|
|
|
169
|
$item = 'COUNT' if $item =~ /NUMBER/i; |
222
|
|
|
|
|
|
|
|
223
|
122
|
|
|
|
|
274
|
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
|
0
|
|
|
0
|
|
|
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
|
0
|
|
|
|
|
|
" Last: ", $self->{ LAST }, "\n", |
248
|
|
|
|
|
|
|
"\n" |
249
|
|
|
|
|
|
|
); |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
1; |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
__END__ |