| 1 | package Config::Processor; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.008000; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 5 | our $VERSION = '0.28'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use YAML::XS qw( LoadFile ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Cpanel::JSON::XS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Hash::Merge; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Scalar::Util qw( refaddr readonly ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 5 | my %FILE_EXTENSIONS_MAP = ( | |||||||||||||||||||||||||||||||
| 17 | yml => 'yaml', | ||||||||||||||||||||||||||||||||
| 18 | yaml => 'yaml', | ||||||||||||||||||||||||||||||||
| 19 | json => 'json', | ||||||||||||||||||||||||||||||||
| 20 | jsn => 'json', | ||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 24 | 7 | my $class = shift; | |||||||||||||||||||||||||||||||
| 25 | 7 | my %params = @_; | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 7 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 7 | ▶ | $self->{dirs} = $params{dirs} || []; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$params{'dirs'} || []
MC/DC100%1/1
$params{'dirs'} || []
$params{'dirs'}
Truth table100%2/2
A
A= $params{'dirs'}
| |||||||||||||||||||||||||||||||||
| 30 | 7 | ▶ | unless ( @{ $self->{dirs} } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 2 | push( @{ $self->{dirs} }, '.' ); | |||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | $self->{interpolate_variables} = exists $params{interpolate_variables} | ||||||||||||||||||||||||||||||||
| 35 | 7 | ▶ | ? $params{interpolate_variables} : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | $self->{process_directives} = exists $params{process_directives} | ||||||||||||||||||||||||||||||||
| 37 | 7 | ▶ | ? $params{process_directives} : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | 7 | $self->{export_env} = $params{export_env}; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 7 | my $merger = Hash::Merge->new; | |||||||||||||||||||||||||||||||
| 41 | $merger->specify_behavior( | ||||||||||||||||||||||||||||||||
| 42 | { SCALAR => { | ||||||||||||||||||||||||||||||||
| 43 | 105 | ▶ | SCALAR => sub { $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | ARRAY => sub { $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 45 | 4 | ▶ | HASH => sub { $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||||||
| 47 | ARRAY => { | ||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | SCALAR => sub { $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 49 | 39 | ▶ | ARRAY => sub { $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | HASH => sub { $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||||||
| 52 | HASH => { | ||||||||||||||||||||||||||||||||
| 53 | 8 | ▶ | SCALAR => sub { $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | ARRAY => sub { $_[1] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 55 | 193 | ▶ | HASH => sub { $merger->_merge_hashes( $_[0], $_[1] ) }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||||||
| 58 | 7 | 'CONFIG_PRECEDENT', | |||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||
| 60 | 7 | $self->{_merger} = $merger; | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 7 | $self->{_config} = undef; | |||||||||||||||||||||||||||||||
| 63 | 7 | $self->{_vars} = {}; | |||||||||||||||||||||||||||||||
| 64 | 7 | $self->{_seen_nodes} = {}; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 7 | return $self; | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||||||
| 70 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 5 | foreach my $name ( | |||||||||||||||||||||||||||||||
| 73 | qw( interpolate_variables process_directives export_env ) ) | ||||||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||||||
| 75 | 15 | ▶ | *{$name} = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 76 | 16 | my $self = shift; | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 16 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 7 | $self->{$name} = shift; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 16 | return $self->{$name}; | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | 15 | } | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 15 | ▶ | sub load { | ||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 88 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||
| 89 | 15 | my @config_sections = @_; | |||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 15 | $self->{_config} = $self->_build_tree(@config_sections); | |||||||||||||||||||||||||||||||
| 92 | 10 | ▶ | if ( $self->{export_env} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | $self->{_config} = $self->{_merger}->merge( $self->{_config}, | ||||||||||||||||||||||||||||||||
| 94 | 1 | { ENV => {%ENV} } ); | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | 10 | $self->_process_tree( $self->{_config}, [] ); | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 9 | $self->{_vars} = {}; | |||||||||||||||||||||||||||||||
| 99 | 9 | $self->{_seen_nodes} = {}; | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 9 | return $self->{_config}; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 32 | ▶ | sub _build_tree { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_tree: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||
| 105 | 32 | my $self = shift; | |||||||||||||||||||||||||||||||
| 106 | 32 | my @config_sections = @_; | |||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 32 | my $config = {}; | |||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 32 | foreach my $config_section (@config_sections) { | |||||||||||||||||||||||||||||||
| 111 | 66 | ▶ | next unless defined $config_section; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 66 | ▶ | if ( ref($config_section) eq 'HASH' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | 2 | $config = $self->{_merger}->merge( $config, $config_section ); | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||||||||||||||
| 117 | 64 | my %not_found_idx; | |||||||||||||||||||||||||||||||
| 118 | 64 | my @file_patterns = split( /\s+/, $config_section ); | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 64 | foreach my $dir ( @{ $self->{dirs} } ) { | |||||||||||||||||||||||||||||||
| 121 | 65 | foreach my $file_pattern ( @file_patterns ) { | |||||||||||||||||||||||||||||||
| 122 | 5 | ▶ | my @file_pathes = glob( File::Spec->catfile( $dir, $file_pattern ) ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 67 | foreach my $file_path (@file_pathes) { | |||||||||||||||||||||||||||||||
| 125 | 73 | ▶ | next if -d $file_path; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 73 | ▶ | unless ( $file_path =~ m/\.([^.]+)$/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 128 | 1 | croak "File extension not specified." | |||||||||||||||||||||||||||||||
| 129 | . " Don't known how parse $file_path"; | ||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 72 | my $file_ext = $1; | |||||||||||||||||||||||||||||||
| 133 | 72 | my $file_type = $FILE_EXTENSIONS_MAP{$file_ext}; | |||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 72 | ▶ | unless ( defined $file_type ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 1 | croak "Unknown file extension \".$file_ext\" encountered." | |||||||||||||||||||||||||||||||
| 137 | . " Don't known how parse $file_path"; | ||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | 71 | ▶ | unless ( -e $file_path ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | 4 | ▶ | $not_found_idx{$file_pattern} ||= 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$not_found_idx{$file_pattern} ||= 0
MC/DC100%1/1
$not_found_idx{$file_pattern} ||= 0
$not_found_idx{$file_pattern}
Truth table100%2/2
A
A= $not_found_idx{$file_pattern}
| |||||||||||||||||||||||||||||||||
| 142 | 4 | $not_found_idx{$file_pattern}++; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 4 | next; | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 67 | my @data = eval { | |||||||||||||||||||||||||||||||
| 148 | 67 | my $method = "_load_$file_type"; | |||||||||||||||||||||||||||||||
| 149 | 67 | return $self->$method($file_path); | |||||||||||||||||||||||||||||||
| 150 | }; | ||||||||||||||||||||||||||||||||
| 151 | 67 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 152 | 2 | croak "Can't parse $file_path\n$@"; | |||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | 65 | foreach my $data_chunk (@data) { | |||||||||||||||||||||||||||||||
| 156 | 65 | $config = $self->{_merger}->merge( $config, $data_chunk ); | |||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | my @not_found = grep { | ||||||||||||||||||||||||||||||||
| 163 | 60 | $not_found_idx{$_} == scalar @{ $self->{dirs} } | |||||||||||||||||||||||||||||||
| 164 | } keys %not_found_idx; | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 60 | ▶ | if ( @not_found ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | croak "Can't locate " . join( ', ', @not_found ) | ||||||||||||||||||||||||||||||||
| 168 | 1 | . " in " . join( ', ', @{ $self->{dirs} } ); | |||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 27 | return $config; | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 44 | ▶ | sub _load_yaml { | ||||||||||||||||||||||||||||||
|
Subroutine
_load_yaml: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 177 | 44 | my $self = shift; | |||||||||||||||||||||||||||||||
| 178 | 44 | my $file_path = shift; | |||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 44 | return LoadFile($file_path); | |||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | 23 | ▶ | sub _load_json { | ||||||||||||||||||||||||||||||
|
Subroutine
_load_json: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 184 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||
| 185 | 23 | my $file_path = shift; | |||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 23 | ▶ | open( my $fh, '<', $file_path ) || die "Can't open $file_path: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 188 | 23 | my @data = ( decode_json( join( '', <$fh> ) ) ); | |||||||||||||||||||||||||||||||
| 189 | 22 | close($fh); | |||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | 22 | return @data; | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | 963 | ▶ | sub _process_tree { | ||||||||||||||||||||||||||||||
|
Subroutine
_process_tree: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||
| 195 | 963 | my $self = shift; | |||||||||||||||||||||||||||||||
| 196 | 963 | my $ancs = pop; | |||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 963 | ▶ | return if readonly( $_[0] ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 963 | $_[0] = $self->_process_node( $_[0], $ancs ); | |||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | 962 | ▶ | if ( my $node_addr = refaddr( $_[0] ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 203 | 266 | ▶ | return if $self->{_seen_nodes}{$node_addr}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | 257 | $self->{_seen_nodes}{$node_addr} = 1; | |||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 953 | ▶ | if ( ref( $_[0] ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 209 | 175 | foreach ( values %{ $_[0] } ) { | |||||||||||||||||||||||||||||||
| 210 | 775 | $self->_process_tree( $_, [ $_[0], @{$ancs} ] ); | |||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||
| 213 | elsif ( ref( $_[0] ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||
| 214 | 82 | foreach ( @{ $_[0] } ) { | |||||||||||||||||||||||||||||||
| 215 | 178 | $self->_process_tree( $_, [ $_[0], @{$ancs} ] ); | |||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | 951 | return; | |||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 1092 | ▶ | sub _process_node { | ||||||||||||||||||||||||||||||
|
Subroutine
_process_node: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||
| 223 | 1092 | my $self = shift; | |||||||||||||||||||||||||||||||
| 224 | 1092 | my $node = shift; | |||||||||||||||||||||||||||||||
| 225 | 1092 | my $ancs = shift; | |||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | 1092 | ▶ | return unless defined $node; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 1092 | ▶ | if ( !ref($node) && $self->{interpolate_variables} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not ref $node and $self->{"interpolate_variables"}
MC/DC100%2/2
not ref $node and $self->{"interpolate_variables"}
not ref $node
$self->{"interpolate_variables"}
Truth table100%3/3
not(A) and B
A= not ref $nodeB= $self->{"interpolate_variables"}
| |||||||||||||||||||||||||||||||||
| 230 | 699 | $node =~ s/\$((\$?)\{([^\}]*)\})/ | |||||||||||||||||||||||||||||||
| 231 | 85 | ▶ | $2 ? $1 : ( $self->_resolve_var( $3, [ @{$ancs} ] ) || '' )/ge; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->_resolve_var($3, [@{$ancs;}]) or ""
MC/DC100%1/1
$self->_resolve_var($3, [@{$ancs;}]) or ""
$self->_resolve_var($3, [@{$ancs;}])
Truth table100%2/2
A
A= $self->_resolve_var($3, [@{$ancs;}])
| |||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||
| 233 | ▶ | elsif ( ref($node) eq 'HASH' && $self->{process_directives} ) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
ref $node eq "HASH" and $self->{"process_directives"}
MC/DC100%2/2
ref $node eq "HASH" and $self->{"process_directives"}
ref $node eq "HASH"
$self->{"process_directives"}
Truth table100%3/3
A and B
A= ref $node eq "HASH"B= $self->{"process_directives"}
| |||||||||||||||||||||||||||||||||
| 234 | 198 | ▶ | if ( defined $node->{var} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 235 | 35 | $node = $self->_resolve_var( $node->{var}, [ @{$ancs} ] ); | |||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||
| 237 | elsif ( defined $node->{include} ) { | ||||||||||||||||||||||||||||||||
| 238 | 17 | $node = $self->_build_tree( $node->{include} ); | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | else { | ||||||||||||||||||||||||||||||||
| 241 | 146 | ▶ | if ( defined $node->{underlay} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 242 | 18 | my $layer = delete $node->{underlay}; | |||||||||||||||||||||||||||||||
| 243 | 18 | $layer = $self->_process_layer( $layer, $ancs ); | |||||||||||||||||||||||||||||||
| 244 | 18 | $node = $self->{_merger}->merge( $layer, $node ); | |||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | 146 | ▶ | if ( defined $node->{overlay} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 248 | 15 | my $layer = delete $node->{overlay}; | |||||||||||||||||||||||||||||||
| 249 | 15 | $layer = $self->_process_layer( $layer, $ancs ); | |||||||||||||||||||||||||||||||
| 250 | 15 | $node = $self->{_merger}->merge( $node, $layer ); | |||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | 1091 | return $node; | |||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 33 | ▶ | sub _process_layer { | ||||||||||||||||||||||||||||||
|
Subroutine
_process_layer: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 259 | 33 | my $self = shift; | |||||||||||||||||||||||||||||||
| 260 | 33 | my $layer = shift; | |||||||||||||||||||||||||||||||
| 261 | 33 | my $ancs = shift; | |||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | 33 | ▶ | if ( ref($layer) eq 'HASH' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 264 | 18 | $layer = $self->_process_node( $layer, $ancs ); | |||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||
| 266 | elsif ( ref($layer) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||
| 267 | 11 | my $new_layer = {}; | |||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||
| 269 | 11 | foreach my $node ( @{$layer} ) { | |||||||||||||||||||||||||||||||
| 270 | 28 | $node = $self->_process_node( $node, $ancs ); | |||||||||||||||||||||||||||||||
| 271 | 28 | $new_layer = $self->{_merger}->merge( $new_layer, $node ); | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | 11 | $layer = $new_layer; | |||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | 33 | return $layer; | |||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | 118 | ▶ | sub _resolve_var { | ||||||||||||||||||||||||||||||
|
Subroutine
_resolve_var: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||
| 281 | 118 | my $self = shift; | |||||||||||||||||||||||||||||||
| 282 | 118 | my $name = shift; | |||||||||||||||||||||||||||||||
| 283 | 118 | my $ancs = shift; | |||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||
| 285 | 118 | my $value; | |||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | 118 | ▶ | if ( $name =~ m/^\./ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 288 | 10 | my $node; | |||||||||||||||||||||||||||||||
| 289 | 10 | my @tokens = split( /\./, $name, -1 ); | |||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | 10 | while (1) { | |||||||||||||||||||||||||||||||
| 292 | 24 | my $token = $tokens[0]; | |||||||||||||||||||||||||||||||
| 293 | 24 | $token =~ s/^\s+//; | |||||||||||||||||||||||||||||||
| 294 | 24 | $token =~ s/\s+$//; | |||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | 24 | ▶ | last if length($token) > 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | 14 | shift @tokens; | |||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | 14 | ▶ | last unless @tokens; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 301 | 14 | ▶ | next unless @{$ancs}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | 14 | $node = shift @{$ancs}; | |||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||
| 306 | 10 | $value = eval { | |||||||||||||||||||||||||||||||
| 307 | 10 | $self->_fetch_value( $node, $ancs, \@tokens ); | |||||||||||||||||||||||||||||||
| 308 | }; | ||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | 10 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 311 | 0 | chomp $@; | |||||||||||||||||||||||||||||||
| 312 | 0 | die qq{Can't resolve variable "$name"; $@\n}; | |||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||
| 315 | else { | ||||||||||||||||||||||||||||||||
| 316 | 108 | my $vars = $self->{_vars}; | |||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||
| 318 | 108 | ▶ | unless ( defined $vars->{$name} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 319 | 83 | my @tokens = split( /\./, $name, -1 ); | |||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | 83 | $vars->{$name} = eval { | |||||||||||||||||||||||||||||||
| 322 | 83 | $self->_fetch_value( $self->{_config}, [], \@tokens ); | |||||||||||||||||||||||||||||||
| 323 | }; | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | 83 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 326 | 1 | chomp $@; | |||||||||||||||||||||||||||||||
| 327 | 1 | die qq{Can't resolve variable "$name"; $@\n}; | |||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | 107 | $value = $vars->{$name}; | |||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | 117 | return $value; | |||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | #### | ||||||||||||||||||||||||||||||||
| 338 | 93 | ▶ | sub _fetch_value { | ||||||||||||||||||||||||||||||
|
Subroutine
_fetch_value: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||
| 339 | 93 | my $self = shift; | |||||||||||||||||||||||||||||||
| 340 | 93 | my $node = shift; | |||||||||||||||||||||||||||||||
| 341 | 93 | my $ancs = shift; | |||||||||||||||||||||||||||||||
| 342 | 93 | my $tokens = shift; | |||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | 93 | ▶ | return $node unless @{$tokens}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | 93 | my $value; | |||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||
| 348 | 93 | while (1) { | |||||||||||||||||||||||||||||||
| 349 | 240 | my $token = shift @{$tokens}; | |||||||||||||||||||||||||||||||
| 350 | 240 | $token =~ s/^\s+//; | |||||||||||||||||||||||||||||||
| 351 | 240 | $token =~ s/\s+$//; | |||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | 240 | ▶ | if ( ref($node) eq 'HASH' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 354 | 205 | ▶ | last unless defined $node->{$token}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | 198 | unshift( @{$ancs}, $node ); | |||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | 198 | ▶ | unless ( @{$tokens} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 359 | 55 | $node->{$token} = $self->_process_node( $node->{$token}, $ancs ); | |||||||||||||||||||||||||||||||
| 360 | 55 | $value = $node->{$token}; | |||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||
| 362 | 55 | last; | |||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | 143 | ▶ | last unless ref( $node->{$token} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||
| 367 | 143 | $node = $node->{$token}; | |||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||
| 369 | else { # ARRAY | ||||||||||||||||||||||||||||||||
| 370 | 35 | ▶ | if ( $token =~ m/\D/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 371 | 1 | die qq{Argument "$token" isn't numeric in array element.\n}; | |||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | 34 | ▶ | last unless defined $node->[$token]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||
| 376 | 32 | unshift( @{$ancs}, $node ); | |||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | 32 | ▶ | unless ( @{$tokens} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 379 | 28 | $node->[$token] = $self->_process_node( $node->[$token], $ancs ); | |||||||||||||||||||||||||||||||
| 380 | 28 | $value = $node->[$token]; | |||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | 28 | last; | |||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||
| 385 | 4 | ▶ | last unless ref( $node->[$token] ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||
| 387 | 4 | $node = $node->[$token]; | |||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | 92 | return $value; | |||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||
| 394 | 5 | 1; | |||||||||||||||||||||||||||||||
| 395 | __END__ | ||||||||||||||||||||||||||||||||