line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Alloy::Iterator; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Template::Alloy::Iterator - Handle foreach iterations |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
|
9
|
10
|
|
|
10
|
|
70
|
use strict; |
|
10
|
|
|
|
|
22
|
|
|
10
|
|
|
|
|
320
|
|
10
|
10
|
|
|
10
|
|
53
|
use warnings; |
|
10
|
|
|
|
|
19
|
|
|
10
|
|
|
|
|
6112
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
267
|
|
|
267
|
0
|
617
|
my ($class, $items) = @_; |
14
|
267
|
50
|
|
|
|
654
|
$items = [] if ! defined $items; |
15
|
267
|
50
|
|
|
|
1319
|
if (ref($items) eq 'HASH') { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
16
|
0
|
|
|
|
|
0
|
$items = [ map { {key => $_, value => $items->{ $_ }} } sort keys %$items ]; |
|
0
|
|
|
|
|
0
|
|
17
|
|
|
|
|
|
|
} elsif (UNIVERSAL::can($items, 'as_list')) { |
18
|
0
|
|
|
|
|
0
|
$items = $items->as_list; |
19
|
|
|
|
|
|
|
} elsif (ref($items) ne 'ARRAY') { |
20
|
38
|
|
|
|
|
109
|
$items = [$items]; |
21
|
|
|
|
|
|
|
} |
22
|
267
|
|
|
|
|
2956
|
return bless [$items, 0], $class; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub get_first { |
26
|
267
|
|
|
267
|
0
|
471
|
my $self = shift; |
27
|
267
|
100
|
|
|
|
454
|
return (undef, 3) if ! @{ $self->[0] }; |
|
267
|
|
|
|
|
776
|
|
28
|
265
|
|
|
|
|
2664
|
return ($self->[0]->[$self->[1] = 0], undef); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub get_next { |
32
|
683
|
|
|
683
|
0
|
1125
|
my $self = shift; |
33
|
683
|
100
|
|
|
|
1158
|
return (undef, 3) if ++ $self->[1] > $#{ $self->[0] }; |
|
683
|
|
|
|
|
4287
|
|
34
|
443
|
|
|
|
|
952
|
return ($self->items->[$self->[1]], undef); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
443
|
|
|
443
|
0
|
4871
|
sub items { shift->[0] } |
38
|
|
|
|
|
|
|
|
39
|
160
|
|
|
160
|
0
|
717
|
sub index { shift->[1] } |
40
|
|
|
|
|
|
|
|
41
|
57
|
|
|
57
|
0
|
88
|
sub max { $#{ shift->[0] } } |
|
57
|
|
|
|
|
277
|
|
42
|
|
|
|
|
|
|
|
43
|
36
|
|
|
36
|
0
|
99
|
sub size { shift->max + 1 } |
44
|
|
|
|
|
|
|
|
45
|
60
|
|
|
60
|
0
|
128
|
sub count { shift->index + 1 } |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
0
|
0
|
0
|
sub number { shift->index + 1 } |
48
|
|
|
|
|
|
|
|
49
|
67
|
100
|
|
67
|
0
|
172
|
sub first { (shift->index == 0) || 0 } |
50
|
|
|
|
|
|
|
|
51
|
21
|
|
100
|
21
|
0
|
41
|
sub last { my $self = shift; return ($self->index == $self->max) || 0 } |
|
21
|
|
|
|
|
54
|
|
52
|
|
|
|
|
|
|
|
53
|
15
|
100
|
|
15
|
0
|
33
|
sub odd { shift->count % 2 ? 1 : 0 } |
54
|
|
|
|
|
|
|
|
55
|
15
|
100
|
|
15
|
0
|
38
|
sub even { shift->count % 2 ? 0 : 1 } |
56
|
|
|
|
|
|
|
|
57
|
15
|
100
|
|
15
|
0
|
31
|
sub parity { shift->count % 2 ? 'odd' : 'even' } |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub prev { |
60
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
61
|
0
|
0
|
|
|
|
|
return undef if $self->index <= 0; |
62
|
0
|
|
|
|
|
|
return $self->items->[$self->index - 1]; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub next { |
66
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
67
|
0
|
0
|
|
|
|
|
return undef if $self->index >= $self->max; |
68
|
0
|
|
|
|
|
|
return $self->items->[$self->index + 1]; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
1; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
__END__ |