Hierarchical.pm

filter: Statementstmt 100% 306 / 306 Branchbran 99.4% 193 / 194 Conditioncond 98.2% 55 / 56 MC/DCmcdc 96.8% 31 / 32 Subroutinesub 96.5% 28 / 29 Podpod 40.0% 6 / 15 totaltotal 97.9% 619 / 632 CC 134 SCAR 26.4
CC
134
Cov
100%
syntax_check
29.0
merge_values
19.0
convert_value
15.0
CRAP 14.1
↑ Summary Hierarchical/Dump.pm »
1 #
2 # Config::General::Hierarchical.pm - Hierarchical Generic Config Module
3 #
4 # Purpose: Permits to organize configuration values
5 # in a hierarchical structure of files
6 #
7 # Copyright (c) 2007-2009 Daniele Ricci <icc |AT| cpan.org>.
8 # All Rights Reserved. Std. disclaimer applies.
9 # Artificial License, same as perl itself.
10  
11 package Config::General::Hierarchical;
12  
13 5 $Config::General::Hierarchical::VERSION = 0.07;
14  
15 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Clone::PP qw( clone );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Config::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Config::General::Hierarchical::ExcludeWeaken;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Cwd qw( abs_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 my @properties = qw( constraint name opt value );
28 5 my %properties = map( ( $_ => 1 ), @properties );
29  
30 5 __PACKAGE__->mk_accessors( @properties, qw( cache ) );
31  
32 5 my %Config_General_Proxy = (
33 '-AutoLaunder' => 1,
34 '-CComments' => 1,
35 '-LowerCaseNames' => 1,
36 '-SplitDelimiter' => 1,
37 '-SplitPolicy' => 1,
38 );
39  
40 90 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
41 90 my ( $ref, %args ) = @_;
42  
43 90 my $file;
44 my %general;
45 90 my %props;
46 90 my %options;
47  
48 90 foreach my $key ( keys %args ) {
49 234 ▶ if ( $key eq 'file' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'file') { } 42 192 234
elsif ($properties{$key}) { } 168 24 192
elsif ($Config::General::Hierarchical::Options::options{$key}) { } 19 5 24
elsif ($Config_General_Proxy{$key}) { } 2 3 5
50 42 $file = $args{$key};
51 }
52 elsif ( $properties{$key} ) {
53 168 $props{$key} = $args{$key};
54 }
55 elsif ( $Config::General::Hierarchical::Options::options{$key} ) {
56 19 $options{$key} = $args{$key};
57 }
58 elsif ( $Config_General_Proxy{$key} ) {
59 2 $general{$key} = $args{$key};
60 }
61 }
62  
63 90 ▶ my $class = ref $ref || $ref or croak __PACKAGE__ . ": wrong new call";
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $ref || $ref or croak("Config::General::Hierarchical: wrong new call") 89 1 90
Condition100%3/3
ref $ref || $ref
l!l&&r!l&&!r
41481
MC/DC100%2/2
ref $ref || $ref
ref $ref $ref
Truth table100%3/3
A || B
A= ref $refB= $ref
ABresult
1X1
011
000
64 89 my $self = $class->SUPER::new(
65 {
66 cache => {},
67 %props
68 }
69 );
70  
71 89 ▶ unless (%props) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%props) 45 44 89
72 45 $self->opt(
73 Config::General::Hierarchical::Options->new(
74 {
75 files => [],
76 general => \%general,
77 inherits => 'inherits',
78 root => $self,
79 struct => { '0' => {} },
80 undefined => 'undefined',
81 wild => '*',
82 %options
83 }
84 )
85 );
86  
87 weaken( $self->opt->{root} )
88 45 ▶ unless $Config::General::Hierarchical::ExcludeWeaken::exclude;
Branch: 1 branch
BranchTrueFalseTotal
unless $Config::General::Hierarchical::ExcludeWeaken::exclude 45 0 45
89  
90 45 ▶ $self->read($file) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 42 3 45
91  
92 36 ▶ $self->check if $args{check};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"check"} 2 34 36
93 }
94  
95 79 return $self;
96 }
97  
98 3 ▶sub check {
Subroutine check: called CC=3 SCAR=11.0
Pod documented
99 3 my ($self) = @_;
100  
101 3 foreach my $key ( keys %{ $self->value } ) {
102 7 my $v = $self->get($key);
103  
104 6 ▶ $v->check if eval { $v->isa(__PACKAGE__); };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $v->isa(__PACKAGE__) } } 1 5 6
105 }
106  
107 2 return $self;
108 }
109  
110 12 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
111 12 my ( $class, @pars ) = @_;
112 12 my $syntax = $class->syntax;
113  
114 12 ▶ die "$class: syntax method musts return an HASH reference\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $syntax ne "HASH" 1 11 12
115 if ref $syntax ne 'HASH';
116  
117 11 $class->check_syntax( $syntax, [] );
118 }
119  
120 47 ▶sub check_syntax {
Subroutine check_syntax: called CC=10 SCAR=23.0
Pod undocumented
121 47 my ( $class, $syntax, $parents ) = @_;
122  
123 47 foreach my $key ( keys %$syntax ) {
124 129 my $ref = ref $syntax->{$key};
125 129 my $syn = $syntax->{$key};
126  
127 129 push @$parents, $key;
128  
129 129 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 37 92 129
130 37 ▶ die "$class: wrong use of $ref reference as syntax for variable '"
Branch: 1 branch
BranchTrueFalseTotal
if $ref ne "HASH" 1 36 37
131 . join( '->', @$parents ) . "'\n"
132 if $ref ne 'HASH';
133  
134 36 $class->check_syntax( $syn, $parents );
135 }
136 else {
137 92 ▶ die "$class: wrong '$syn' syntax for variable '"
Branch: 1 branch
BranchTrueFalseTotal
if defined $syn and not $syn =~ /^[amuABDEINST]*$/ 1 91 92
Condition100%3/3
defined $syn and not $syn =~ /^[amuABDEINST]*$/
!ll&&!rl&&r
14771
MC/DC100%2/2
defined $syn and not $syn =~ /^[amuABDEINST]*$/
defined $syn not $syn =~ /^[amuABDEINST]*$/
Truth table100%3/3
A and not(B)
A= defined $synB= not $syn =~ /^[amuABDEINST]*$/
ABresult
0X0
100
111
138 . join( '->', @$parents ) . "'\n"
139 if defined $syn && $syn !~ /^[amuABDEINST]*$/;
140  
141 91 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
if defined $syn and $syn =~ /m/ and syntax_check_get_type($syn) ne "S" and not $syn =~ /a/ 1 90 91
Condition100%9/9
defined $syn and $syn =~ /m/ and syntax_check_get_type($syn) ne "S" and not $syn =~ /a/
!ll&&!rl&&r
8731
defined $syn and $syn =~ /m/ and syntax_check_get_type($syn) ne "S"
!ll&&!rl&&r
8254
defined $syn and $syn =~ /m/
!ll&&!rl&&r
14689
MC/DC100%4/4
defined $syn and $syn =~ /m/ and syntax_check_get_type($syn) ne "S" and not $syn =~ /a/
defined $syn $syn =~ /m/ syntax_check_get_type($syn) ne "S" not $syn =~ /a/
Truth table100%5/5
A and B and C and not(D)
A= defined $synB= $syn =~ /m/C= syntax_check_get_type($syn) ne "S"D= not $syn =~ /a/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
142 "$class: wrong use of 'm' flag for not string nor array variable '"
143 . join( '->', @$parents ) . "'\n"
144 if defined $syn
145 && $syn =~ /m/
146 && syntax_check_get_type($syn) ne 'S'
147 && $syn !~ /a/;
148 }
149  
150 125 pop @$parents;
151 }
152 }
153  
154 255 ▶sub get {
Subroutine get: called CC=12 SCAR=24.8
Pod documented
155 255 my ( $self, $name, @names ) = @_;
156  
157 255 ▶ my $vname = $self->name ? $self->name . '->' . $name : $name;
Branch: 1 branch
BranchTrueFalseTotal
$self->name ? : 105 150 255
158  
159 255 ▶ if ( exists $self->cache->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->cache->{$name}) 53 202 255
160 53 my $value = $self->cache->{$name};
161  
162 53 ▶ return $value unless scalar @names;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @names 43 10 53
163 10 ▶ return $value->get(@names) if eval { $value->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $value->isa(__PACKAGE__) } } 6 4 10
164  
165 4 croak __PACKAGE__
166 . ": can't get subkey '$names[0]' value for not node variable '$vname'";
167 }
168  
169 my $syntax =
170 exists $self->constraint->{$name}
171 ? $self->constraint->{$name}
172 202 ▶ : $self->constraint->{ $self->wild };
Branch: 1 branch
BranchTrueFalseTotal
exists $self->constraint->{$name} ? : 97 105 202
173 202 ▶ my $value = $self->value->{$name} || $self->value->{ $self->wild };
Condition100%3/3
$self->value->{$name} || $self->value->{$self->wild}
l!l&&r!l&&!r
184612
MC/DC100%2/2
$self->value->{$name} || $self->value->{$self->wild}
$self->value->{$name} $self->value->{$self->wild}
Truth table100%3/3
A || B
A= $self->value->{$name}B= $self->value->{$self->wild}
ABresult
1X1
011
000
174  
175 202 ▶ if ( !defined $value || ref $value->value ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value->value ne "HASH") 161 41 202
Condition100%3/3
not defined $value or ref $value->value ne "HASH"
l!l&&r!l&&!r
1214941
MC/DC100%2/2
not defined $value or ref $value->value ne "HASH"
not defined $value ref $value->value ne "HASH"
Truth table100%3/3
not(A) or B
A= not defined $valueB= ref $value->value ne "HASH"
ABresult
1X1
011
000
176 161 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if scalar @names 1 160 161
177 . ": can't get subkey '$names[0]' value for not node variable '$vname'"
178 if scalar @names;
179  
180 160 return $self->cache->{$name} =
181 $self->syntax_check( $vname, $value, $syntax );
182 }
183  
184 41 ▶ $self->cache->{$name} = $value = $self->syntax_check(
Condition100%2/2
$syntax || {}
l!l
2120
MC/DC100%1/1
$syntax || {}
$syntax
Truth table100%2/2
A
A= $syntax
Aresult
11
00
185 $vname,
186 $self->new(
187 constraint => $syntax || {},
188 name => $vname,
189 opt => $self->opt,
190 value => $value->value
191 ),
192 $syntax
193 );
194  
195 37 ▶ return $value unless scalar @names;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @names 31 6 37
196  
197 6 return $value->get(@names);
198 }
199  
200 5 our $AUTOLOAD;
201  
202 741 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
203 741 my ( $self, @args ) = @_;
204 741 my $name = $AUTOLOAD;
205  
206 741 $name =~ s/\w+:://g;
207  
208 741 ▶ return $self->get( $1, @args ) if $name =~ /^_(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^_(\w+)$/ 132 609 741
209  
210 return $self->opt->$name
211 609 ▶ if $Config::General::Hierarchical::Options::options{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $Config::General::Hierarchical::Options::options{$name} 608 1 609
212  
213 1 my $ref = ref $self;
214  
215 1 croak "Can't locate object method \"$name\" via package \"$ref\"";
216 }
217  
218 # simply avoid AUTOLOAD is called when an object is destroied
219 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
220  
221 2 ▶sub getk {
Subroutine getk: called CC=2 SCAR=6.9
Pod documented
222 2 my ($self) = @_;
223  
224 2 ▶ croak __PACKAGE__ . ": can't get keys before reading any file"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->value 1 1 2
225 unless $self->value;
226  
227 1 return keys %{ $self->value };
228 }
229  
230 42 ▶sub read {
Subroutine read: called CC=1 SCAR=0.0
Pod documented
231 42 my ( $self, $name ) = @_;
232  
233 42 $self->constraint( $self->syntax );
234 42 $self->value( $self->read_( $name, [] ) );
235 33 $self->expand_wild_keys( $self->value );
236 33 $self->name('');
237  
238 33 return $self;
239 }
240  
241 65 ▶sub read_ {
Subroutine read_: called CC=14 SCAR=26.4
Pod undocumented
242 65 my ( $self, $name, $children ) = @_;
243  
244 65 my $tmp = eval { abs_path($name); };
245 65 ▶ my $error = $@ || !$tmp;
Condition66%2/3
$@ || !$tmp
l!l&&r!l&&!r
0164
MC/DC50%1/2
$@ || !$tmp
$@ !$tmp
Truth table66%2/3
A || not(B)
A= $@B= !$tmp
ABresult
1X1
011
000
246 65 ▶ $name = $tmp if $tmp;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 64 1 65
247 65 my $files = $self->opt->files;
248 65 my $in_file =
249 $name
250 . join( '', reverse( map( "\ninherited by: $files->[$_]", @$children ) ) )
251 . "\n ";
252 65 my $nfile = scalar @$files;
253  
254 65 ▶ croak __PACKAGE__ . ": no such directory: $in_file" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 64 65
255 64 ▶ croak __PACKAGE__ . ": no such file: $in_file" unless -e $name;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $name 1 63 64
256 63 ▶ croak __PACKAGE__ . ": recursive hierarchy\nin file: $in_file"
Branch: 1 branch
BranchTrueFalseTotal
if grep /^$name$/, map($files->[$_], @$children) 1 62 63
257 if grep /^$name$/, map $files->[$_], @$children;
258 62 push @$files, $name;
259  
260 62 my $cfg;
261 62 eval {
262 $cfg = Config::General->new(
263 '-AllowMultiOptions' => 1,
264 '-AutoTrue' => 0,
265 '-CComments' => 0,
266 '-ConfigFile' => $name,
267 '-ExtendedAccess' => 0,
268 '-InterPolateEnv' => 0,
269 '-InterPolateVars' => 0,
270 '-MergeDuplicateBlocks' => 1,
271 '-MergeDuplicateOptions' => 0,
272 '-SlashIsDirectory' => 0,
273 '-UseApacheInclude' => 0,
274 62 ( %{ $self->opt->general } )
275 );
276 };
277  
278 62 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 59 62
279 3 my @list = split /\n/, $@;
280 3 pop @list;
281 3 my $msg = join "\n", @list;
282  
283 3 croak __PACKAGE__ . ": $msg\nin file: $in_file";
284 }
285  
286 59 ▶ if ( scalar @$children ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$children) 21 38 59
287 21 my $str = $self->opt->struct;
288  
289 21 foreach (@$children) {
290 23 $str = $str->{$_};
291 }
292  
293 21 $str->{$nfile} = {};
294 }
295  
296 59 my $hash = { $cfg->getall };
297 59 my $syntax = $self->inherits;
298 59 my $inherits = $hash->{$syntax};
299  
300 59 delete $hash->{$syntax};
301 59 my ( $undefined, $value ) = $self->convert_hash( $hash, $nfile, $in_file );
302  
303 57 ▶ if ($inherits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inherits) 19 38 57
304 19 my $parents;
305  
306 19 ▶ if ( !ref $inherits ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $inherits) { } 13 6 19
elsif (ref $inherits eq 'ARRAY') { } 5 1 6
307 13 $parents = [$inherits];
308 }
309 elsif ( ref $inherits eq 'ARRAY' ) {
310 5 $parents = $inherits;
311 }
312 else {
313 1 croak __PACKAGE__
314 . ": wrong use of inherits ('$syntax') directive\nin file: $in_file";
315 }
316  
317 18 push @$children, @$files - 1;
318 18 foreach my $parent ( reverse @$parents ) {
319 23 ▶ if ( $parent =~ /^\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent =~ m[^/]) { } 1 22 23
320 1 $name = $parent;
321 }
322 else {
323 22 my @list = split /\//, $name;
324 22 pop @list;
325 22 $name = join( '/', @list ) . '/' . $parent;
326 }
327  
328 23 $hash = $self->read_( $name, $children );
329  
330 20 $self->merge_values( $value, $hash, $self->constraint );
331 }
332 15 pop @$children;
333 }
334  
335 53 $self->undefine( $value, $undefined, $nfile );
336  
337 53 return $value;
338 }
339  
340 129 ▶sub convert_hash {
Subroutine convert_hash: called CC=9 SCAR=22.0
Pod undocumented
341 129 my ( $self, $hash, $file, $in_file ) = @_;
342  
343 129 my $syntax = $self->inherits;
344 129 my %undefined;
345  
346 croak __PACKAGE__
347 . ": inherits ('$syntax') directive cannot be used as node name\nin file: $in_file"
348 129 ▶ if exists $hash->{$syntax};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{$syntax} 1 128 129
349  
350 128 $syntax = $self->undefined;
351 128 my $undefined = $hash->{$syntax};
352 128 delete $hash->{$syntax};
353  
354 128 foreach my $key ( keys %$hash ) {
355 262 ▶ if ( ref $hash->{$key} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash->{$key} eq "HASH") 70 192 262
356 ( $undefined{$key}, my $tmp ) =
357 70 $self->convert_hash( $hash->{$key}, $file, $in_file );
358 }
359 261 ▶ $hash->{$key} = '' unless defined $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash->{$key} 18 243 261
360 $hash->{$key} = Config::General::Hierarchical::Value->new(
361 261 { value => $hash->{$key}, file => $file } );
362 }
363  
364 127 ▶ if ($undefined) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undefined) 13 114 127
365 13 ▶ if ( !ref $undefined ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $undefined) { } 6 7 13
elsif (ref $undefined eq 'ARRAY') { } 6 1 7
366 6 $undefined{$undefined} = undef;
367 }
368 elsif ( ref $undefined eq 'ARRAY' ) {
369 6 $undefined{$_} = undef foreach @$undefined;
370 }
371 else {
372 1 croak __PACKAGE__
373 . ": wrong use of undefined ('$syntax') directive\nin file: $in_file";
374 }
375 }
376  
377 126 return ( \%undefined, $hash );
378 }
379  
380 74 ▶sub merge_values {
Subroutine merge_values: called CC=19 SCAR=29.4
Pod undocumented
381 74 my ( $self, $value, $hash, $constraint ) = @_;
382 74 my ( $key, $val );
383  
384 74 foreach $key ( keys %$value ) {
385 360 $val = $value->{$key};
386  
387 360 ▶ if ( ref $val eq 'Config::General::Hierarchical::Value' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'Config::General::Hierarchical::Value') { } 132 228 360
elsif (ref $val eq 'HASH') { } 80 148 228
388 132 ▶ my $other = exists $hash->{$key} ? $hash->{$key}->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{$key} ? : 44 88 132
389 my $syn = (
390 exists $constraint->{$key}
391 ? $constraint->{$key}
392 132 ▶ : $constraint->{ $self->wild }
Branch: 1 branch
BranchTrueFalseTotal
exists $constraint->{$key} ? : 68 64 132
Condition100%2/2
(exists $constraint->{$key} ? $constraint->{$key} : $$constraint{$self->wild}) || {}
l!l
7656
MC/DC100%1/1
(exists $constraint->{$key} ? $constraint->{$key} : $$constraint{$self->wild}) || {}
(exists $constraint->{$key} ? $constraint->{$key} : $$constraint{$self->wild})
Truth table100%2/2
A
A= (exists $constraint->{$key} ? $constraint->{$key} : $$constraint{$self->wild})
Aresult
11
00
393 )
394 || {};
395  
396 132 ▶ if ( ref $val->value eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val->value eq 'HASH') { } 48 84 132
elsif (not ref $syn and $syn =~ /m/ and defined $other) { } 13 137 150
397 48 ▶ if ( ref $other eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other eq "HASH") 20 28 48
398 20 $self->merge_values( $val->value, $other, $syn );
399 }
400 }
401 ▶ elsif ( !ref $syn && $syn =~ /m/ && defined $other ) {
Condition100%6/6
not ref $syn and $syn =~ /m/ and defined $other
!ll&&!rl&&r
66513
not ref $syn and $syn =~ /m/
!ll&&!rl&&r
412518
MC/DC100%3/3
not ref $syn and $syn =~ /m/ and defined $other
not ref $syn $syn =~ /m/ defined $other
Truth table100%4/4
not(A) and B and C
A= not ref $synB= $syn =~ /m/C= defined $other
ABCresult
0XX0
10X0
1100
1111
402 13 ▶ if ( $syn =~ /a/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($syn =~ /a/) { } 3 10 13
403 3 ▶ $other = [$other] unless ref $other;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other 2 1 3
404  
405 3 ▶ if ( ref $val->value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val->value) { } 1 2 3
406 1 unshift @{ $val->value }, @$other;
407 }
408 else {
409 2 $val->value( [ @$other, $val->value ] );
410 }
411 }
412 else {
413 10 $val->value( $other . $val->value );
414 }
415 }
416 }
417 elsif ( ref $val eq 'HASH' ) {
418 80 ▶ if ( exists $hash->{$key} && ref $hash->{$key} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$key} and ref $hash->{$key} eq "HASH") 8 72 80
Condition100%3/3
exists $hash->{$key} and ref $hash->{$key} eq "HASH"
!ll&&!rl&&r
6848
MC/DC100%2/2
exists $hash->{$key} and ref $hash->{$key} eq "HASH"
exists $hash->{$key} ref $hash->{$key} eq "HASH"
Truth table100%3/3
A and B
A= exists $hash->{$key}B= ref $hash->{$key} eq "HASH"
ABresult
0X0
100
111
419 8 $self->merge_values( $val, $hash->{$key} );
420 }
421 }
422 }
423  
424 74 foreach $key ( keys %$hash ) {
425 269 ▶ $value->{$key} = $hash->{$key} unless exists $value->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $value->{$key} 197 72 269
426 }
427  
428 74 return $value;
429 }
430  
431 80 ▶sub expand_wild_keys {
Subroutine expand_wild_keys: called CC=11 SCAR=24.0
Pod undocumented
432 80 my ( $self, $value ) = @_;
433  
434 80 my $wild = $self->wild;
435 80 my $wv = $value->{$wild};
436  
437 80 ▶ if ( $wv && ref $wv->value eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wv and ref $wv->value eq "HASH") 2 78 80
Condition100%3/3
$wv and ref $wv->value eq "HASH"
!ll&&!rl&&r
7712
MC/DC100%2/2
$wv and ref $wv->value eq "HASH"
$wv ref $wv->value eq "HASH"
Truth table100%3/3
A and B
A= $wvB= ref $wv->value eq "HASH"
ABresult
0X0
100
111
438 2 $wv = $wv->value;
439  
440 2 foreach my $key ( keys %$value ) {
441 5 my $v = $value->{$key}->value;
442  
443 5 ▶ next if $key eq $wild;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $wild 2 3 5
444 3 ▶ next if ref $v ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v ne "HASH" 1 2 3
445  
446 2 foreach my $wk ( keys %$wv ) {
447 4 ▶ $v->{$wk} = clone( $wv->{$wk} ) unless exists $v->{$wk};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $v->{$wk} 3 1 4
448 }
449 }
450 }
451  
452 80 foreach my $key ( keys %$value ) {
453 216 my $v = $value->{$key}->value;
454  
455 216 ▶ next if $key eq $wild;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $wild 3 213 216
456 213 ▶ next if ref $v ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v ne "HASH" 166 47 213
457  
458 47 $self->expand_wild_keys($v);
459 }
460 }
461  
462 122 ▶sub undefine {
Subroutine undefine: called CC=4 SCAR=13.9
Pod undocumented
463 122 my ( $self, $value, $undefined, $file ) = @_;
464  
465 122 foreach my $key ( keys %$value ) {
466 316 ▶ next unless exists $undefined->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $undefined->{$key} 229 87 316
467  
468 87 ▶ if ( defined $undefined->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $undefined->{$key}) { } 69 18 87
469 69 $self->undefine( $value->{$key}->value, $undefined->{$key}, $file );
470 }
471 else {
472 18 $value->{$key} = Config::General::Hierarchical::Value->new(
473 { value => undef, file => $file } );
474 }
475 }
476 }
477  
478 53 ▶sub syntax {
Subroutine syntax: called CC=1 SCAR=0.0
Pod documented
479 53 return {};
480 }
481  
482 5 my %types = (
483 A => { d => 'datetime', e => '^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$' },
484 B => { d => 'boolean value', e => '^(0|1|on|off|yes|no|true|false)$' },
485 D => { d => 'date', e => '^\d\d\d\d-\d\d-\d\d$' },
486 E => { d => 'e-mail address', e => '^[\w\d\-_\.]+@[\w\d\-_\.]+\.\w+$' },
487 I => { d => 'integer value', e => '^-?\d+$' },
488 N => { d => 'number', e => '^-?\d*\.?\d+$' },
489 S => { d => 'string' },
490 T => { d => 'time', e => '^\d\d:\d\d:\d\d$' },
491 );
492  
493 201 ▶sub syntax_check {
Subroutine syntax_check: called CC=29 SCAR=33.7
Pod undocumented
494 201 my ( $self, $vname, $value, $syntax ) = @_;
495 201 my $can_be_undefined;
496 201 my $file = 0;
497  
498 201 ▶ if ( defined $value
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and ref $value eq "Config::General::Hierarchical::Value") 148 53 201
Condition100%3/3
defined $value and ref $value eq "Config::General::Hierarchical::Value"
!ll&&!rl&&r
1241148
MC/DC100%2/2
defined $value and ref $value eq "Config::General::Hierarchical::Value"
defined $value ref $value eq "Config::General::Hierarchical::Value"
Truth table100%3/3
A and B
A= defined $valueB= ref $value eq "Config::General::Hierarchical::Value"
ABresult
0X0
100
111
499 && ref $value eq 'Config::General::Hierarchical::Value' )
500 {
501 148 $file = $value->file;
502 148 $value = $value->value;
503 }
504 201 $file = "\nin file: " . $self->opt->files->[$file];
505  
506 201 ▶ if ( defined $syntax ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $syntax) { } 102 99 201
elsif (defined $value) { } 94 5 99
507 $can_be_undefined =
508 102 ▶ ref $syntax ? exists $syntax->{ $self->undefined } : $syntax =~ /u/;
Branch: 1 branch
BranchTrueFalseTotal
ref $syntax ? : 22 80 102
509 }
510 elsif ( defined $value ) {
511 94 ▶ return ref $value ? $value : $self->convert_value( $value, $vname )
Branch: 2 branches
BranchTrueFalseTotal
if ref $value ne "ARRAY" 92 2 94
ref $value ? : 20 72 92
512 if ref $value ne 'ARRAY';
513  
514 2 croak __PACKAGE__
515 . ": variable '$vname' is an array: should be a string or a node$file";
516 }
517  
518 107 ▶ unless ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 25 82 107
519 25 ▶ if ($can_be_undefined) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_be_undefined) 12 13 25
520 12 ▶ return undef if ref $syntax;
Branch: 1 branch
BranchTrueFalseTotal
if ref $syntax 2 10 12
521 10 ▶ return [] if $syntax =~ /a/;
Branch: 1 branch
BranchTrueFalseTotal
if $syntax =~ /a/ 1 9 10
522 9 ▶ return 0 if $syntax =~ /B/;
Branch: 1 branch
BranchTrueFalseTotal
if $syntax =~ /B/ 1 8 9
523 8 return undef;
524 }
525  
526 13 croak __PACKAGE__ . ": request for undefined variable '$vname'$file";
527 }
528  
529 82 ▶ if ( ref $syntax ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $syntax) 19 63 82
530 19 ▶ return $value if eval { $value->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $value->isa(__PACKAGE__) } } 17 2 19
531  
532 2 croak __PACKAGE__ . ": variable '$vname' should be a node$file";
533 }
534  
535 63 my $type = syntax_check_get_type($syntax);
536  
537 63 ▶ if ( $syntax !~ /a/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($syntax =~ /a/) 40 23 63
538 40 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 2 38 40
539 . ": variable '$vname' is an array but should be a $types{$type}->{d}$file"
540 if ref $value eq 'ARRAY'; #"}"
541 38 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 3 35 38
542 . ": variable '$vname' is a node but should be a $types{$type}->{d}$file"
543 if ref $value; #"}"
544  
545 35 $value = $self->convert_value( $value, $vname );
546  
547 35 ▶ return $value if $type eq 'S';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "S" 8 27 35
548  
549 27 $value =~ s/^\s+//;
550 27 $value =~ s/\s+$//;
551  
552 27 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$types{$type}{"e"}/i 15 12 27
553 . ": value '$value' for variable '$vname' is not a prooper $types{$type}->{d}$file"
554 unless $value =~ /$types{$type}->{e}/i; #}"}"
555  
556 12 ▶ return $value if $type ne 'B';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "B" 10 2 12
557 2 ▶ return $value =~ /(1|on|yes|true)/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /(1|on|yes|true)/i ? : 1 1 2
558 }
559  
560 23 my $ref = ref $value;
561 23 my @ret;
562 23 ▶ my $arr = $ref ? $value : [$value];
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 19 4 23
563  
564 23 ▶ croak __PACKAGE__ . ": variable '$vname' should be an array$file"
Branch: 1 branch
BranchTrueFalseTotal
if $ref and $ref ne "ARRAY" 1 22 23
Condition100%3/3
$ref and $ref ne "ARRAY"
!ll&&!rl&&r
4181
MC/DC100%2/2
$ref and $ref ne "ARRAY"
$ref $ref ne "ARRAY"
Truth table100%3/3
A and B
A= $refB= $ref ne "ARRAY"
ABresult
0X0
100
111
565 if $ref && $ref ne 'ARRAY';
566  
567 22 ▶ if ( $type eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'S') { } 8 14 22
568 8 @ret = @$arr;
569 }
570 else {
571 14 foreach $value (@$arr) {
572 26 $value =~ s/^\s+//;
573 26 $value =~ s/\s+$//;
574  
575 26 my @values = split /\s*,\s*/, $value;
576  
577 26 foreach $value (@values) {
578 32 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$types{$type}{"e"}/i 1 31 32
579 . ": element '$value' of variable '$vname' is not a prooper $types{$type}->{d}$file"
580 if $value !~ /$types{$type}->{e}/i; #}"}"
581  
582 31 push @ret, $value;
583 }
584 }
585 }
586  
587 21 return [ map $self->convert_value( $_, $vname ), @ret ];
588 }
589  
590 72 ▶sub syntax_check_get_type {
Subroutine syntax_check_get_type: called CC=3 SCAR=11.0
Pod undocumented
591 72 my ($syntax) = @_;
592  
593 72 foreach (qw( A B D E I N T )) {
594 398 ▶ return $_ if $syntax =~ /$_/;
Branch: 1 branch
BranchTrueFalseTotal
if $syntax =~ /$_/ 48 350 398
595 }
596  
597 24 return 'S';
598 }
599  
600 5 my %back_slash = (
601 36 => 36,
602 92 => 92,
603 97 => 7,
604 98 => 8,
605 102 => 12,
606 110 => 10,
607 114 => 13,
608 116 => 9,
609 118 => 11,
610 );
611  
612 152 ▶sub convert_value {
Subroutine convert_value: called CC=15 SCAR=27.1
Pod undocumented
613 152 my ( $self, $value, $vname ) = @_;
614  
615 152 my @arr = unpack 'C*', $value;
616 152 my @ret;
617  
618 152 ▶ while ( my $c = shift @arr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = shift @arr) 641 145 786
619 641 ▶ if ( $c == 92 && scalar @arr ) { # \
Branch: 2 branches
BranchTrueFalseTotal
if ($c == 92 and scalar @arr) { } 12 629 641
elsif ($c == 36 and scalar @arr and $arr[0] == 123) { } 13 616 629
Condition100%2/2
$c == 92 and scalar @arr
!ll
62912
MC/DC100%1/1
$c == 92 and scalar @arr
$c == 92
Truth table100%2/2
A
A= $c == 92
Aresult
00
11
620 12 my $n = shift @arr;
621  
622 12 ▶ if ( $back_slash{$n} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($back_slash{$n}) { } 11 1 12
623 11 push @ret, $back_slash{$n};
624 }
625 else {
626 1 push @ret, 92, $n;
627 }
628 }
629 ▶ elsif ( $c == 36 && scalar @arr && $arr[0] == 123 ) { # $
Condition100%5/5
$c == 36 and scalar @arr and $arr[0] == 123
!ll&&!rl&&r
613313
$c == 36 and scalar @arr
!ll
61316
MC/DC100%2/2
$c == 36 and scalar @arr and $arr[0] == 123
$c == 36 $arr[0] == 123
Truth table100%3/3
A and B
A= $c == 36B= $arr[0] == 123
ABresult
0X0
100
111
630 13 my $i;
631 my $v;
632 13 my @var_name;
633  
634 13 ▶ for ( $i = 1 ; $i < scalar @arr && $arr[$i] != 125 ; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @arr and $arr[$i] != 125) 52 13 65
Condition100%3/3
$i < scalar @arr and $arr[$i] != 125
!ll&&!rl&&r
11252
MC/DC100%2/2
$i < scalar @arr and $arr[$i] != 125
$i < scalar @arr $arr[$i] != 125
Truth table100%3/3
A and B
A= $i < scalar @arrB= $arr[$i] != 125
ABresult
0X0
100
111
635 52 push @var_name, $arr[$i];
636 }
637  
638 13 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if $i == scalar @arr 1 12 13
639 . ": systax error in inline variable substitution for value '$value' for variable '$vname'"
640 if $i == scalar @arr;
641 12 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless $self->opt->root 1 11 12
642 . ": can't do inline variable substitution for variable '$vname' when reference to root node was lost"
643 unless $self->opt->root;
644  
645 11 eval {
646 11 $v = $self->opt->root->get( split /->/, pack 'C*', @var_name );
647 };
648  
649 11 ▶ croak "$@ during inline variable sostitution for variable '$vname'"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 4 7 11
650 if $@;
651 7 ▶ croak __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 1 6 7
652 . ": can't use node or array variable in inline variable sostitution for variable '$vname'"
653 if ref $v;
654  
655 6 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 5 6
656 6 push @ret, unpack 'C*', $v;
657 6 splice @arr, 0, $i + 1;
658 }
659 else {
660 616 push @ret, $c;
661 }
662 }
663  
664 145 return pack 'C*', @ret;
665 }
666  
667 package Config::General::Hierarchical::Options;
668  
669 5 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
670  
671 5 my @options = qw( inherits root undefined wild );
672 5 our %options = map( ( $_ => 1 ), @options );
673  
674 5 __PACKAGE__->mk_accessors( @options, qw( files general struct ) );
675  
676 package Config::General::Hierarchical::Value;
677  
678 5 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
679  
680 5 __PACKAGE__->mk_accessors(qw( file value ));
681  
682 5 1;
683  
684 __END__
↑ Summary Hierarchical/Dump.pm »