blib/lib/Hash/Iterator.pm

filter: Statementstmt 84.7% 78 / 92 Branchbran 62.5% 20 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 76.1% 16 / 21 Podpod 0.0% 0 / 9 totaltotal 73.5% 117 / 159 CC 18 SCAR 10.7
CC
18
Cov
79%
done
6.0
get_data
6.0
get_keys
6.0
CRAP 2.9
↑ Summary
1 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Hash::Iterator;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use constant MsgEmptyHash => 'the iterator has no values';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use constant FALSE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.001';
13  
14 sub new {
15 8 ▶ my ( $class ) = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
16  
17 8 ▶ Carp::croak("new() requires key-value pairs")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 8 8
18 unless @_ % 2 == 0;
19  
20 8 my ( @keys, %data, $object );
21  
22 8 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 13 8 21
23 13 my $k = shift;
24 13 push @keys, $k;
25 13 $data{$k} = shift
26 }
27  
28 $object = {
29 8 Keys => [ @keys ],
30 Data => { %data },
31 LengthKeys => $#keys,
32 CurrentState => -1,
33 PreviousState => -1,
34 };
35  
36 8 return bless $object, $class;
37 }
38  
39 sub done {
40 0 ▶ my $self = shift;
Subroutine done: not called CC=2 SCAR=17.9
Pod undocumented
41  
42 0 ▶ return ($self->_get_position == $self->_get_LengthKeys)
Branch: 1 branch
BranchTrueFalseTotal
$self->_get_position == $self->_get_LengthKeys ? : 0 0 0
43 ? TRUE
44 : FALSE;
45 }
46  
47 sub next {
48 15 ▶ my $self = shift;
Subroutine next: called CC=3 SCAR=11.0
Pod undocumented
49  
50 15 ▶ unless (%{$self->{Data}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$self->{"Data"};}) 1 14 15
51 1 Carp::croak(MsgEmptyHash);
52 }
53  
54 14 ▶ if ( $self->{LengthKeys} > $self->{CurrentState} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'LengthKeys'} > $self->{'CurrentState'}) { } 12 2 14
55 12 $self->{PreviousState} = $self->{CurrentState};
56 12 $self->{CurrentState}++;
57 } else {
58 2 return 0;
59 }
60 12 return 1;
61 }
62  
63 sub previous {
64 2 ▶ my $self = shift;
Subroutine previous: called CC=3 SCAR=11.0
Pod undocumented
65  
66 2 ▶ if ((int $self->{CurrentState} != -1) and ( int $self->{PreviousState} != -1 )) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $self->{"CurrentState"} != -1 and int $self->{"PreviousState"} != -1) 1 1 2
Condition66%2/3
int $self->{"CurrentState"} != -1 and int $self->{"PreviousState"} != -1
!ll&&!rl&&r
101
MC/DC50%1/2
int $self->{"CurrentState"} != -1 and int $self->{"PreviousState"} != -1
int $self->{"CurrentState"} != -1 int $self->{"PreviousState"} != -1
Truth table66%2/3
A and B
A= int $self->{"CurrentState"} != -1B= int $self->{"PreviousState"} != -1
ABresult
0X0
100
111
67 1 $self->{CurrentState} = $self->{PreviousState};
68 1 $self->{PreviousState}--;
69  
70 1 return 1;
71 }
72 1 return undef;
73 }
74  
75 sub peek_key {
76 9 ▶ my $self = shift;
Subroutine peek_key: called CC=2 SCAR=7.6
Pod undocumented
77 9 my $key = $self->_get_key;
78  
79 9 ▶ return $key if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 9 0 9
80 0 return undef;
81 }
82  
83 sub peek_value {
84 8 ▶ my $self = shift;
Subroutine peek_value: called CC=3 SCAR=11.0
Pod undocumented
85 8 my $value = $self->_get_value;
86  
87 8 ▶ if ( ref $value eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 2 6 8
elsif (ref $value eq 'ARRAY') { } 2 4 6
88 2 return \%$value;
89 }
90 elsif ( ref $value eq 'ARRAY' ) {
91 2 return \@$value
92 }
93 else {
94 4 return $value;
95 }
96 0 return undef;
97 }
98  
99 sub is_ref {
100 11 ▶ my ( $self, $ref ) = @_;
Subroutine is_ref: called CC=2 SCAR=6.9
Pod undocumented
101 11 my $value = $self->_get_value;
102  
103 11 ▶ if ( ref $value eq $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq $ref) 4 7 11
104 4 return 1;
105 }
106 7 return;
107 }
108  
109 sub _get_value {
110 19 ▶ my $self = shift;
Subroutine _get_value: called CC=3 SCAR=11.3
111  
112 19 my ( $CurrentValue, $CurrentKey );
113 19 eval {
114 19 $CurrentKey = ${$self->{Keys}}[ $self->_get_position ];
115 19 $CurrentValue = ${ $self->{Data} }{$CurrentKey};
116 };
117 19 ▶ Carp::croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 19 19
118  
119 19 ▶ return $CurrentValue if $CurrentValue;
Branch: 1 branch
BranchTrueFalseTotal
if $CurrentValue 19 0 19
120 0 return undef;
121 }
122  
123 sub _get_key {
124 9 ▶ my $self = shift;
Subroutine _get_key: called CC=3 SCAR=11.4
125  
126 9 my $curKey;
127 9 eval {
128 9 $curKey = ${$self->{Keys}}[ $self->_get_position ];
129 };
130 9 ▶ Carp::croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9 9
131  
132 9 ▶ return $curKey if $curKey;
Branch: 1 branch
BranchTrueFalseTotal
if $curKey 9 0 9
133 0 return undef;
134 }
135  
136 sub get_data {
137 0 ▶ my $self = shift;
Subroutine get_data: not called CC=2 SCAR=17.9
Pod undocumented
138 return wantarray
139 0 ▶ ? @{ $self->{Data} }
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
140 : $self->_get_LengthKeys;
141 }
142  
143 sub get_keys {
144 0 ▶ my $self = shift;
Subroutine get_keys: not called CC=2 SCAR=17.9
Pod undocumented
145 return wantarray
146 0 ▶ ? @{ $self->{Keys} }
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
147 : $self->_get_LengthKeys;
148 }
149  
150 28 ▶sub _get_position { shift->{CurrentState} }
Subroutine _get_position: called CC=1 SCAR=0.0
151 0 ▶sub _get_PreviousState { shift->{PreviousState} }
Subroutine _get_PreviousState: not called CC=1 SCAR=6.9
152 0 ▶sub _get_LengthKeys { shift->{LengthKeys} }
Subroutine _get_LengthKeys: not called CC=1 SCAR=6.9
153  
154 1 1;
155  
156 __END__
↑ Summary