blib/lib/Config/Model/ValueComputer.pm

filter: Statementstmt 95.3% 224 / 235 Branchbran 75.0% 87 / 116 Conditioncond 87.8% 29 / 33 MC/DCmcdc 76.1% 16 / 21 Subroutinesub 100% 32 / 32 Podpod 50.0% 2 / 4 totaltotal 88.4% 390 / 441 CC 76 SCAR 21.8
CC
76
Cov
89%
compute_info
19.2
compute
14.0
compute_variables
12.0
CRAP 8.8
« blib/lib/Config/Model/Instance.pm ↑ Summary blib/lib/Config/Model/ObjTreeScanner.pm »
1 #
2 # This file is part of Config-Model
3 #
4 # This software is Copyright (c) 2005-2022 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::ValueComputer 2.169;
11  
12 59 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 59 ▶use MouseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # use Scalar::Util qw(weaken) ;
16 59 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 59 ▶use Parse::RecDescent 1.90.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 59 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 59 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 59 ▶use vars qw($compute_grammar $compute_parser);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 59 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 59 ▶no warnings qw/experimental::postderef experimental::signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 59 my $logger = get_logger("ValueComputer");
27  
28 # allow_override is intercepted and handled by Value object
29  
30 59 has formula => ( is => 'ro', isa => 'Str', required => 1 );
31 59 has value_type => ( is => 'ro', isa => 'Str', required => 1 );
32  
33 # value_object is mostly used for error messages
34 59 has value_object => (
35 is => 'ro',
36 isa => 'Config::Model::AnyThing',
37 required => 1,
38 weak_ref => 1,
39 handles => [qw/grab grab_value location index element/],
40 );
41  
42 22 ▶has variables => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
43 158 ▶has replace => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
44 59 has [qw/use_eval allow_override use_as_upstream_default/] =>
45 ( is => 'ro', isa => 'Bool', default => 0 );
46  
47 has allow_user_override => (
48 is => 'ro',
49 isa => 'Bool',
50 lazy => 1,
51 builder =>
52 9 ▶ sub { my $self = shift; return $self->allow_override || $self->use_as_upstream_default; } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%3/3
$self->allow_override || $self->use_as_upstream_default
l!l&&r!l&&!r
711
MC/DC100%2/2
$self->allow_override || $self->use_as_upstream_default
$self->allow_override $self->use_as_upstream_default
Truth table100%3/3
A || B
A= $self->allow_overrideB= $self->use_as_upstream_default
ABresult
1X1
011
000
53  
54 59 has need_quote => ( is => 'ro', isa => 'Bool', builder => '_need_quote', lazy => 1 );
55  
56 167 ▶sub _need_quote {
Subroutine _need_quote: called CC=3 SCAR=11.0
57 167 my $self = shift;
58  
59 167 my $need_quote = 0;
60 167 ▶ $need_quote = 1 if $self->{use_eval} and $self->{value_type} !~ /(integer|number|boolean)/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"use_eval"} and not $self->{"value_type"} =~ /(integer|number|boolean)/ 12 155 167
Condition66%2/3
$self->{"use_eval"} and not $self->{"value_type"} =~ /(integer|number|boolean)/
!ll&&!rl&&r
155012
MC/DC50%1/2
$self->{"use_eval"} and not $self->{"value_type"} =~ /(integer|number|boolean)/
$self->{"use_eval"} not $self->{"value_type"} =~ /(integer|number|boolean)/
Truth table66%2/3
A and not(B)
A= $self->{"use_eval"}B= not $self->{"value_type"} =~ /(integer|number|boolean)/
ABresult
0X0
100
111
61 167 return $need_quote;
62 }
63  
64 59 has undef_is => ( is => 'ro', isa => 'Maybe[Str]' );
65  
66 59 has undef_replacement => (
67 is => 'ro',
68 isa => 'Maybe[Str]',
69 builder => '_build_undef_replacement',
70 lazy => 1
71 );
72  
73 164 ▶sub _build_undef_replacement {
Subroutine _build_undef_replacement: called CC=4 SCAR=13.9
74 164 my $self = shift;
75  
76 164 my $sui = $self->undef_is;
77  
78 164 ▶ return defined $sui && $sui eq "''" ? ''
Branch: 2 branches
BranchTrueFalseTotal
defined $sui && $sui eq q[''] ? : 2 162 164
defined $sui ? : 3 159 162
Condition100%3/3
defined $sui and $sui eq "''"
!ll&&!rl&&r
15932
MC/DC100%2/2
defined $sui and $sui eq "''"
defined $sui $sui eq "''"
Truth table100%3/3
A and B
A= defined $suiB= $sui eq "''"
ABresult
0X0
100
111
79 : defined $sui ? $sui
80 : undef;
81  
82 }
83  
84 167 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.2
Pod documented
85 167 my $self = shift;
86  
87 # create parser if needed
88 167 ▶ $compute_parser ||= Parse::RecDescent->new($compute_grammar);
Condition66%2/3
$Config::Model::ValueComputer::compute_parser ||= "Parse::RecDescent"->new($Config::Model::ValueComputer::compute_grammar)
l!l&&r!l&&!r
148190
MC/DC0%0/2
$Config::Model::ValueComputer::compute_parser ||= "Parse::RecDescent"->new($Config::Model::ValueComputer::compute_grammar)
$Config::Model::ValueComputer::compute_parser "Parse::RecDescent"->new($Config::Model::ValueComputer::compute_grammar)
Truth table66%2/3
A ||= B
A= $Config::Model::ValueComputer::compute_parserB= "Parse::RecDescent"->new($Config::Model::ValueComputer::compute_grammar)
ABresult
1X1
011
000
89  
90 167 $logger->trace("called with formula: $self->{formula}");
91 # must make a first pass at computation to subsitute index and
92 # element values. leaves $xxx outside of &index or &element untouched
93 my $result_r =
94 $compute_parser->pre_compute( $self->{formula}, 1, $self->{value_object},
95 167 $self->{variables}, $self->{replace}, 'yes', $self->need_quote, );
96  
97 167 ▶ $logger->trace("pre_formula: ". ($result_r ? $$result_r : ' pre_compute failed, using original formula'));
Branch: 1 branch
BranchTrueFalseTotal
$result_r ? : 167 0 167
98 167 ▶ $self->{pre_formula} = $result_r ? $$result_r : $self->{formula};
Branch: 1 branch
BranchTrueFalseTotal
$result_r ? : 167 0 167
99 167 return;
100 }
101  
102 527 ▶sub compute ($self, %args) {
Subroutine compute: called CC=14 SCAR=26.4
Pod documented
103 527 ▶ my $check = $args{check} || 'yes';
Condition100%2/2
$args{'check'} || 'yes'
l!l
7520
MC/DC100%1/1
$args{'check'} || 'yes'
$args{'check'}
Truth table100%2/2
A
A= $args{'check'}
Aresult
11
00
104  
105 527 my $pre_formula = $self->{pre_formula};
106 527 $logger->trace("called with pre_formula: $pre_formula");
107 527 my $variables = $self->compute_variables( check => $check );
108  
109 527 ▶ die "internal error" unless defined $variables;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variables 0 527 527
110  
111 527 my $result;
112 my @parser_args = (
113 $self->{value_object}, $variables, $self->{replace}, $check, $self->{need_quote},
114 527 $self->undef_replacement
115 );
116  
117 527 ▶ if ( $self->{use_eval}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_eval'} or $self->{'value_type'} =~ /(integer|number|boolean)/) { } 67 460 527
Condition100%3/3
$self->{"use_eval"} or $self->{"value_type"} =~ /(integer|number|boolean)/
l!l&&r!l&&!r
3433460
MC/DC100%2/2
$self->{"use_eval"} or $self->{"value_type"} =~ /(integer|number|boolean)/
$self->{"use_eval"} $self->{"value_type"} =~ /(integer|number|boolean)/
Truth table100%3/3
A or B
A= $self->{"use_eval"}B= $self->{"value_type"} =~ /(integer|number|boolean)/
ABresult
1X1
011
000
118 or $self->{value_type} =~ /(integer|number|boolean)/ ) {
119 67 $logger->trace("will use eval");
120 67 my $all_defined = 1;
121 67 my %__vars;
122 67 foreach my $key ( sort keys %$variables ) {
123  
124 # no need to get variable if not used in formula;
125 89 ▶ next unless index( $pre_formula, $key ) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::index($pre_formula, $key) > 0 0 89 89
126 89 my $vr = _value_from_object( $key, @parser_args );
127 89 my $v = $$vr;
128 89 ▶ $v = $self->undef_replacement unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 4 85 89
129 89 ▶ $logger->debug( "compute: var $key -> ", ( defined $v ? $v : '<undef>' ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 87 2 89
130 # security: $v are stored in %__vars hash, so they are
131 # used by eval'ed code, but not directly eval'ed
132 89 ▶ if ( defined $v ) { $__vars{$key} = $v }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) { } 87 2 89
133 2 else { $all_defined = 0; }
134 }
135  
136 67 ▶ if ($all_defined) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_defined) 66 1 67
137 66 my $formula = $pre_formula;
138 66 ▶ $formula =~ s/\$([_a-zA-Z]\w*)/defined $__vars{$1} ? "\$__vars{$1}" : "\$$1" /eg;
Branch: 1 branch
BranchTrueFalseTotal
defined $__vars{$1} ? : 87 17 104
139 66 $logger->debug("compute: evaluating '$formula'");
140 66 ▶ $result = eval $formula; ## no critic (ProhibitStringyEval)
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
141 66 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 66 66
142 Config::Model::Exception::Formula->throw(
143 object => $self->{value_object},
144 0 error => "Eval of formula '$formula' failed:\n$@"
145 . "Make sure that your element is indeed "
146 . "'$self->{value_type}'"
147 );
148 }
149 }
150 }
151 else {
152 460 $logger->trace("calling parser with compute on pre_formula $pre_formula");
153 460 my $formula_r = $compute_parser->compute( $pre_formula, 1, @parser_args );
154  
155 460 $result = $$formula_r;
156  
157 #$result = $self->{computed_formula} = $formula;
158 }
159  
160 527 ▶ $logger->debug( "compute result is '" . ( defined $result ? $result : '<undef>' ) . "'" );
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 512 15 527
161  
162 527 return $result;
163 }
164  
165 8 ▶sub compute_info ($self, %args) {
Subroutine compute_info: called CC=14 SCAR=29.5
Pod undocumented
166 8 ▶ my $check = $args{check} || 'yes';
Condition50%1/2
$args{'check'} || 'yes'
l!l
08
MC/DC0%0/1
$args{'check'} || 'yes'
$args{'check'}
Truth table50%1/2
A
A= $args{'check'}
Aresult
11
00
167 8 $logger->trace("compute_info called with $self->{formula}");
168  
169 8 my $orig_variables = $self->{variables};
170 8 my $variables = $self->compute_variables;
171 8 my $str = "value is computed from '$self->{formula}'";
172  
173 8 ▶ return $str unless defined $variables;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variables 0 8 8
174  
175 8 ▶ if (%$variables) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$variables) 7 1 8
176 7 $str .= ", where ";
177 7 foreach my $k ( sort keys %$variables ) {
178 14 my $u_val = $variables->{$k};
179 14 ▶ if ( ref($u_val) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $u_val) { } 0 14 14
180 0 foreach (sort keys %$u_val) {
181 0 $str .= "\n\t\t'\$$k" . "{$_} is converted to '$orig_variables->{$k}{$_}'";
182 }
183 }
184 else {
185 14 my $val;
186 14 ▶ if ( defined $u_val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $u_val) 11 3 14
187 11 my $obj = eval { $self->{value_object}->grab($u_val) };
188 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
189 0 my $e = $@;
190 0 ▶ my $msg = ref($e) ? $e->full_message : $e;
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 0 0 0
191 0 Config::Model::Exception::Model->throw(
192 object => $self,
193 error => "Compute variable:\n" . $msg
194 );
195 }
196 $val =
197 11 ▶ $obj->get_type eq 'node' ? '<node>'
Branch: 3 branches
BranchTrueFalseTotal
$obj->get_type eq 'node' ? : 0 11 11
$obj->get_type eq 'hash' ? : 0 11 11
$obj->get_type eq 'list' ? : 0 11 11
198 : $obj->get_type eq 'hash' ? '<hash>'
199 : $obj->get_type eq 'list' ? '<list>'
200 : $obj->fetch( check => $check );
201 }
202 14 $str .= "\n\t\t'$k' from path '$orig_variables->{$k}' is ";
203 14 ▶ $str .= defined $val ? "'$val'" : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 7 7 14
204 }
205 }
206 }
207  
208 #$str .= " (evaluated as '$self->{computed_formula}')"
209 # if $self->{formula} ne $self->{computed_formula} ;
210  
211 8 return $str;
212 }
213  
214 # internal. resolves variables that contains $foo or &bar
215 # returns a hash of variable names -> variable path
216 588 ▶sub compute_variables ($self, %args) {
Subroutine compute_variables: called CC=12 SCAR=24.9
Pod undocumented
217 588 ▶ my $check = $args{check} || 'yes';
Condition100%2/2
$args{'check'} || 'yes'
l!l
52761
MC/DC100%1/1
$args{'check'} || 'yes'
$args{'check'}
Truth table100%2/2
A
A= $args{'check'}
Aresult
11
00
218  
219 # a shallow copy should be enough as we don't allow
220 # replace in replacement rules
221 588 my %variables = %{ $self->{variables} };
222 588 ▶ $logger->trace( "called on variables '", join( "', '", sort keys %variables ), "'" )
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace 31 557 588
223 if $logger->is_trace;
224  
225 # apply a compute on all variables until no $var is left
226 588 my $var_left = scalar( keys %variables ) + 1;
227  
228 588 ▶ while ($var_left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_left) 592 588 1180
229 592 my $old_var_left = $var_left;
230 592 foreach my $key ( keys %variables ) {
231 243 my $value = $variables{$key}; # value may be undef
232 243 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 241 243
233  
234 #next if ref($value); # skip replacement rules
235 241 $logger->trace("key '$key', value '$value', left $var_left");
236 241 ▶ next unless $value =~ /\$|&/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\$|&/ 202 39 241
237  
238 my $pre_res_r =
239 $compute_parser->pre_compute( $value, 1, $self->{value_object}, \%variables,
240 39 $self->{replace}, $check );
241 39 $logger->trace("key '$key', pre res '$$pre_res_r', left $var_left\n");
242 39 $variables{$key} = $$pre_res_r;
243 39 ▶ $logger->trace( "variable after pre_compute: ", join( " ", keys %variables ) )
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace 0 39 39
244 if $logger->is_trace;
245  
246 39 ▶ if ( $$pre_res_r =~ /\$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pre_res_r =~ /\$/) 29 10 39
247 # variables needs to be evaluated
248 my $res_ref =
249 $compute_parser->compute( $$pre_res_r, 1, $self->{value_object}, \%variables,
250 29 $self->{replace}, $check );
251  
252 #return undef unless defined $res ;
253 29 $variables{$key} = $$res_ref;
254 29 ▶ $logger->trace( "variable after compute: ", join( " ", keys %variables ) )
Branch: 1 branch
BranchTrueFalseTotal
if $logger->is_trace 0 29 29
255 if $logger->is_trace;
256 }
257 {
258 59 ▶ no warnings "uninitialized"; ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
259 39 $logger->trace("result $key -> '$variables{$key}' left '$var_left'");
260 }
261 }
262  
263 592 ▶ my @var_left = grep { defined $variables{$_} && $variables{$_} =~ /[\$&]/ }
Condition100%3/3
defined $variables{$_} && $variables{$_} =~ /[\$&]/
!ll&&!rl&&r
112284
MC/DC100%2/2
defined $variables{$_} && $variables{$_} =~ /[\$&]/
defined $variables{$_} $variables{$_} =~ /[\$&]/
Truth table100%3/3
A && B
A= defined $variables{$_}B= $variables{$_} =~ /[\$&]/
ABresult
0X0
100
111
264 sort keys %variables;
265  
266 592 $var_left = @var_left;
267  
268 Config::Model::Exception::Formula->throw(
269 object => $self->{value_object},
270 592 ▶ error => "Can't resolve user variable: '" . join( "','", @var_left ) . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless $var_left < $old_var_left 0 592 592
271 ) unless ( $var_left < $old_var_left );
272 }
273  
274 588 $logger->trace("done");
275 588 return \%variables;
276 }
277  
278 22 ▶sub _pre_replace {
Subroutine _pre_replace: called CC=2 SCAR=6.9
279 22 my ( $replace_h, $pre_value ) = @_;
280  
281 22 $logger->trace("value: _pre_replace called with value '$pre_value'");
282 my $result =
283 exists $replace_h->{$pre_value}
284 22 ▶ ? $replace_h->{$pre_value}
Branch: 1 branch
BranchTrueFalseTotal
exists $replace_h->{$pre_value} ? : 3 19 22
285 : '$replace{' . $pre_value . '}';
286 22 return \$result;
287 }
288  
289 23 ▶sub _replace {
Subroutine _replace: called CC=7 SCAR=20.2
290 23 my ( $replace_h, $value, $value_object, $variables, $replace, $check, $need_quote, $undef_is )
291 = @_;
292  
293 23 ▶ if ( $logger->is_trace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger->is_trace) 0 23 23
294 0 ▶ my $str = defined $value ? $value : '<undef>';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
295 0 $logger->trace("value: _replace called with value '$str'");
296 }
297  
298 23 my $result;
299 23 ▶ if ( defined $value and $value =~ /\$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value and $value =~ /\$/) { } 4 19 23
elsif (defined $value) { } 10 9 19
Condition100%3/3
defined $value and $value =~ /\$/
!ll&&!rl&&r
9104
MC/DC100%2/2
defined $value and $value =~ /\$/
defined $value $value =~ /\$/
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /\$/
ABresult
0X0
100
111
300  
301 # must keep original variable
302 4 $result = '$replace{' . $value . '}';
303 }
304 elsif ( defined $value ) {
305 10 my $r = $replace_h->{$value};
306 10 ▶ $result = defined $r ? $r : $undef_is;
Branch: 1 branch
BranchTrueFalseTotal
defined $r ? : 8 2 10
307 }
308 23 return \$result;
309 }
310  
311 20 ▶sub _function_on_object {
Subroutine _function_on_object: called CC=2 SCAR=6.9
312 20 my ( $up, $function, $return, $value_object, $variables_h, $replace_h, $check, $need_quote ) =
313 @_;
314  
315 20 $logger->trace("handling &$function($up) ");
316  
317 20 my $target = $value_object->eval_function($function, $up, $check);
318 20 $return = \$target ;
319  
320 # print "\&foo(...) result = ",$$return," \n";
321  
322 # make sure that result of function is quoted (avoid bareword errors)
323 20 ▶ $$return = '"' . $$return . '"' if $need_quote;
Branch: 1 branch
BranchTrueFalseTotal
if $need_quote 1 19 20
324  
325 20 $logger->debug("&$function(...) returns $$return");
326 20 return $return;
327 }
328  
329 19 ▶sub _function_alone {
Subroutine _function_alone: called CC=7 SCAR=19.6
330 19 my ( $f_name, $return, $value_object, $variables_h, $replace_h, $check, $need_quote ) = @_;
331  
332 19 $logger->trace("_function_alone: handling $f_name");
333  
334 19 ▶ my $method_name =
Branch: 3 branches
BranchTrueFalseTotal
$f_name eq 'element' ? : 10 9 19
$f_name eq 'index' ? : 6 3 9
$f_name eq 'location' ? : 3 0 3
335 $f_name eq 'element' ? 'element_name'
336 : $f_name eq 'index' ? 'index_value'
337 : $f_name eq 'location' ? 'location'
338 : undef;
339  
340 19 ▶ Config::Model::Exception::Formula->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method_name 0 19 19
341 object => $value_object,
342 error => "Unknown computation function &$f_name, " . "expected &element or &index"
343 ) unless defined $method_name;
344  
345 19 my $result = $value_object->$method_name();
346  
347 19 my $vt = $value_object->value_type;
348 19 ▶ if ( $vt =~ /^integer|number|boolean$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vt =~ /^integer|number|boolean$/) 4 15 19
349 4 $result = '"' . $result . '"';
350 }
351  
352 19 $return = \$result;
353  
354 19 ▶ Config::Model::Exception::Formula->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 19 19
355 object => $value_object,
356 error => "Missing $f_name attribute (method '$method_name' on "
357 . ref($value_object) . ")\n"
358 ) unless defined $result;
359 19 return $return;
360 }
361  
362 491 ▶sub _compute {
Subroutine _compute: called CC=7 SCAR=19.6
363 491 my ( $value_ref, $return,
364 $value_object, $variables_h, $replace_h, $check, $need_quote, $undef_is )
365 = @_;
366  
367 491 my @values = map { $$_ } @{$value_ref};
368  
369 491 ▶ if ( $logger->is_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger->is_debug) 12 479 491
370 12 ▶ my @display = map { defined $_ ? $_ : '<undef>' } @values;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 24 0 24
371 12 $logger->debug( "_compute called with values '", join( "','", @display ) );
372 }
373  
374 491 my $result = '';
375  
376 # return undef if one value is undef
377 491 foreach my $v (@values) {
378 1101 ▶ if ( defined $v or defined $undef_is ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v or defined $undef_is) { } 1085 16 1101
Condition66%2/3
defined $v or defined $undef_is
l!l&&r!l&&!r
1085016
MC/DC50%1/2
defined $v or defined $undef_is
defined $v defined $undef_is
Truth table66%2/3
A or B
A= defined $vB= defined $undef_is
ABresult
1X1
011
000
379 1085 ▶ $result .= defined $v ? $v : $undef_is;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 1085 0 1085
380 }
381 else {
382 16 $result = undef;
383 16 last;
384 }
385 }
386  
387 491 return \$result;
388 }
389  
390 189 ▶sub _value_from_object {
Subroutine _value_from_object: called CC=10 SCAR=23.9
391 189 my ( $name, $value_object, $variables_h, $replace_h, $check, $need_quote ) = @_;
392  
393 $logger->warn("Warning: No variable definition found for \$$name")
394 189 ▶ unless exists $variables_h->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $variables_h->{$name} 0 189 189
395  
396 # $path can be a ref for test purpose, or can be undef if path is computed from another value
397 189 my $path = $variables_h->{$name};
398 189 my $my_res;
399  
400 189 ▶ if ( $logger->is_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logger->is_debug) 22 167 189
401 22 ▶ my $str = defined $path ? $path : '<undef>';
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 22 0 22
402 22 $logger->debug("replace \$$name with path $str...");
403 }
404  
405 189 ▶ if ( defined $path and $path =~ /[\$&]/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $path and $path =~ /[\$&]/) { } 4 185 189
elsif (defined $path) { } 178 7 185
Condition100%3/3
defined $path and $path =~ /[\$&]/
!ll&&!rl&&r
71784
MC/DC100%2/2
defined $path and $path =~ /[\$&]/
defined $path $path =~ /[\$&]/
Truth table100%3/3
A and B
A= defined $pathB= $path =~ /[\$&]/
ABresult
0X0
100
111
406 4 $logger->trace("skip name $name path '$path'");
407 4 $my_res = "\$$name"; # restore name that contain '$var'
408 }
409 elsif ( defined $path ) {
410  
411 178 $logger->trace("fetching var object '$name' with '$path'");
412  
413 178 $my_res = eval { $value_object->grab_value( step => $path, check => $check ); };
414  
415 178 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 178 178
416 0 my $e = $@;
417 0 ▶ my $msg = $e ? $e->full_message : '';
Branch: 1 branch
BranchTrueFalseTotal
$e ? : 0 0 0
418 0 Config::Model::Exception::Model->throw(
419 object => $value_object,
420 error => "Compute argument '$name', error with '$path':\n" . $msg
421 );
422 }
423  
424 178 ▶ $logger->trace( "fetched var object '$name' with '$path', result '",
Branch: 1 branch
BranchTrueFalseTotal
defined $my_res ? : 161 17 178
425 defined $my_res ? $my_res : '<undef>', "'" );
426 }
427  
428 # my_res stays undef if $path if not defined
429  
430 # quote result if asked when calling compute
431 #my $quote = $need_quote || 0;
432 #if ($quote && $my_res) {
433 # $my_res =~ s/'/\\'/g;
434 # $my_res = "'$my_res'";
435 #}
436  
437 189 return \$my_res; # So I can return undef ... or a ref to undef
438 }
439  
440 59 $compute_grammar = << 'END_OF_GRAMMAR' ;
441 {
442  
443 # This grammar is compatible with Parse::RecDescent < 1.90 or >= 1.90
444 use strict;
445 use warnings ;
446 }
447  
448 # computed value may return undef even if parsing is done right. To
449 # avoid getting problems with Parse::RecDescent (where undef means
450 # that the parsing did not match), we always return a scalar
451 # reference to the actual returned value
452  
453 # @arg is value_object, $variables_h, $replace_h, $check,$need_quote
454  
455 pre_compute: <skip:''> pre_value[@arg](s) {
456 # print "pre-compute on @{$item[-1]}\n";
457 my $str = join ( '', map { $$_ } @{ $item[-1] } ) ;
458 $return = \$str;
459 }
460  
461 pre_value:
462 <skip:''> '$replace' '{' /\s*/ pre_value[@arg] /\s*/ '}' {
463 $return = Config::Model::ValueComputer::_pre_replace($arg[2], ${ $item{pre_value} } ) ;
464 }
465 | <skip:''> function '(' /\s*/ up /\s*/ ')' {
466 $return = Config::Model::ValueComputer::_function_on_object($item{up},$item{function},$return,@arg ) ;
467 }
468 | <skip:''> '&' /\w+/ func_param(?) {
469 $return = Config::Model::ValueComputer::_function_alone($item[3],$return,@arg ) ;
470 }
471 | <skip:''> /\$( |\d+|_|!|&|@|{\^[A-Z]+})/ {
472 my $result = $item[-1] ;
473 $return = \$result ;
474 }
475 | object {
476 # print "pre_value handling \$foo\n";
477 my $object = $item{object};
478 my $result ="\$".$object ;
479 $return = \$result ;
480 }
481 | <skip:''> /[^\$&]*/ {
482 # print "pre_value copying '$item[-1]'\n";
483 my $result = $item[-1] ;
484 $return = \$result ;
485 }
486  
487 func_param: /\(\s*\)/
488  
489 up: /-\d+|-( ?-)*/
490  
491 compute: <skip:''> value[@arg](s) {
492 # if one value is undef, return undef;
493 Config::Model::ValueComputer::_compute($item[-1],$return,@arg ) ;
494 }
495  
496 value:
497 <skip:''> '$replace' '{' <commit> /\s*/ value_to_replace[@arg] /\s*/ '}' {
498 $return = Config::Model::ValueComputer::_replace($arg[2], ${ $item{value_to_replace} },@arg ) ;
499 }
500 | <skip:''> /\$(\d+|_)\b/ {
501 my $result = $item[-1] ;
502 $return = \$result ;
503 }
504 | <skip:''> object <commit> {
505 $return = Config::Model::ValueComputer::_value_from_object($item{object},@arg ) ;
506 1;
507 }
508 | <skip:''> /[^\$]*/ {
509 my $result = $item[-1] ;
510 $return = \$result ;
511 }
512  
513 value_to_replace:
514 <skip:''> object <commit> {
515 $return = Config::Model::ValueComputer::_value_from_object($item{object},@arg ) ;
516 1;
517 }
518 | <skip:''> /[\w\-\.+]*/ {
519 my $result = $item[-1] ;
520 $return = \$result ;
521 }
522  
523 object: <skip:''> /\$/ /[a-zA-Z]\w*/
524  
525 function: <skip:''> '&' /\w+/
526  
527 END_OF_GRAMMAR
528  
529 59 __PACKAGE__->meta->make_immutable;
530  
531 59 1;
532  
533 # ABSTRACT: Provides configuration value computation
534  
535 __END__
« blib/lib/Config/Model/Instance.pm ↑ Summary blib/lib/Config/Model/ObjTreeScanner.pm »