Scoped.pm

filter: Statementstmt 97.9% 283 / 289 Branchbran 71.0% 135 / 190 Conditioncond 65.0% 52 / 80 MC/DCmcdc 38.7% 19 / 49 Subroutinesub 100% 32 / 32 Podpod 100% 10 / 10 totaltotal 81.6% 531 / 650 CC 127 SCAR 22.7
CC
127
Cov
84%
new
23.1
retrieve_cache
14.2
permissions_validate
12.0
CRAP 9.7
↑ Summary Scoped/Error.pm »
1 # Copyright (c) 2004-2012 by Karl Gaissmaier, Ulm University, Germany
2 #
3 # This library is free software; you can redistribute it and/or modify
4 # it under the same terms as Perl itself.
5 #
6  
7  
8 # for documentation see Config::Scoped.pod
9  
10 package Config::Scoped;
11  
12 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 18 ▶use Storable qw(dclone lock_nstore lock_retrieve);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use Config::Scoped::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use base 'Parse::RecDescent';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 18 our $VERSION = '0.22';
25  
26 18 my $grammar;
27 {
28 18 local $/;
29 18 $grammar = <DATA>;
30 18 close DATA;
31 }
32  
33 18 my @state_hashes = qw(config params macros warnings includes);
34  
35 51 ▶sub new {
Subroutine new: called CC=22 SCAR=31.4
Pod documented
36 51 my $class = shift;
37  
38 51 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 50 51
39 -text => Carp::shortmess("odd number of arguments,") )
40 if @_ % 2;
41  
42 50 my %args = @_;
43  
44 ##############################################
45 # Delayed compilation of grammar in method parse()
46 #
47 50 my $empty_grammar = '';
48 50 $args{compiled} = undef;
49  
50 ##############################################
51 # create the parser object, delayed grammar
52 #
53 50 ▶ my $thisparser = $class->SUPER::new($empty_grammar)
Branch: 1 branch
BranchTrueFalseTotal
my $thisparser = $class->SUPER::new($empty_grammar) or "Config::Scoped::Error"->throw("-text", "Can't create a '${class}' parser,") 50 0 50
54 or Config::Scoped::Error->throw(
55 -text => "Can't create a '$class' parser," );
56  
57 ##############################################
58 # store the args in the P::RD object below 'local'
59 # don't use deep copy since we use always one and
60 # only one global config hash
61 #
62 50 $thisparser->{local} = {%args};
63  
64 # frequent typos, be polite
65 50 ▶ $thisparser->{local}{warnings} ||= $thisparser->{local}{warning};
Condition66%2/3
$thisparser->{"local"}{"warnings"} ||= $thisparser->{"local"}{"warning"}
l!l&&r!l&&!r
15035
MC/DC50%1/2
$thisparser->{"local"}{"warnings"} ||= $thisparser->{"local"}{"warning"}
$thisparser->{"local"}{"warnings"} $thisparser->{"local"}{"warning"}
Truth table66%2/3
A ||= B
A= $thisparser->{"local"}{"warnings"}B= $thisparser->{"local"}{"warning"}
ABresult
1X1
011
000
66 50 ▶ $thisparser->{local}{lc} ||= $thisparser->{local}{lowercase};
Condition66%2/3
$thisparser->{"local"}{"lc"} ||= $thisparser->{"local"}{"lowercase"}
l!l&&r!l&&!r
2048
MC/DC50%1/2
$thisparser->{"local"}{"lc"} ||= $thisparser->{"local"}{"lowercase"}
$thisparser->{"local"}{"lc"} $thisparser->{"local"}{"lowercase"}
Truth table66%2/3
A ||= B
A= $thisparser->{"local"}{"lc"}B= $thisparser->{"local"}{"lowercase"}
ABresult
1X1
011
000
67 50 ▶ $thisparser->{local}{safe} ||= $thisparser->{local}{Safe};
Condition66%2/3
$thisparser->{"local"}{"safe"} ||= $thisparser->{"local"}{"Safe"}
l!l&&r!l&&!r
7043
MC/DC50%1/2
$thisparser->{"local"}{"safe"} ||= $thisparser->{"local"}{"Safe"}
$thisparser->{"local"}{"safe"} $thisparser->{"local"}{"Safe"}
Truth table66%2/3
A ||= B
A= $thisparser->{"local"}{"safe"}B= $thisparser->{"local"}{"Safe"}
ABresult
1X1
011
000
68 50 ▶ $thisparser->{local}{file} ||= $thisparser->{local}{File};
Condition66%2/3
$thisparser->{"local"}{"file"} ||= $thisparser->{"local"}{"File"}
l!l&&r!l&&!r
14036
MC/DC50%1/2
$thisparser->{"local"}{"file"} ||= $thisparser->{"local"}{"File"}
$thisparser->{"local"}{"file"} $thisparser->{"local"}{"File"}
Truth table66%2/3
A ||= B
A= $thisparser->{"local"}{"file"}B= $thisparser->{"local"}{"File"}
ABresult
1X1
011
000
69  
70 ##############################################
71 # validate and munge the 'file' param
72 #
73 # a cfg_file isn't necessary, the parse method can be feeded
74 # with a plain text string
75 50 ▶ if ( my $cfg_file = $thisparser->{local}{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cfg_file = $thisparser->{'local'}{'file'}) { } 14 36 50
76  
77 14 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
if ref $cfg_file 1 13 14
78 -text => Carp::shortmess("can't use filehandle as cfg file") )
79 if ref $cfg_file;
80  
81 # retrieve the dir part, later on needed for relative include files
82 13 ▶ my ( undef, $cfg_dir ) = fileparse($cfg_file)
Branch: 1 branch
BranchTrueFalseTotal
my(undef, $cfg_dir) = fileparse($cfg_file) or "Config::Scoped::Error"->throw("-text", "error in fileparse", "-file", $thisparser->_get_file(%args), "-line", $thisparser->_get_line(%args)) 13 0 13
83 or Config::Scoped::Error->throw(
84 -text => "error in fileparse",
85 -file => $thisparser->_get_file(%args),
86 -line => $thisparser->_get_line(%args),
87 );
88  
89 13 ▶ $cfg_file = File::Spec->rel2abs($cfg_file)
Branch: 1 branch
BranchTrueFalseTotal
$cfg_file = "File::Spec"->rel2abs($cfg_file) or "Config::Scoped::Error"->throw("-text", "error in rel2abs", "-file", $thisparser->_get_file(%args), "-line", $thisparser->_get_line(%args)) 13 0 13
90 or Config::Scoped::Error->throw(
91 -text => "error in rel2abs",
92 -file => $thisparser->_get_file(%args),
93 -line => $thisparser->_get_line(%args),
94 );
95  
96 13 $thisparser->{local}{cfg_file} = $cfg_file;
97 13 $thisparser->{local}{cfg_dir} = $cfg_dir;
98 }
99  
100 else {
101  
102 # no cfg_file defined, use _STRING and cwd
103 36 $thisparser->{local}{cfg_file} = '_STRING';
104 $thisparser->{local}{cfg_dir} =
105 36 File::Spec->rel2abs( File::Spec->curdir );
106 }
107  
108 ##############################################
109 # check for warnings
110 #
111 # set the default to all on
112 $thisparser->{local}{warnings} = { all => 'on' }
113 49 ▶ unless $thisparser->{local}{warnings};
Branch: 1 branch
BranchTrueFalseTotal
unless $thisparser->{"local"}{"warnings"} 34 15 49
114  
115 # allow the simple form: 'warnings' => 'on/off'
116 49 ▶ if ( ref $thisparser->{local}{warnings} ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thisparser->{"local"}{"warnings"} ne "HASH") 2 47 49
117 $thisparser->{local}{warnings} = { all => 'on' }
118 2 ▶ if $thisparser->{local}{warnings} =~ m/on/i;
Branch: 1 branch
BranchTrueFalseTotal
if $thisparser->{"local"}{"warnings"} =~ /on/i 0 2 2
119 $thisparser->{local}{warnings} = { all => 'off' }
120 2 ▶ if $thisparser->{local}{warnings} =~ m/off/i;
Branch: 1 branch
BranchTrueFalseTotal
if $thisparser->{"local"}{"warnings"} =~ /off/i 2 0 2
121 }
122  
123 # store the warnings in a normalized form
124 49 foreach my $name ( keys %{ $thisparser->{local}{warnings} } ) {
125 52 my $switch = delete $thisparser->{local}{warnings}{$name};
126 52 $thisparser->_set_warnings(
127 name => $name,
128 switch => $switch,
129 );
130 }
131  
132 ##############################################
133 # preset the state hashes
134 #
135 # use empty state_hashes if not defined
136 49 foreach my $hash_name (@state_hashes) {
137 241 ▶ $thisparser->{local}{$hash_name} ||= {};
Condition100%2/2
$thisparser->{"local"}{$hash_name} ||= {}
l!l
73168
MC/DC100%1/1
$thisparser->{"local"}{$hash_name} ||= {}
$thisparser->{"local"}{$hash_name}
Truth table100%2/2
A
A= $thisparser->{"local"}{$hash_name}
Aresult
11
00
138  
139 # be defensive
140 Config::Scoped::Error->throw(
141 -text => Carp::shortmess("$hash_name is no hash ref") )
142 241 ▶ unless ref $thisparser->{local}{$hash_name} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $thisparser->{"local"}{$hash_name} eq "HASH" 1 240 241
143 }
144  
145 # install/create Safe compartment for perl_code
146 48 my $compartment = $thisparser->{local}{safe};
147 48 ▶ if ( $thisparser->{local}{safe} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thisparser->{'local'}{'safe'}) { } 7 41 48
148 Config::Scoped::Error->throw(
149 -text => Carp::shortmess("can't find method 'reval' on compartment")
150 )
151 7 ▶ unless UNIVERSAL::can( $thisparser->{local}{safe}, 'reval' );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::can($thisparser->{"local"}{"safe"}, "reval") 1 6 7
152 }
153 else {
154 41 ▶ $thisparser->{local}{safe} = Safe->new
Branch: 1 branch
BranchTrueFalseTotal
$thisparser->{"local"}{"safe"} = "Safe"->new or "Config::Scoped::Error"->throw("-text", "can't create a Safe compartment!") 41 0 41
155 or Config::Scoped::Error->throw(
156 -text => "can't create a Safe compartment!" );
157 }
158  
159 47 return $thisparser;
160 }
161  
162 46 ▶sub parse {
Subroutine parse: called CC=10 SCAR=23.0
Pod documented
163 46 my $thisparser = shift;
164  
165 46 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 45 46
166 -text => Carp::shortmess("odd number of arguments,") )
167 if @_ % 2;
168  
169 45 ▶ unless (defined $thisparser->{local}{compiled} ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $thisparser->{"local"}{"compiled"}) 44 1 45
170 44 $thisparser->Replace($grammar);
171 44 $thisparser->{local}{compiled} = 1;
172 }
173  
174 45 my %args = @_;
175  
176 45 my $cfg_text = $args{text};
177  
178 45 ▶ unless ( defined $cfg_text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg_text) 14 31 45
179 my $cfg_file = $thisparser->{local}{cfg_file}
180 14 ▶ or Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
my $cfg_file = $thisparser->{"local"}{"cfg_file"} or "Config::Scoped::Error"->throw("-text", Carp::shortmess("no cfg_file defined"), "-file", $thisparser->_get_file(%args), "-line", $thisparser->_get_line(%args)) 14 0 14
181 -text => Carp::shortmess("no cfg_file defined"),
182 -file => $thisparser->_get_file(%args),
183 -line => $thisparser->_get_line(%args),
184 );
185  
186 14 ▶ Config::Scoped::Error->throw( -text => "no text to parse defined" )
Branch: 1 branch
BranchTrueFalseTotal
if $cfg_file eq "_STRING" 1 13 14
187 if $cfg_file eq '_STRING';
188  
189 # slurp the cfg file
190 13 $cfg_text = $thisparser->_get_cfg_text( %args, file => $cfg_file );
191  
192 12 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg_text 1 11 12
193 -file => $thisparser->_get_file(%args),
194 -line => $thisparser->_get_line(%args),
195 -text => "'$cfg_file' is empty"
196 )
197 unless $cfg_text;
198  
199 # calculate the message digest and remember this cfg text in includes
200 11 my $digest = md5_base64($cfg_text);
201  
202 Config::Scoped::Error->throw(
203 -file => $thisparser->_get_file(%args),
204 -line => $thisparser->_get_line(%args),
205 -text => "include loop for '$cfg_file' encountered",
206 )
207 11 ▶ if $thisparser->{local}{includes}{$digest};
Branch: 1 branch
BranchTrueFalseTotal
if $thisparser->{"local"}{"includes"}{$digest} 1 10 11
208  
209 10 $thisparser->{local}{includes}{$digest} = $cfg_file;
210 }
211  
212 # call the P::RD with the startrule of the grammar
213 41 $thisparser->config($cfg_text);
214  
215 ##############################################
216 # no declarations but parameters in scope?
217 #
218 # copy them to an automatically generated _GLOBAL hash
219 # first use some shortcuts
220 37 my $params = $thisparser->{local}{params};
221 37 my $config = $thisparser->{local}{config};
222  
223 # all $config keys other than _GLOBAL are real declarations
224 37 my @declarations = grep !/^_GLOBAL$/, keys %$config;
225  
226 # no declarations but parameters in global scope
227 37 ▶ if ( !@declarations && %$params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @declarations and %$params) { } 13 24 37
Condition100%3/3
not @declarations and %$params
!ll&&!rl&&r
22213
MC/DC100%2/2
not @declarations and %$params
not @declarations %$params
Truth table100%3/3
not(A) and B
A= not @declarationsB= %$params
ABresult
0X0
100
111
228  
229 # the overall parent scope overrides scopes from include files
230 13 $config->{_GLOBAL} = dclone $params;
231 }
232 else {
233  
234 # perhaps a prior parse for an include file filled this slot
235 24 delete $config->{_GLOBAL};
236 }
237  
238 37 return $thisparser->{local}{config};
239 }
240  
241 224 ▶sub warnings_on {
Subroutine warnings_on: called CC=11 SCAR=24.1
Pod documented
242 224 my $thisparser = shift;
243  
244 224 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 224 224
245 -text => Carp::shortmess("odd number of arguments,") )
246 if @_ % 2;
247  
248 224 my %args = @_;
249  
250 Config::Scoped::Error->throw(
251 -text => Carp::shortmess("missing parameters") )
252 224 ▶ unless defined $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"name"} 0 224 224
253  
254 224 my $name = $args{name};
255 224 my $warnings = $thisparser->{local}{warnings};
256  
257 224 $name = $thisparser->_trim_warnings($name);
258  
259 224 ▶ return undef if exists $warnings->{$name} && $warnings->{$name} eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if exists $warnings->{$name} and $warnings->{$name} eq "off" 81 143 224
Condition100%3/3
exists $warnings->{$name} and $warnings->{$name} eq "off"
!ll&&!rl&&r
140381
MC/DC100%2/2
exists $warnings->{$name} and $warnings->{$name} eq "off"
exists $warnings->{$name} $warnings->{$name} eq "off"
Truth table100%3/3
A and B
A= exists $warnings->{$name}B= $warnings->{$name} eq "off"
ABresult
0X0
100
111
260 143 ▶ return 1 if exists $warnings->{$name} && $warnings->{$name} eq 'on';
Branch: 1 branch
BranchTrueFalseTotal
if exists $warnings->{$name} and $warnings->{$name} eq "on" 3 140 143
Condition66%2/3
exists $warnings->{$name} and $warnings->{$name} eq "on"
!ll&&!rl&&r
14003
MC/DC50%1/2
exists $warnings->{$name} and $warnings->{$name} eq "on"
exists $warnings->{$name} $warnings->{$name} eq "on"
Truth table66%2/3
A and B
A= exists $warnings->{$name}B= $warnings->{$name} eq "on"
ABresult
0X0
100
111
261  
262 # use 'all'
263 140 ▶ return undef if exists $warnings->{all} && $warnings->{all} eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if exists $warnings->{"all"} and $warnings->{"all"} eq "off" 6 134 140
Condition100%3/3
exists $warnings->{"all"} and $warnings->{"all"} eq "off"
!ll&&!rl&&r
291056
MC/DC100%2/2
exists $warnings->{"all"} and $warnings->{"all"} eq "off"
exists $warnings->{"all"} $warnings->{"all"} eq "off"
Truth table100%3/3
A and B
A= exists $warnings->{"all"}B= $warnings->{"all"} eq "off"
ABresult
0X0
100
111
264 134 ▶ return 1 if exists $warnings->{all} && $warnings->{all} eq 'on';
Branch: 1 branch
BranchTrueFalseTotal
if exists $warnings->{"all"} and $warnings->{"all"} eq "on" 105 29 134
Condition66%2/3
exists $warnings->{"all"} and $warnings->{"all"} eq "on"
!ll&&!rl&&r
290105
MC/DC50%1/2
exists $warnings->{"all"} and $warnings->{"all"} eq "on"
exists $warnings->{"all"} $warnings->{"all"} eq "on"
Truth table66%2/3
A and B
A= exists $warnings->{"all"}B= $warnings->{"all"} eq "on"
ABresult
0X0
100
111
265  
266 # hmm, name and all not defined, defaults to on
267 29 return 1;
268 }
269  
270 64 ▶sub set_warnings {
Subroutine set_warnings: called CC=5 SCAR=16.1
Pod documented
271 64 my $thisparser = shift;
272  
273 64 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 64 64
274 -text => Carp::shortmess("odd number of arguments,") )
275 if @_ % 2;
276  
277 64 my %args = @_;
278  
279 Config::Scoped::Error->throw(
280 -text => Carp::shortmess("no warnings switch (on/off) defined") )
281 64 ▶ unless defined $args{switch};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"switch"} 0 64 64
282  
283 64 my $warnings = $thisparser->{local}{warnings};
284 64 ▶ my $name = $args{name} || 'all';
Condition100%2/2
$args{'name'} || 'all'
l!l
631
MC/DC100%1/1
$args{'name'} || 'all'
$args{'name'}
Truth table100%2/2
A
A= $args{'name'}
Aresult
11
00
285 64 my $switch = $args{switch};
286  
287 64 $name = $thisparser->_trim_warnings($name);
288  
289 # trim the switch, convert to lowercase
290 64 $switch = lc($switch);
291  
292 64 ▶ if ( $name eq 'all' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq 'all') { } 38 26 64
293  
294 # reset the hash
295 38 %{$warnings} = ();
296 38 $warnings->{all} = $args{switch};
297 }
298 else {
299  
300 # override the key, key is 'macro', 'declaration', 'parameter', ...
301 26 $warnings->{$name} = $args{switch};
302 }
303  
304 64 return 1;
305 }
306  
307 # just a wrapper for the same method without leading _
308 # this method is called in the grammar file whereas the set_warnings
309 # may be overriden by the application
310 61 ▶sub _set_warnings {
Subroutine _set_warnings: called CC=1 SCAR=0.0
311 61 my $thisparser = shift;
312 61 $thisparser->set_warnings(@_);
313 }
314  
315 # shortcuts allowed, less spelling errors
316 288 ▶sub _trim_warnings {
Subroutine _trim_warnings: called CC=6 SCAR=17.9
317 288 my ( $thisparser, $name ) = @_;
318  
319 # trim the names
320 288 ▶ return 'declaration' if $name =~ /^decl/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^decl/i 35 253 288
321 253 ▶ return 'parameter' if $name =~ /^param/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^param/i 160 93 253
322 93 ▶ return 'macro' if $name =~ /^mac/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^mac/i 11 82 93
323 82 ▶ return 'permissions' if $name =~ /^perm/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^perm/i 34 48 82
324 48 ▶ return 'digests' if $name =~ /^dig/i;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^dig/i 2 46 48
325 46 return $name;
326 }
327  
328 1 ▶sub store_cache {
Subroutine store_cache: called CC=7 SCAR=20.4
Pod documented
329 1 my $thisparser = shift;
330  
331 1 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
332 -text => Carp::shortmess("odd number of arguments,") )
333 if @_ % 2;
334  
335 1 my %args = @_;
336  
337 1 my $cache_file = $args{cache};
338  
339 1 ▶ unless ($cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache_file) 1 0 1
340 my $cfg_file = $thisparser->{local}{cfg_file}
341 1 ▶ or Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
my $cfg_file = $thisparser->{"local"}{"cfg_file"} or "Config::Scoped::Error"->throw("-text", Carp::shortmess("no cache_file and no cfg_file defined")) 1 0 1
342 -text => Carp::shortmess("no cache_file and no cfg_file defined") );
343  
344 1 ▶ Config::Scoped::Error->throw( -text =>
Branch: 1 branch
BranchTrueFalseTotal
if $cfg_file eq "_STRING" 0 1 1
345 Carp::shortmess("parameter 'cache' needed for parsed strings") )
346 if $cfg_file eq '_STRING';
347  
348 1 $cache_file = $cfg_file . '.dump';
349 }
350  
351 my $cfg_hash = {
352 includes => $thisparser->{local}{includes},
353 config => $thisparser->{local}{config},
354 1 };
355  
356 1 my $result = eval { lock_nstore( $cfg_hash, $cache_file ); };
357  
358 1 ▶ Config::Scoped::Error->throw( -text => Carp::shortmess($@) ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
359  
360 1 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 1 1
361 -text => Carp::shortmess("can't store the cfg hash to '$cache_file'") )
362 unless $result;
363 }
364  
365 1 ▶sub retrieve_cache {
Subroutine retrieve_cache: called CC=12 SCAR=26.6
Pod documented
366 1 my $thisparser = shift;
367  
368 1 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
369 -text => Carp::shortmess("odd number of arguments,") )
370 if @_ % 2;
371  
372 1 my %args = @_;
373  
374 1 my $cache_file = $args{cache};
375 1 $args{parent_file} = $cache_file; # for better error messages
376  
377 1 ▶ unless ($cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache_file) 1 0 1
378 my $cfg_file = $thisparser->{local}{cfg_file}
379 1 ▶ or Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
my $cfg_file = $thisparser->{"local"}{"cfg_file"} or "Config::Scoped::Error"->throw("-text", Carp::shortmess("no cache_file and no cfg_file defined")) 1 0 1
380 -text => Carp::shortmess("no cache_file and no cfg_file defined") );
381  
382 1 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $cfg_file eq "_STRING" 0 1 1
383 -text => Carp::shortmess("cache not supported for strings") )
384 if $cfg_file eq '_STRING';
385  
386 1 $cache_file = $cfg_file . '.dump';
387 }
388  
389 Config::Scoped::Error::IO->throw(
390 1 ▶ -text => Carp::shortmess("Can't read the cfg_cache '$cache_file'") )
Branch: 1 branch
BranchTrueFalseTotal
unless -r $cache_file 0 1 1
391 unless -r $cache_file;
392  
393 # check the permission and ownership, I know, it's no handle and of
394 # restricted usage
395 1 ▶ Config::Scoped::Error::Validate::Permissions->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $thisparser->permissions_validate(%args, "file", $cache_file) 0 1 1
396 -text => Carp::shortmess(
397 "permissions_validate returned false for cache_file '$cache_file'")
398 )
399 unless $thisparser->permissions_validate( %args, file => $cache_file );
400  
401 1 my $cfg_cache = eval { lock_retrieve($cache_file); };
402  
403 1 ▶ Config::Scoped::Error->throw( -text => Carp::shortmess($@) ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
404  
405 1 ▶ Config::Scoped::Error->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg_cache 0 1 1
406 -text => Carp::shortmess( "cfg cache is empty", ) )
407 unless $cfg_cache;
408  
409 # warnings for digests enabled?
410 return $cfg_cache->{config}
411 1 ▶ unless $thisparser->warnings_on( %args, name => 'digests', );
Branch: 1 branch
BranchTrueFalseTotal
unless $thisparser->warnings_on(%args, "name", "digests") 0 1 1
412  
413 # check the include digests for modification
414 1 ▶ while ( my ( $digest, $file ) = each %{ $cfg_cache->{includes} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$cfg_cache{"includes"};}) 2 1 3
415  
416 2 my $text = $thisparser->_get_cfg_text( %args, file => $file, );
417  
418 2 ▶ if ( $digest ne md5_base64($text) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digest ne md5_base64($text)) 0 2 2
419 0 Config::Scoped::Error->throw(
420 -text => Carp::shortmess(
421 "'$file' modified, can't use the cache '$cache_file',")
422 );
423 }
424 }
425  
426 1 return $cfg_cache->{config};
427 }
428  
429 # _include
430 #
431 # this method is called as an action in the INCLUDE grammar rule
432 # the current localized $thisparser->{local}... parameters are used and adjusted
433 # and a new P::RD parser with the same grammar is created and started
434 # for the include file.
435 # After that the parse in the parent cfg file is continued.
436  
437 # We don't change the $text and don't resync the linecounter in P::RD, since
438 # this would result in awfully wrong line numbers in error messages and
439 # we would still have no hint in which include file the error happened.
440 #
441 # The current scope, macro and warnings hash is used during include file parsing
442 # so the include file can use (or overwrite) the current parse state.
443 #
444 # The changed state during the include file parse is propagated to the
445 # parent parser state (except warnings). If this import isn't intended
446 # put the include # in a own block: { %include filename; }
447 #
448  
449 6 ▶sub _include {
Subroutine _include: called CC=5 SCAR=16.6
450 6 my $thisparser = shift;
451 6 my %args = @_;
452  
453 Config::Scoped::Error->throw(
454 -file => $thisparser->_get_file(%args),
455 -line => $thisparser->_get_line(%args),
456 -text => Carp::shortmess("missing parameters"),
457 )
458 6 ▶ unless defined $args{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"file"} 0 6 6
459  
460 6 my $include_file = $args{file};
461 6 my $parent_cfg_file = $thisparser->{local}{cfg_file};
462 6 my $parent_cfg_dir = $thisparser->{local}{cfg_dir};
463  
464 # absolute path? else concat with parent cfg dir
465 6 ▶ unless ( File::Spec->file_name_is_absolute($include_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($include_file)) 6 0 6
466 6 ▶ $include_file = File::Spec->catfile( $parent_cfg_dir, $include_file )
Branch: 1 branch
BranchTrueFalseTotal
$include_file = "File::Spec"->catfile($parent_cfg_dir, $include_file) or "Config::Scoped::Error"->throw("-file", $parent_cfg_file, "-line", $thisparser->_get_line(%args), "-text", "error in catfile for '${include_file}'") 6 0 6
467 or Config::Scoped::Error->throw(
468 -file => $parent_cfg_file,
469 -line => $thisparser->_get_line(%args),
470 -text => "error in catfile for '$include_file'"
471 );
472 }
473  
474 # Create a new parser for this include file parsing.
475 # Use the current parser states (perhaps already localized
476 # in a grammar { action }), and change some args for the new
477 # include parser creation.
478 #
479 my $clone_parser =
480 ( ref $thisparser )
481 6 ▶ ->new( %{ $thisparser->{local} }, file => $include_file )
Branch: 1 branch
BranchTrueFalseTotal
my $clone_parser = ref($thisparser)->new(%{$$thisparser{"local"};}, "file", $include_file) or "Config::Scoped::Error"->throw("-file", $parent_cfg_file, "-line", $thisparser->_get_line(%args), "-text", "Internal error: Can't create a clone parser") 6 0 6
482  
483 or Config::Scoped::Error->throw(
484 -file => $parent_cfg_file,
485 -line => $thisparser->_get_line(%args),
486 -text => "Internal error: Can't create a clone parser"
487 );
488  
489 # parse the include file (recursively) and return to the parent
490 # cfg parse. Loop includes are detected (via md5) and throws an exception.
491 6 return $clone_parser->parse(
492 parent_file => $parent_cfg_file, # for better error reporting
493 );
494 }
495  
496 # this method is called as an action in the MACRO rule in order
497 # to store the macro in the macros hash
498 10 ▶sub _store_macro {
Subroutine _store_macro: called CC=3 SCAR=11.8
499 10 my $thisparser = shift;
500 10 my %args = @_;
501  
502 Config::Scoped::Error->throw(
503 -text => Carp::shortmess("missing parameters") )
504 10 ▶ unless ( defined $args{name} && defined $args{value} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"name"} and defined $args{"value"} 0 10 10
Condition33%1/3
defined $args{"name"} and defined $args{"value"}
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $args{"name"} and defined $args{"value"}
defined $args{"name"} defined $args{"value"}
Truth table33%1/3
A and B
A= defined $args{"name"}B= defined $args{"value"}
ABresult
0X0
100
111
505  
506 # macro validation, may be overwritten by the application
507 10 my $valid_macro = $thisparser->macro_validate(%args);
508  
509 10 return $thisparser->{local}{macros}{ $args{name} } = $valid_macro;
510 }
511  
512 10 ▶sub macro_validate {
Subroutine macro_validate: called CC=5 SCAR=16.7
Pod documented
513 10 my $thisparser = shift;
514 10 my %args = @_;
515  
516 Config::Scoped::Error->throw(
517 -text => Carp::shortmess("missing parameters") )
518 10 ▶ unless ( defined $args{name} && defined $args{value} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"name"} and defined $args{"value"} 0 10 10
Condition33%1/3
defined $args{"name"} and defined $args{"value"}
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $args{"name"} and defined $args{"value"}
defined $args{"name"} defined $args{"value"}
Truth table33%1/3
A and B
A= defined $args{"name"}B= defined $args{"value"}
ABresult
0X0
100
111
519  
520 10 my $name = $args{name};
521 10 my $value = $args{value};
522  
523 # warnings for macros enabled?
524 10 ▶ if ( $thisparser->warnings_on( name => 'macro', ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thisparser->warnings_on("name", "macro")) 9 1 10
525 Config::Scoped::Error::Validate::Macro->throw(
526 -file => $thisparser->_get_file(%args),
527 -line => $thisparser->_get_line(%args),
528 -text => "macro redefinition for '$name"
529 )
530 9 ▶ if exists $thisparser->{local}{macros}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $thisparser->{"local"}{"macros"}{$name} 0 9 9
531 }
532  
533 # return unchanged, subclass methods may do it different
534 10 return $value;
535 }
536  
537 # macro expansion
538 93 ▶sub _expand_macro {
Subroutine _expand_macro: called CC=4 SCAR=14.0
539 93 my $thisparser = shift;
540 93 my %args = @_;
541  
542 Config::Scoped::Error->throw(
543 -text => Carp::shortmess("missing parameters") )
544 93 ▶ unless defined $args{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"value"} 0 93 93
545  
546 93 my $value = $args{value};
547  
548 93 ▶ while ( my ( $macro, $defn ) = each %{ $thisparser->{local}{macros} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$thisparser{"local"}{"macros"};}) 29 93 122
549 29 $value =~ s/\Q$macro\E/$defn/g;
550 }
551  
552 # a P::RD rule can't return undef, then the rule will fail
553 93 ▶ return defined $value ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 93 0 93
554 }
555  
556 # parameter storage, called as action from within the grammar
557 152 ▶sub _store_parameter {
Subroutine _store_parameter: called CC=4 SCAR=14.3
558 152 my $thisparser = shift;
559 152 my %args = @_;
560  
561 Config::Scoped::Error->throw(
562 -text => Carp::shortmess("missing parameters") )
563 152 ▶ unless ( defined $args{value} && defined $args{name} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"value"} and defined $args{"name"} 0 152 152
Condition33%1/3
defined $args{"value"} and defined $args{"name"}
!ll&&!rl&&r
00152
MC/DC0%0/2
defined $args{"value"} and defined $args{"name"}
defined $args{"value"} defined $args{"name"}
Truth table33%1/3
A and B
A= defined $args{"value"}B= defined $args{"name"}
ABresult
0X0
100
111
564  
565 152 ▶ $args{name} = lc( $args{name} ) if $thisparser->{local}{lc};
Branch: 1 branch
BranchTrueFalseTotal
if $thisparser->{"local"}{"lc"} 2 150 152
566  
567 # parameter validation, may be overwritten by the application
568 152 my $valid_value = $thisparser->parameter_validate(%args);
569  
570 # store the return value in the params hash
571 151 return $thisparser->{local}{params}{ $args{name} } = $valid_value;
572 }
573  
574 152 ▶sub parameter_validate {
Subroutine parameter_validate: called CC=5 SCAR=16.5
Pod documented
575 152 my $thisparser = shift;
576 152 my %args = @_;
577  
578 Config::Scoped::Error->throw(
579 -text => Carp::shortmess("missing parameters") )
580 152 ▶ unless ( defined $args{value} && defined $args{name} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"value"} and defined $args{"name"} 0 152 152
Condition33%1/3
defined $args{"value"} and defined $args{"name"}
!ll&&!rl&&r
00152
MC/DC0%0/2
defined $args{"value"} and defined $args{"name"}
defined $args{"value"} defined $args{"name"}
Truth table33%1/3
A and B
A= defined $args{"value"}B= defined $args{"name"}
ABresult
0X0
100
111
581  
582 # warnings for parameters enabled?
583 152 ▶ if ( $thisparser->warnings_on( name => 'parameter', ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thisparser->warnings_on("name", "parameter")) 89 63 152
584 Config::Scoped::Error::Validate::Parameter->throw(
585 -file => $thisparser->_get_file(%args),
586 -line => $thisparser->_get_line(%args),
587 -text => "parameter redefinition for '$args{name}'"
588 )
589 89 ▶ if exists $thisparser->{local}{params}{ $args{name} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $thisparser->{"local"}{"params"}{$args{"name"}} 1 88 89
590 }
591  
592 # return unchanged, subclass methods may do it different
593 151 return $args{value};
594 }
595  
596 # declaration storage, called as action from within the grammar
597 34 ▶sub _store_declaration {
Subroutine _store_declaration: called CC=6 SCAR=18.0
598 34 my $thisparser = shift;
599 34 my %args = @_;
600  
601 Config::Scoped::Error->throw(
602 -text => Carp::shortmess("missing parameters") )
603 34 ▶ unless ( defined $args{name} && defined $args{value} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"name"} and defined $args{"value"} 0 34 34
Condition33%1/3
defined $args{"name"} and defined $args{"value"}
!ll&&!rl&&r
0034
MC/DC0%0/2
defined $args{"name"} and defined $args{"value"}
defined $args{"name"} defined $args{"value"}
Truth table33%1/3
A and B
A= defined $args{"name"}B= defined $args{"value"}
ABresult
0X0
100
111
604  
605 {
606 34 local $_;
607 3 map { $_ = lc($_) } @{ $args{name} }
608 34 ▶ if $thisparser->{local}{lc};
Branch: 1 branch
BranchTrueFalseTotal
if $thisparser->{"local"}{"lc"} 1 33 34
609 }
610  
611 # convert declaration: foo bar ... baz { parameters }
612 # to the data structure
613 # $config->{foo}{bar}...{baz} = { parameters };
614 34 my $tail = $thisparser->{local}{config};
615  
616 # walking down the street ...
617 34 foreach my $name ( @{ $args{name} } ) {
618 44 ▶ $tail->{$name} = {} unless exists $tail->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tail->{$name} 39 5 44
619 44 $tail = $tail->{$name};
620 }
621  
622 # now we have baz = {}
623  
624 # application validation
625 34 my $valid_value = $thisparser->declaration_validate( %args, tail => $tail );
626  
627 # store the current scope in the last $config->{foo}...{baz} = $params
628 # use deep copy to break dependencies when config parameters
629 # get's changed in the application in different declarations
630 34 return %$tail = %{ dclone( $args{value} ) };
631 }
632  
633 34 ▶sub declaration_validate {
Subroutine declaration_validate: called CC=5 SCAR=17.3
Pod documented
634 34 my $thisparser = shift;
635 34 my %args = @_;
636  
637 Config::Scoped::Error->throw(
638 -text => Carp::shortmess("missing parameters") )
639 34 ▶ unless ( defined $args{name} && defined $args{value} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"name"} and defined $args{"value"} 0 34 34
Condition33%1/3
defined $args{"name"} and defined $args{"value"}
!ll&&!rl&&r
0034
MC/DC0%0/2
defined $args{"name"} and defined $args{"value"}
defined $args{"name"} defined $args{"value"}
Truth table33%1/3
A and B
A= defined $args{"name"}B= defined $args{"value"}
ABresult
0X0
100
111
640  
641 # warnings for declarations enabled and 'tail' already set?
642 34 ▶ if ( $thisparser->warnings_on( name => 'declaration', ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thisparser->warnings_on("name", "declaration")) 33 1 34
643 Config::Scoped::Error::Validate::Declaration->throw(
644 -file => $thisparser->_get_file(%args),
645 -line => $thisparser->_get_line(%args),
646 0 -text => "declaration redefinition for '@{$args{name}}'"
647 )
648 33 ▶ if %{ $args{tail} };
Branch: 1 branch
BranchTrueFalseTotal
if %{$args{"tail"};} 0 33 33
649 }
650  
651 # return unchanged, subclass methods may do it different
652 34 return $args{value};
653 }
654  
655 16 ▶sub permissions_validate {
Subroutine permissions_validate: called CC=9 SCAR=24.8
Pod documented
656 16 my $thisparser = shift;
657 16 my %args = @_;
658  
659 Config::Scoped::Error->throw(
660 -text => Carp::shortmess("missing parameters") )
661 16 ▶ unless ( defined $args{handle} || defined $args{file} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"handle"} or defined $args{"file"} 0 16 16
Condition66%2/3
defined $args{"handle"} or defined $args{"file"}
l!l&&r!l&&!r
1510
MC/DC0%0/2
defined $args{"handle"} or defined $args{"file"}
defined $args{"handle"} defined $args{"file"}
Truth table66%2/3
A or B
A= defined $args{"handle"}B= defined $args{"file"}
ABresult
1X1
011
000
662  
663 16 my $warnings = $thisparser->{local}{warnings};
664  
665 # warnings for files enabled?
666 16 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
unless $thisparser->warnings_on("name", "permissions", "warnings", $warnings) 15 1 16
667 unless $thisparser->warnings_on(
668 name => 'permissions',
669 warnings => $warnings,
670 );
671  
672 1 ▶ my $fh = $args{handle} || $args{file};
Condition33%1/3
$args{'handle'} || $args{'file'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'handle'} || $args{'file'}
$args{'handle'} $args{'file'}
Truth table33%1/3
A || B
A= $args{'handle'}B= $args{'file'}
ABresult
1X1
011
000
673  
674 # mysteriously vaporized
675 1 ▶ Config::Scoped::Error::IO->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless stat $fh 0 1 1
676 -file => $thisparser->_get_file(%args),
677 -line => $thisparser->_get_line(%args),
678 -text => "'$args{file}' can't stat cfg file/handle: $!"
679 )
680 unless stat $fh;
681  
682 1 my ( $dev, $ino, $mode, $nlink, $uid, $gid ) = stat(_);
683  
684 # owner is not root and not real uid
685 1 ▶ Config::Scoped::Error::Validate::Permissions->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $uid != 0 and $uid != $< 0 1 1
Condition33%1/3
$uid != 0 and $uid != $<
!ll&&!rl&&r
010
MC/DC0%0/2
$uid != 0 and $uid != $<
$uid != 0 $uid != $<
Truth table33%1/3
A and B
A= $uid != 0B= $uid != $<
ABresult
0X0
100
111
686 -file => $thisparser->_get_file(%args),
687 -line => $thisparser->_get_line(%args),
688 -text => "'$args{file}' is unsafe: owner is not root and not real uid",
689 )
690 if $uid != 0 && $uid != $<;
691  
692 1 ▶ Config::Scoped::Error::Validate::Permissions->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $mode & 18 1 0 1
693 -file => $thisparser->_get_file(%args),
694 -line => $thisparser->_get_line(%args),
695 -text => "'$args{file}' is unsafe: writeable by group or others",
696 )
697 if $mode & 022;
698  
699 0 return 1;
700 }
701  
702 # handle quoted strings, expand macro's and interpolate backslash
703 # patterns like \t, \n, etc. Called as action from within the grammar.
704 84 ▶sub _quotelike {
Subroutine _quotelike: called CC=11 SCAR=24.0
705 84 my $thisparser = shift;
706 84 my %args = @_;
707  
708 Config::Scoped::Error->throw(
709 -text => Carp::shortmess("missing parameter") )
710 84 ▶ unless defined $args{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"value"} 0 84 84
711  
712 84 my $value = $args{value};
713  
714 # accepts only '', "", <<foo, <<'foo', <<"foo" quotes and
715 # not q, qq, qx, qw, ..., s///, tr/// etc.
716 84 my %accept = ( single => 1, double => 1, '<<' => 1 );
717  
718 # see Text::Balanced::extract_quotelike() to understand this
719 # and of course Parse::RecDescent <perl_quotelike> directive
720 84 my $quote_name = $value->[0];
721 84 my $quote_delim = substr( $value->[1], 0, 1 );
722 84 my $quote_text = $value->[2];
723  
724 # the quote_name isn't set with plain quotes, set it now
725 84 ▶ unless ($quote_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quote_name) 69 15 84
726 69 ▶ $quote_name = 'double' if $quote_delim eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $quote_delim eq "\"" 28 41 69
727 69 ▶ $quote_name = 'single' if $quote_delim eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $quote_delim eq "'" 41 28 69
728 }
729  
730 # let the rule fail if not an accepted quote name
731 84 ▶ return undef unless $accept{$quote_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $accept{$quote_name} 0 84 84
732  
733 # backslash substitution in double quoted strings is
734 # done by reval() in the Safe compartment since
735 # it's possible to smuggle a subroutine call
736 # in a double quoted string.
737 #
738 84 ▶ $quote_text = $thisparser->_perl_code( expr => "\"$quote_text\"" )
Branch: 1 branch
BranchTrueFalseTotal
unless $quote_name eq "single" or $quote_delim eq "'" 38 46 84
Condition100%3/3
$quote_name eq "single" or $quote_delim eq "'"
l!l&&r!l&&!r
41538
MC/DC100%2/2
$quote_name eq "single" or $quote_delim eq "'"
$quote_name eq "single" $quote_delim eq "'"
Truth table100%3/3
A or B
A= $quote_name eq "single"B= $quote_delim eq "'"
ABresult
1X1
011
000
739 unless $quote_name eq 'single' || $quote_delim eq "'";
740  
741 # macro expansion for double quoted constructs
742 84 ▶ $quote_text = $thisparser->_expand_macro( %args, value => $quote_text )
Branch: 1 branch
BranchTrueFalseTotal
unless $quote_name eq "single" or $quote_delim eq "'" 38 46 84
Condition100%3/3
$quote_name eq "single" or $quote_delim eq "'"
l!l&&r!l&&!r
41538
MC/DC100%2/2
$quote_name eq "single" or $quote_delim eq "'"
$quote_name eq "single" $quote_delim eq "'"
Truth table100%3/3
A or B
A= $quote_name eq "single"B= $quote_delim eq "'"
ABresult
1X1
011
000
743 unless $quote_name eq 'single' || $quote_delim eq "'";
744  
745 # a P::RD rule can't return undef, then the rule would fail
746 84 ▶ return defined $quote_text ? $quote_text : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $quote_text ? : 84 0 84
747 }
748  
749 # slurp in the cfg files
750 15 ▶sub _get_cfg_text {
Subroutine _get_cfg_text: called CC=5 SCAR=16.6
751 15 my $thisparser = shift;
752 15 my %args = @_;
753  
754 Config::Scoped::Error->throw(
755 -text => Carp::shortmess("no cfg_file defined") )
756 15 ▶ unless defined $args{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"file"} 0 15 15
757 15 my $cfg_file = $args{file};
758  
759 15 local *CFG;
760  
761 # open the cfg file
762 15 ▶ Config::Scoped::Error::IO->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless open CFG, $cfg_file 0 15 15
763 -file => $thisparser->_get_file(%args),
764 -line => $thisparser->_get_line(%args),
765 -text => "Can't open cfg_file '$cfg_file': $!"
766 )
767 unless open( CFG, $cfg_file );
768  
769 # check the permission and ownership
770 15 ▶ Config::Scoped::Error::Validate::Permissions->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $thisparser->permissions_validate(%args, "handle", \*CFG) 0 14 14
771 -file => $thisparser->_get_file(%args),
772 -line => $thisparser->_get_line(%args),
773 -text => "permissions_validate returned false for cfg_file '$cfg_file'"
774 )
775 unless $thisparser->permissions_validate( %args, handle => \*CFG );
776  
777 # slurp the cfg_file, close the handle and return the text
778 14 my $cfg_text = join '', <CFG>;
779  
780 14 ▶ Config::Scoped::Error::IO->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless close CFG 0 14 14
781 -file => $thisparser->_get_file(%args),
782 -line => $thisparser->_get_line(%args),
783 -text => "Can't close cfg_file '$cfg_file' : $!"
784 )
785 unless close CFG;
786  
787 14 return $cfg_text;
788 }
789  
790 # eval perlcode in Safe compartment, called as action from within the grammar.
791 55 ▶sub _perl_code {
Subroutine _perl_code: called CC=5 SCAR=18.5
792 55 my $thisparser = shift;
793 55 my %args = @_;
794  
795 Config::Scoped::Error->throw(
796 -text => Carp::shortmess("no expression to eval defined") )
797 55 ▶ unless defined $args{expr};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"expr"} 0 55 55
798  
799 55 my $expr = $args{expr};
800  
801 # macro expansion before code evaluation
802 55 $expr = $thisparser->_expand_macro( %args, value => $expr );
803  
804 55 my $compartment = $thisparser->{local}{safe};
805  
806 # eval in Safe compartment
807 55 my $result = $compartment->reval($expr);
808  
809 # adjust error message and rethrow
810 55 ▶ if ( !defined $result && $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $result and $@) 0 55 55
Condition33%1/3
not defined $result and $@
!ll&&!rl&&r
5500
MC/DC0%0/2
not defined $result and $@
not defined $result $@
Truth table33%1/3
not(A) and B
A= not defined $resultB= $@
ABresult
0X0
100
111
811 0 chomp $@;
812 0 $@ .= "\n... (re)blessed and propagated via perl_code{}";
813  
814 0 Config::Scoped::Error::Parse->throw(
815 -file => $thisparser->_get_file(%args),
816 -line => $thisparser->_get_line(%args),
817 -text => $@,
818 );
819 }
820  
821 # a P::RD rule can't return undef, then the rule would fail
822 55 ▶ return defined $result ? $result : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 55 0 55
823 }
824  
825 # used for well spotted error messages
826 4 ▶sub _get_file {
Subroutine _get_file: called CC=4 SCAR=14.1
827 4 my $thisparser = shift;
828 4 my %args = @_;
829 return $args{parent_file}
830 || $args{file}
831 || $thisparser->{local}{cfg_file}
832 4 ▶ || '?';
Condition75%6/8
$args{'parent_file'} || $args{'file'} || $thisparser->{'local'}{'cfg_file'} || '?'
l!l
40
$args{"parent_file"} or $args{"file"} or $thisparser->{"local"}{"cfg_file"}
l!l&&r!l&&!r
220
$args{"parent_file"} or $args{"file"}
l!l&&r!l&&!r
112
MC/DC0%0/3
$args{'parent_file'} || $args{'file'} || $thisparser->{'local'}{'cfg_file'} || '?'
$args{"parent_file"} $args{"file"} $thisparser->{"local"}{"cfg_file"}
Truth table75%3/4
A or B or C
A= $args{"parent_file"}B= $args{"file"}C= $thisparser->{"local"}{"cfg_file"}
ABCresult
1XX1
01X1
0011
0000
833 }
834  
835 # used for well spotted error messages
836 4 ▶sub _get_line {
Subroutine _get_line: called CC=3 SCAR=11.0
837 4 my $thisparser = shift;
838 4 my %args = @_;
839 4 ▶ return $args{line} || $thisparser->{local}{line} || 0;
Condition80%4/5
$args{'line'} || $thisparser->{'local'}{'line'} || 0
l!l
22
$args{"line"} or $thisparser->{"local"}{"line"}
l!l&&r!l&&!r
022
MC/DC50%1/2
$args{'line'} || $thisparser->{'local'}{'line'} || 0
$args{"line"} $thisparser->{"local"}{"line"}
Truth table66%2/3
A or B
A= $args{"line"}B= $thisparser->{"local"}{"line"}
ABresult
1X1
011
000
840 }
841  
842 18 1;
843  
844 __DATA__
↑ Summary Scoped/Error.pm »