lib/Config/Neat/Util.pm

filter: Statementstmt 84.0% 100 / 119 Branchbran 66.0% 37 / 56 Conditioncond 53.1% 17 / 32 MC/DCmcdc 33.3% 7 / 21 Subroutinesub 84.0% 21 / 25 Podpod 0.0% 0 / 23 totaltotal 65.9% 182 / 276 CC 42 SCAR 17.7
CC
42
Cov
74%
to_ixhash_recursive
20.0
to_ixhash
12.0
is_homogenous_simple_array
10.2
CRAP 5.8
« lib/Config/Neat/Array.pm ↑ Summary Build »
1 =head1 NAME
2  
3 Config::Neat::Util - Common utility functions for other Config::Neat modules
4  
5 =head1 COPYRIGHT
6  
7 Copyright (C) 2012-2015 Igor Afanasyev <igor.afanasyev@gmail.com>
8  
9 =head1 SEE ALSO
10  
11 L<https://github.com/iafan/Config-Neat>
12  
13 =cut
14  
15 package Config::Neat::Util;
16  
17 4 our $VERSION = '1.4';
18  
19 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 our @ISA = qw(Exporter);
24  
25 4 our @EXPORT_OK = qw(
26 new_ixhash
27 to_ixhash
28 to_ixhash_recursive
29 is_number
30 is_code
31 is_hash
32 is_ixhash
33 is_any_hash
34 is_array
35 is_neat_array
36 is_any_array
37 is_scalar
38 is_simple_array
39 is_homogenous_simple_array
40 hash_has_only_sequential_keys
41 hash_has_sequential_keys
42 get_next_auto_key
43 offset_keys
44 get_keys_in_order
45 reorder_ixhash_numerically
46 reorder_ixhash
47 rename_ixhash_key
48 read_file
49 );
50  
51 sub new_ixhash {
52 634 ▶ my $new = {};
Subroutine new_ixhash: called CC=1 SCAR=0.0
Pod undocumented
53 634 tie(%$new, 'Tie::IxHash');
54 634 return $new;
55 }
56  
57 sub to_ixhash {
58 0 ▶ my $node = shift;
Subroutine to_ixhash: not called CC=3 SCAR=24.8
Pod undocumented
59 0 ▶ die "Not a regular hash" unless is_hash($node) && !is_ixhash($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_hash($node) and not is_ixhash($node) 0 0 0
Condition0%0/3
is_hash($node) and not is_ixhash($node)
!ll&&!rl&&r
000
MC/DC0%0/2
is_hash($node) and not is_ixhash($node)
is_hash($node) not is_ixhash($node)
Truth table0%0/3
A and not(B)
A= is_hash($node)B= not is_ixhash($node)
ABresult
0X0
100
111
60 0 my $new = new_ixhash;
61 0 map { $new->{$_} = $node->{$_} } keys %$node;
62 0 return $new;
63 }
64  
65 sub to_ixhash_recursive {
66 0 ▶ my ($node) = @_;
Subroutine to_ixhash_recursive: not called CC=4 SCAR=30.0
Pod undocumented
67 0 ▶ my $result = is_hash($node) && !is_ixhash($node) ? to_ixhash($node) : $node;
Branch: 1 branch
BranchTrueFalseTotal
is_hash($node) && !is_ixhash($node) ? : 0 0 0
Condition0%0/3
is_hash($node) and not is_ixhash($node)
!ll&&!rl&&r
000
MC/DC0%0/2
is_hash($node) and not is_ixhash($node)
is_hash($node) not is_ixhash($node)
Truth table0%0/3
A and not(B)
A= is_hash($node)B= not is_ixhash($node)
ABresult
0X0
100
111
68 0 ▶ if (is_ixhash($result)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ixhash($result)) 0 0 0
69 map {
70 0 $result->{$_} = to_ixhash_recursive($result->{$_});
71 } keys %$result;
72 }
73 0 return $result;
74 }
75  
76 sub is_number {
77 522 ▶ my $n = shift;
Subroutine is_number: called CC=2 SCAR=7.1
Pod undocumented
78 522 ▶ return defined $n && $n =~ m/^\d+$/;
Condition66%2/3
defined $n && $n =~ /^\d+$/
!ll&&!rl&&r
043290
MC/DC50%1/2
defined $n && $n =~ /^\d+$/
defined $n $n =~ /^\d+$/
Truth table66%2/3
A && B
A= defined $nB= $n =~ /^\d+$/
ABresult
0X0
100
111
79 }
80  
81 sub is_code {
82 0 ▶ my $node = shift;
Subroutine is_code: not called CC=1 SCAR=6.9
Pod undocumented
83 0 return ref($node) eq 'CODE';
84 }
85  
86 sub is_hash {
87 5129 ▶ my $node = shift;
Subroutine is_hash: called CC=1 SCAR=0.0
Pod undocumented
88 5129 return ref($node) eq 'HASH';
89 }
90  
91 sub is_ixhash {
92 285 ▶ my $node = shift;
Subroutine is_ixhash: called CC=2 SCAR=7.1
Pod undocumented
93 285 ▶ return undef unless is_hash($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_hash($node) 0 285 285
94 285 return ref(tied(%$node)) eq 'Tie::IxHash';
95 }
96  
97 sub is_any_hash {
98 4 ▶ my $node = shift;
Subroutine is_any_hash: called CC=2 SCAR=8.1
Pod undocumented
99 4 ▶ return is_hash($node) || is_ixhash($node);
Condition33%1/3
is_hash($node) || is_ixhash($node)
l!l&&r!l&&!r
400
MC/DC0%0/2
is_hash($node) || is_ixhash($node)
is_hash($node) is_ixhash($node)
Truth table33%1/3
A || B
A= is_hash($node)B= is_ixhash($node)
ABresult
1X1
011
000
100 }
101  
102 sub is_array {
103 1415 ▶ my $node = shift;
Subroutine is_array: called CC=1 SCAR=0.0
Pod undocumented
104 1415 return ref($node) eq 'ARRAY';
105 }
106  
107 sub is_neat_array {
108 2173 ▶ my $node = shift;
Subroutine is_neat_array: called CC=1 SCAR=0.0
Pod undocumented
109 2173 return ref($node) eq 'Config::Neat::Array';
110 }
111  
112 sub is_any_array {
113 647 ▶ my $node = shift;
Subroutine is_any_array: called CC=2 SCAR=7.1
Pod undocumented
114 647 ▶ return is_array($node) || is_neat_array($node);
Condition66%2/3
is_array($node) || is_neat_array($node)
l!l&&r!l&&!r
0176471
MC/DC50%1/2
is_array($node) || is_neat_array($node)
is_array($node) is_neat_array($node)
Truth table66%2/3
A || B
A= is_array($node)B= is_neat_array($node)
ABresult
1X1
011
000
115 }
116  
117 sub is_scalar {
118 1153 ▶ my $node = shift;
Subroutine is_scalar: called CC=1 SCAR=0.0
Pod undocumented
119 1153 return (ref(\$node) eq 'SCALAR') or (ref($node) eq 'SCALAR');
120 }
121  
122 sub is_simple_array {
123 32 ▶ my $node = shift;
Subroutine is_simple_array: called CC=6 SCAR=18.1
Pod undocumented
124  
125 32 ▶ return 1 if is_scalar($node);
Branch: 1 branch
BranchTrueFalseTotal
if is_scalar($node) 0 32 32
126 32 ▶ return undef unless is_array($node) || is_neat_array($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_array($node) or is_neat_array($node) 1 31 32
Condition66%2/3
is_array($node) or is_neat_array($node)
l!l&&r!l&&!r
0311
MC/DC50%1/2
is_array($node) or is_neat_array($node)
is_array($node) is_neat_array($node)
Truth table66%2/3
A or B
A= is_array($node)B= is_neat_array($node)
ABresult
1X1
011
000
127  
128 31 foreach my $value (@$node) {
129 50 ▶ return undef unless is_scalar($value);
Branch: 1 branch
BranchTrueFalseTotal
unless is_scalar($value) 10 40 50
130 }
131 21 return 1;
132 }
133  
134 sub is_homogenous_simple_array {
135 359 ▶ my $node = shift;
Subroutine is_homogenous_simple_array: called CC=10 SCAR=23.2
Pod undocumented
136  
137 359 ▶ return 1 if is_scalar($node);
Branch: 1 branch
BranchTrueFalseTotal
if is_scalar($node) 0 359 359
138 359 ▶ return undef unless is_array($node) || is_neat_array($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_array($node) or is_neat_array($node) 101 258 359
Condition100%3/3
is_array($node) or is_neat_array($node)
l!l&&r!l&&!r
15243101
MC/DC100%2/2
is_array($node) or is_neat_array($node)
is_array($node) is_neat_array($node)
Truth table100%3/3
A or B
A= is_array($node)B= is_neat_array($node)
ABresult
1X1
011
000
139  
140 258 my $contains_hash = undef;
141 258 my $contains_array = undef;
142 258 my $contains_scalar = undef;
143  
144 258 foreach my $value (@$node) {
145 515 ▶ if (is_hash($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_hash($value)) { } 17 498 515
elsif (is_any_array($value)) { } 28 470 498
146 17 $contains_hash |= 1;
147 } elsif (is_any_array($value)) {
148 28 $contains_array |= 1;
149 } else {
150 470 $contains_scalar |= is_scalar($value);
151 }
152 515 ▶ die "Mixing hashes with simple arrays/scalars within one node is not supported" if $contains_hash && $contains_scalar;
Branch: 1 branch
BranchTrueFalseTotal
if $contains_hash and $contains_scalar 0 515 515
Condition66%2/3
$contains_hash and $contains_scalar
!ll&&!rl&&r
496190
MC/DC0%0/2
$contains_hash and $contains_scalar
$contains_hash $contains_scalar
Truth table66%2/3
A and B
A= $contains_hashB= $contains_scalar
ABresult
0X0
100
111
153 }
154 258 ▶ return $contains_scalar && !$contains_array;
Condition66%2/3
$contains_scalar && !$contains_array
!ll&&!rl&&r
190239
MC/DC50%1/2
$contains_scalar && !$contains_array
$contains_scalar !$contains_array
Truth table66%2/3
A && not(B)
A= $contains_scalarB= !$contains_array
ABresult
0X0
100
111
155 }
156  
157 sub hash_has_only_sequential_keys {
158 140 ▶ my $node = shift;
Subroutine hash_has_only_sequential_keys: called CC=1 SCAR=0.0
Pod undocumented
159 140 return hash_has_sequential_keys($node, 1);
160 }
161  
162 sub hash_has_sequential_keys {
163 295 ▶ my ($node, $strict) = @_;
Subroutine hash_has_sequential_keys: called CC=5 SCAR=16.1
Pod undocumented
164 295 ▶ die "Not a hash" unless is_hash($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_hash($node) 0 295 295
165  
166 295 my $i = 0;
167 map {
168 295 ▶ if (is_number($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_number($_)) { } 73 327 400
169 73 ▶ return undef if $_ != $i;
Branch: 1 branch
BranchTrueFalseTotal
if $_ != $i 8 65 73
170 65 $i++;
171 } else {
172 327 ▶ return undef if $strict;
Branch: 1 branch
BranchTrueFalseTotal
if $strict 131 196 327
173 }
174 } keys %$node;
175 156 return 1;
176 }
177  
178 # supposed to be used against hash that matches the
179 # `hash_has_sequential_keys() == true` criterion
180 sub get_next_auto_key {
181 27 ▶ my $node = shift;
Subroutine get_next_auto_key: called CC=4 SCAR=14.0
Pod undocumented
182 27 ▶ die "Not a hash" unless is_hash($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_hash($node) 0 27 27
183  
184 # get max(key)
185 27 my $i = -1; # so that next key will start with 0
186 map {
187 27 ▶ $i = $_ if is_number($_) && $_ > $i;
Branch: 1 branch
BranchTrueFalseTotal
if is_number($_) and $_ > $i 10 25 35
Condition66%2/3
is_number($_) and $_ > $i
!ll&&!rl&&r
25010
MC/DC50%1/2
is_number($_) and $_ > $i
is_number($_) $_ > $i
Truth table66%2/3
A and B
A= is_number($_)B= $_ > $i
ABresult
0X0
100
111
188 } keys %$node;
189  
190 # return max + 1
191 27 return $i + 1;
192 }
193  
194 sub offset_keys {
195 11 ▶ my ($node, $offset) = @_;
Subroutine offset_keys: called CC=4 SCAR=13.9
Pod undocumented
196 11 ▶ die "Not a Tie::IxHash" unless is_ixhash($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ixhash($node) 0 11 11
197 11 ▶ return $node if $offset == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offset == 0 9 2 11
198  
199 2 my $result = new_ixhash;
200  
201 # remap keys
202 map {
203 2 ▶ if (is_number($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_number($_)) { } 3 2 5
204 3 $result->{$_ + $offset} = $node->{$_};
205 } else {
206 2 $result->{$_} = $node->{$_};
207 }
208 } keys %$node;
209  
210 2 return $result;
211 }
212  
213 # accepts an array of hasrefs
214 sub get_keys_in_order {
215 128 ▶ my $result = new_ixhash;
Subroutine get_keys_in_order: called CC=1 SCAR=0.0
Pod undocumented
216  
217 map {
218 128 map {
219 256 $result->{$_} = 1;
220 } keys %$_;
221 } @_;
222  
223 128 return keys %$result;
224 }
225  
226 sub reorder_ixhash_numerically {
227 0 ▶ my ($node) = @_;
Subroutine reorder_ixhash_numerically: not called CC=2 SCAR=17.9
Pod undocumented
228 0 ▶ die "Not a Tie::IxHash" unless is_ixhash($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ixhash($node) 0 0 0
229  
230 # sort keys numerically
231 0 my @a = sort {$a <=> $b} keys %$node;
232  
233 0 return reorder_ixhash($node, \@a);
234 }
235  
236 sub reorder_ixhash {
237 128 ▶ my ($node, $keysref) = @_;
Subroutine reorder_ixhash: called CC=3 SCAR=11.0
Pod undocumented
238 128 ▶ die "Not a Tie::IxHash" unless is_ixhash($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ixhash($node) 0 128 128
239  
240 128 my $result = new_ixhash;
241 128 ▶ map { $result->{$_} = $node->{$_} if exists $node->{$_} } @$keysref;
Branch: 1 branch
BranchTrueFalseTotal
if exists $node->{$_} 151 33 184
242  
243 128 return $result;
244 }
245  
246 sub rename_ixhash_key {
247 18 ▶ my ($node, $from, $to) = @_;
Subroutine rename_ixhash_key: called CC=4 SCAR=14.0
Pod undocumented
248 18 ▶ die "Not a Tie::IxHash" unless is_ixhash($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ixhash($node) 0 18 18
249 18 ▶ die "Can\'t rename key '$from' to '$to', because the target key already exists" if exists $node->{$to};
Branch: 1 branch
BranchTrueFalseTotal
if exists $node->{$to} 0 18 18
250  
251 18 my $result = new_ixhash;
252 map {
253 18 ▶ my $key = $_ eq $from ? $to : $_;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $from ? : 18 7 25
254 25 $result->{$key} = $node->{$_};
255 } keys %$node;
256  
257 18 return $result;
258 }
259  
260 sub read_file {
261 97 ▶ my ($filename, $binmode) = @_;
Subroutine read_file: called CC=3 SCAR=11.2
Pod undocumented
262  
263 97 ▶ open(CFG, $filename) or die "Can't open [$filename]: $!";
Branch: 1 branch
BranchTrueFalseTotal
open CFG, $filename or die "Can't open [$filename]: $!" 0 97 97
264 97 ▶ binmode(CFG, $binmode || ':utf8');
Condition50%1/2
$binmode || ':utf8'
l!l
097
MC/DC0%0/1
$binmode || ':utf8'
$binmode
Truth table50%1/2
A
A= $binmode
Aresult
11
00
265 97 my $text = join('', <CFG>);
266 97 close(CFG);
267  
268 97 return $text;
269 } # end sub
270  
271  
272 4 1;
« lib/Config/Neat/Array.pm ↑ Summary Build »