General.pm

filter: Statementstmt 80.3% 580 / 722 Branchbran 73.2% 328 / 448 Conditioncond 64.9% 74 / 114 MC/DCmcdc 42.0% 29 / 69 Subroutinesub 95.1% 39 / 41 Podpod 88.8% 8 / 9 totaltotal 75.4% 1058 / 1403 CC 279 SCAR 37.1
CC
279
Cov
76%
_parse
86.8
_read
63.5
_prepare
61.9
CRAP 40.9
↑ Summary General/Extended.pm »
1 #
2 # Config::General.pm - Generic Config Module
3 #
4 # Purpose: Provide a convenient way for loading
5 # config values from a given file and
6 # return it as hash structure
7 #
8 # Copyright (c) 2000-2026 Thomas Linden <tlinden |AT| cpan.org>.
9 # All Rights Reserved. Std. disclaimer applies.
10 # Licensed under the Artistic License 2.0.
11 #
12 # namespace
13 package Config::General;
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use File::Spec::Functions qw(splitpath file_name_is_absolute catfile catpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use File::Glob qw/:glob/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # on debian with perl > 5.8.4 croak() doesn't work anymore without this.
26 # There is some require statement which dies 'cause it can't find Carp::Heavy,
27 # I really don't understand, what the hell they made, but the debian perl
28 # installation is definitely bullshit, damn!
29 1 ▶use Carp::Heavy;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 $Config::General::VERSION = "2.69";
36  
37 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 our @EXPORT_OK = qw(ParseConfig SaveConfig SaveConfigString);
39  
40 1 ▶use constant _UTF8_BOM => "\x{ef}\x{bb}\x{bf}";
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 86 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
43 #
44 # create new Config::General object
45 #
46 86 my($this, @param ) = @_;
47 86 ▶ my $class = ref($this) || $this;
Condition66%2/3
ref $this || $this
l!l&&r!l&&!r
8780
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table66%2/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
48  
49 # define default options
50 86 my $self = {
51 # sha256 of current date
52 # hopefully this lowers the probability that
53 # this matches any configuration key or value out there
54 # bugfix for rt.40925
55 EOFseparator => 'ad7d7b87f5b81d2a0d5cb75294afeb91aa4801b1f8e8532dc1b633c0e1d47037',
56 SlashIsDirectory => 0,
57 AllowMultiOptions => 1,
58 MergeDuplicateOptions => 0,
59 MergeDuplicateBlocks => 0,
60 LowerCaseNames => 0,
61 ApacheCompatible => 0,
62 UseApacheInclude => 0,
63 IncludeRelative => 0,
64 IncludeDirectories => 0,
65 IncludeGlob => 0,
66 IncludeAgain => 0,
67 AutoLaunder => 0,
68 AutoTrue => 0,
69 AutoTrueFlags => {
70 true => '^(on|yes|true|1)$',
71 false => '^(off|no|false|0)$',
72 },
73 DefaultConfig => {},
74 String => '',
75 level => 1,
76 InterPolateVars => 0,
77 InterPolateEnv => 0,
78 ExtendedAccess => 0,
79 SplitPolicy => 'guess', # also possible: whitespace, equalsign and custom
80 SplitDelimiter => 0, # must be set by the user if SplitPolicy is 'custom'
81 StoreDelimiter => 0, # will be set by me unless user uses 'custom' policy
82 CComments => 1, # by default turned on
83 BackslashEscape => 0, # deprecated
84 StrictObjects => 1, # be strict on non-existent keys in OOP mode
85 StrictVars => 1, # be strict on undefined variables in Interpolate mode
86 Tie => q(), # could be set to a perl module for tie'ing new hashes
87 parsed => 0, # internal state stuff for variable interpolation
88 files => {}, # which files we have read, if any
89 UTF8 => 0,
90 SaveSorted => 0,
91 ForceArray => 0, # force single value array if value enclosed in []
92 AllowSingleQuoteInterpolation => 0,
93 NoEscape => 0,
94 NormalizeBlock => 0,
95 NormalizeOption => 0,
96 NormalizeValue => 0,
97 Plug => {},
98 UseApacheIfDefine => 0,
99 Define => {},
100 AlwaysQuoteOutput => 0
101 };
102  
103 # create the class instance
104 86 bless $self, $class;
105  
106 86 ▶ if ($#param >= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#param >= 1) { } 76 10 86
elsif ($#param == 0) { } 9 1 10
107 # use of the new hash interface!
108 76 $self->_prepare(@param);
109 }
110 elsif ($#param == 0) {
111 # use of the old style
112 9 $self->{ConfigFile} = $param[0];
113 9 ▶ if (ref($self->{ConfigFile}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"ConfigFile"} eq "HASH") 0 9 9
114 0 $self->{ConfigHash} = delete $self->{ConfigFile};
115 }
116 }
117 else {
118 # this happens if $#param == -1,1 thus no param was given to new!
119 1 $self->{config} = $self->_hashref();
120 1 $self->{parsed} = 1;
121 }
122  
123 # find split policy to use for option/value separation
124 71 $self->_splitpolicy();
125  
126 # bless into variable interpolation module if necessary
127 71 $self->_blessvars();
128  
129 # process as usual
130 71 ▶ if (!$self->{parsed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"parsed"}) 70 1 71
131 70 $self->_process();
132 }
133  
134 63 ▶ if ($self->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"InterPolateVars"}) 11 52 63
135 11 $self->{config} = $self->_clean_stack($self->{config});
136 }
137  
138 # bless into OOP namespace if required
139 63 $self->_blessoop();
140  
141 63 return $self;
142 }
143  
144  
145  
146 70 ▶sub _process {
Subroutine _process: called CC=11 SCAR=28.6
147 #
148 # call _read() and _parse() on the given config
149 70 my($self) = @_;
150  
151 70 ▶ if ($self->{DefaultConfig} && $self->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DefaultConfig"} and $self->{"InterPolateVars"}) 11 59 70
Condition66%2/3
$self->{"DefaultConfig"} and $self->{"InterPolateVars"}
!ll&&!rl&&r
05911
MC/DC50%1/2
$self->{"DefaultConfig"} and $self->{"InterPolateVars"}
$self->{"DefaultConfig"} $self->{"InterPolateVars"}
Truth table66%2/3
A and B
A= $self->{"DefaultConfig"}B= $self->{"InterPolateVars"}
ABresult
0X0
100
111
152 11 $self->{DefaultConfig} = $self->_interpolate_hash($self->{DefaultConfig}); # FIXME: _hashref() ?
153 }
154 70 ▶ if (exists $self->{StringContent}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->{'StringContent'}) { } 14 56 70
elsif (exists $self->{'ConfigHash'}) { } 11 45 56
elsif (ref $self->{'ConfigFile'} eq 'GLOB' or ref $self->{'ConfigFile'} eq 'FileHandle') { } 0 45 45
155 # consider the supplied string as config file
156 14 $self->_read($self->{StringContent}, 'SCALAR');
157 14 $self->{config} = $self->_parse($self->{DefaultConfig}, $self->{content});
158 }
159 elsif (exists $self->{ConfigHash}) {
160 11 ▶ if (ref($self->{ConfigHash}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'ConfigHash'} eq 'HASH') { } 10 1 11
161 # initialize with given hash
162 10 $self->{config} = $self->{ConfigHash};
163 10 $self->{parsed} = 1;
164 }
165 else {
166 1 croak "Config::General: Parameter -ConfigHash must be a hash reference!\n";
167 }
168 }
169 ▶ elsif (ref($self->{ConfigFile}) eq 'GLOB' || ref($self->{ConfigFile}) eq 'FileHandle') {
Condition33%1/3
ref $self->{"ConfigFile"} eq "GLOB" or ref $self->{"ConfigFile"} eq "FileHandle"
l!l&&r!l&&!r
0045
MC/DC0%0/2
ref $self->{"ConfigFile"} eq "GLOB" or ref $self->{"ConfigFile"} eq "FileHandle"
ref $self->{"ConfigFile"} eq "GLOB" ref $self->{"ConfigFile"} eq "FileHandle"
Truth table33%1/3
A or B
A= ref $self->{"ConfigFile"} eq "GLOB"B= ref $self->{"ConfigFile"} eq "FileHandle"
ABresult
1X1
011
000
170 # use the file the glob points to
171 0 $self->_read($self->{ConfigFile});
172 0 $self->{config} = $self->_parse($self->{DefaultConfig}, $self->{content});
173 }
174 else {
175 45 ▶ if ($self->{ConfigFile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ConfigFile'}) { } 45 0 45
176 # open the file and read the contents in
177 45 $self->{configfile} = $self->{ConfigFile};
178 45 ▶ if ( file_name_is_absolute($self->{ConfigFile}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_name_is_absolute($self->{"ConfigFile"})) 0 45 45
179 # look if this is an absolute path and save the basename if it is absolute
180 0 my ($volume, $path, undef) = splitpath($self->{ConfigFile});
181 0 $path =~ s#/$##; # remove eventually existing trailing slash
182 0 ▶ if (! $self->{ConfigPath}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ConfigPath"}) 0 0 0
183 0 $self->{ConfigPath} = [];
184 }
185 0 unshift @{$self->{ConfigPath}}, catpath($volume, $path, q());
186 }
187 45 $self->_open($self->{configfile});
188 # now, we parse immediately, getall simply returns the whole hash
189 42 $self->{config} = $self->_hashref();
190 42 $self->{config} = $self->_parse($self->{DefaultConfig}, $self->{content});
191 }
192 else {
193 # hm, no valid config file given, so try it as an empty object
194 0 $self->{config} = $self->_hashref();
195 0 $self->{parsed} = 1;
196 }
197 }
198 }
199  
200  
201 63 ▶sub _blessoop {
Subroutine _blessoop: called CC=3 SCAR=11.2
202 #
203 # bless into ::Extended if necessary
204 63 my($self) = @_;
205 63 ▶ if ($self->{ExtendedAccess}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ExtendedAccess"}) 11 52 63
206 # we are blessing here again, to get into the ::Extended namespace
207 # for inheriting the methods available over there, which we doesn't have.
208 11 bless $self, 'Config::General::Extended';
209 11 eval {
210 11 require Config::General::Extended;
211 };
212 11 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config::General::EVAL_ERROR) 0 11 11
213 0 croak "Config::General: " . $EVAL_ERROR;
214 }
215 }
216 # return $self;
217 }
218  
219 71 ▶sub _blessvars {
Subroutine _blessvars: called CC=4 SCAR=14.1
220 #
221 # bless into ::Interpolated if necessary
222 71 my($self) = @_;
223 71 ▶ if ($self->{InterPolateVars} || $self->{InterPolateEnv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"InterPolateVars"} or $self->{"InterPolateEnv"}) 11 60 71
Condition66%2/3
$self->{"InterPolateVars"} or $self->{"InterPolateEnv"}
l!l&&r!l&&!r
11060
MC/DC50%1/2
$self->{"InterPolateVars"} or $self->{"InterPolateEnv"}
$self->{"InterPolateVars"} $self->{"InterPolateEnv"}
Truth table66%2/3
A or B
A= $self->{"InterPolateVars"}B= $self->{"InterPolateEnv"}
ABresult
1X1
011
000
224 # InterPolateEnv implies InterPolateVars
225 11 $self->{InterPolateVars} = 1;
226  
227 # we are blessing here again, to get into the ::InterPolated namespace
228 # for inheriting the methods available overthere, which we doesn't have here.
229 11 bless $self, 'Config::General::Interpolated';
230 11 eval {
231 11 require Config::General::Interpolated;
232 };
233 11 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config::General::EVAL_ERROR) 0 11 11
234 0 croak "Config::General: " . $EVAL_ERROR;
235 }
236 # pre-compile the variable regexp
237 11 $self->{regex} = $self->_set_regex();
238 }
239 }
240  
241  
242 71 ▶sub _splitpolicy {
Subroutine _splitpolicy: called CC=9 SCAR=31.7
243 #
244 # find out what split policy to use
245 71 my($self) = @_;
246 71 ▶ if ($self->{SplitPolicy} ne 'guess') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'SplitPolicy'} ne 'guess') { } 2 69 71
247 2 ▶ if ($self->{SplitPolicy} eq 'whitespace') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'SplitPolicy'} eq 'whitespace') { } 0 2 2
elsif ($self->{'SplitPolicy'} eq 'equalsign') { } 0 2 2
elsif ($self->{'SplitPolicy'} eq 'custom') { } 2 0 2
248 0 $self->{SplitDelimiter} = '\s+';
249 0 ▶ if (!$self->{StoreDelimiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"StoreDelimiter"}) 0 0 0
250 0 $self->{StoreDelimiter} = q( );
251 }
252 }
253 elsif ($self->{SplitPolicy} eq 'equalsign') {
254 0 $self->{SplitDelimiter} = '\s*=\s*';
255 0 ▶ if (!$self->{StoreDelimiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"StoreDelimiter"}) 0 0 0
256 0 $self->{StoreDelimiter} = ' = ';
257 }
258 }
259 elsif ($self->{SplitPolicy} eq 'custom') {
260 2 ▶ if (! $self->{SplitDelimiter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"SplitDelimiter"}) 0 2 2
261 0 croak "Config::General: SplitPolicy set to 'custom' but no SplitDelimiter set.\n";
262 }
263 }
264 else {
265 0 croak "Config::General: Unsupported SplitPolicy: $self->{SplitPolicy}.\n";
266 }
267 }
268 else {
269 69 ▶ if (!$self->{StoreDelimiter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"StoreDelimiter"}) 69 0 69
270 69 $self->{StoreDelimiter} = q( );
271 }
272 }
273 }
274  
275 76 ▶sub _prepare {
Subroutine _prepare: called CC=37 SCAR=41.3
276 #
277 # prepare the class parameters, mangle them, if there
278 # are options to reset or to override, do it here.
279 76 my ($self, %conf) = @_;
280  
281 # save the parameter list for ::Extended's new() calls
282 76 $self->{Params} = \%conf;
283  
284 # be backwards compatible
285 76 ▶ if (exists $conf{-file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-file"}) 6 70 76
286 6 $self->{ConfigFile} = delete $conf{-file};
287 }
288 76 ▶ if (exists $conf{-hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-hash"}) 0 76 76
289 0 ▶ if (! $self->_validate_input_hash($conf{-hash})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_validate_input_hash($conf{"-hash"})) 0 0 0
290 0 croak "Config::General: supplied -hash hash contains invalid keys or values!\n";
291 }
292
293 0 $self->{ConfigHash} = delete $conf{-hash};
294 }
295  
296 # store input, file, handle, or array
297 76 ▶ if (exists $conf{-ConfigFile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-ConfigFile"}) 30 46 76
298 30 $self->{ConfigFile} = delete $conf{-ConfigFile};
299 }
300 76 ▶ if (exists $conf{-ConfigHash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-ConfigHash"}) 25 51 76
301 25 ▶ if (! $self->_validate_input_hash($conf{-ConfigHash})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_validate_input_hash($conf{"-ConfigHash"})) 14 11 25
302 14 croak "Config::General: supplied -ConfigHash hash contains invalid keys or values!\n";
303 }
304  
305 11 $self->{ConfigHash} = delete $conf{-ConfigHash};
306 }
307  
308 # store search path for relative configs, if any
309 62 ▶ if (exists $conf{-ConfigPath}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-ConfigPath"}) 0 62 62
310 0 my $configpath = delete $conf{-ConfigPath};
311 0 ▶ $self->{ConfigPath} = ref $configpath eq 'ARRAY' ? $configpath : [$configpath];
Branch: 1 branch
BranchTrueFalseTotal
ref $configpath eq 'ARRAY' ? : 0 0 0
312 }
313  
314 # handle options which contains values we need (strings, hashrefs or the like)
315 62 ▶ if (exists $conf{-String} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-String"}) 15 47 62
316 15 ▶ if (not ref $conf{-String}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $conf{'-String'}) { } 13 2 15
elsif (ref $conf{'-String'} eq 'ARRAY') { } 1 1 2
317 13 ▶ if ( $conf{-String}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf{"-String"}) 13 0 13
318 13 $self->{StringContent} = $conf{-String};
319 }
320 13 delete $conf{-String};
321 }
322 # re-implement arrayref support, removed after 2.22 as _read were
323 # re-organized
324 # fixed bug#33385
325 elsif(ref($conf{-String}) eq 'ARRAY') {
326 1 $self->{StringContent} = join "\n", @{$conf{-String}};
327 }
328 else {
329 1 croak "Config::General: Parameter -String must be a SCALAR or ARRAYREF!\n";
330 }
331 14 delete $conf{-String};
332 }
333 61 ▶ if (exists $conf{-Tie}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-Tie"}) 1 60 61
334 1 ▶ if ($conf{-Tie}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf{"-Tie"}) 1 0 1
335 1 $self->{Tie} = delete $conf{-Tie};
336 1 $self->{DefaultConfig} = $self->_hashref();
337 }
338 }
339  
340 61 ▶ if (exists $conf{-FlagBits}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-FlagBits"}) 2 59 61
341 2 ▶ if ($conf{-FlagBits} && ref($conf{-FlagBits}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf{"-FlagBits"} and ref $conf{"-FlagBits"} eq "HASH") 2 0 2
Condition33%1/3
$conf{"-FlagBits"} and ref $conf{"-FlagBits"} eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
$conf{"-FlagBits"} and ref $conf{"-FlagBits"} eq "HASH"
$conf{"-FlagBits"} ref $conf{"-FlagBits"} eq "HASH"
Truth table33%1/3
A and B
A= $conf{"-FlagBits"}B= ref $conf{"-FlagBits"} eq "HASH"
ABresult
0X0
100
111
342 2 $self->{FlagBits} = 1;
343 2 $self->{FlagBitsFlags} = $conf{-FlagBits};
344 }
345 2 delete $conf{-FlagBits};
346 }
347  
348 61 ▶ if (exists $conf{-DefaultConfig}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-DefaultConfig"}) 3 58 61
349 3 ▶ if ($conf{-DefaultConfig} && ref($conf{-DefaultConfig}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($conf{'-DefaultConfig'} and ref $conf{'-DefaultConfig'} eq 'HASH') { } 2 1 3
elsif ($conf{'-DefaultConfig'} and ref $conf{'-DefaultConfig'} eq '') { } 1 0 1
Condition66%2/3
$conf{"-DefaultConfig"} and ref $conf{"-DefaultConfig"} eq "HASH"
!ll&&!rl&&r
012
MC/DC50%1/2
$conf{"-DefaultConfig"} and ref $conf{"-DefaultConfig"} eq "HASH"
$conf{"-DefaultConfig"} ref $conf{"-DefaultConfig"} eq "HASH"
Truth table66%2/3
A and B
A= $conf{"-DefaultConfig"}B= ref $conf{"-DefaultConfig"} eq "HASH"
ABresult
0X0
100
111
350 # copy the hashref so that it is not being modified by subsequent calls, fixes bug#142095
351 2 ▶ if (! $self->_validate_input_hash($conf{-DefaultConfig})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_validate_input_hash($conf{"-DefaultConfig"})) 0 2 2
352 0 croak "Config::General: supplied -DefaultConfig hash contains invalid keys or values!\n";
353 }
354  
355 2 $self->{DefaultConfig} = $self->_copy($conf{-DefaultConfig});
356 }
357 ▶ elsif ($conf{-DefaultConfig} && ref($conf{-DefaultConfig}) eq q()) {
Condition33%1/3
$conf{"-DefaultConfig"} and ref $conf{"-DefaultConfig"} eq ""
!ll&&!rl&&r
001
MC/DC0%0/2
$conf{"-DefaultConfig"} and ref $conf{"-DefaultConfig"} eq ""
$conf{"-DefaultConfig"} ref $conf{"-DefaultConfig"} eq ""
Truth table33%1/3
A and B
A= $conf{"-DefaultConfig"}B= ref $conf{"-DefaultConfig"} eq ""
ABresult
0X0
100
111
358 1 $self->_read($conf{-DefaultConfig}, 'SCALAR');
359 1 $self->{DefaultConfig} = $self->_parse($self->_hashref(), $self->{content});
360 1 $self->{content} = ();
361 }
362 3 delete $conf{-DefaultConfig};
363 }
364  
365 # handle options which may either be true or false
366 # allowing "human" logic about what is true and what is not
367 61 foreach my $entry (keys %conf) {
368 76 my $key = $entry;
369 76 $key =~ s/^\-//;
370 76 ▶ if (! exists $self->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$key}) 0 76 76
371 0 croak "Config::General: Unknown parameter: $entry => \"$conf{$entry}\" (key: <$key>)\n";
372 }
373 76 ▶ if ($conf{$entry} =~ /$self->{AutoTrueFlags}->{true}/io) {
Branch: 2 branches
BranchTrueFalseTotal
if ($conf{$entry} =~ /$self->{'AutoTrueFlags'}{'true'}/io) { } 62 14 76
elsif ($conf{$entry} =~ /$self->{'AutoTrueFlags'}{'false'}/io) { } 6 8 14
374 62 $self->{$key} = 1;
375 }
376 elsif ($conf{$entry} =~ /$self->{AutoTrueFlags}->{false}/io) {
377 6 $self->{$key} = 0;
378 }
379 else {
380 # keep it untouched
381 8 $self->{$key} = $conf{$entry};
382 }
383 }
384  
385 61 ▶ if ($self->{MergeDuplicateOptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MergeDuplicateOptions"}) 6 55 61
386 # override if not set by user
387 6 ▶ if (! exists $conf{-AllowMultiOptions}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $conf{"-AllowMultiOptions"}) 6 0 6
388 6 $self->{AllowMultiOptions} = 0;
389 }
390 }
391  
392 61 ▶ if ($self->{ApacheCompatible}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ApacheCompatible"}) 0 61 61
393 # turn on all apache compatibility options which has
394 # been incorporated during the years...
395 0 $self->{UseApacheInclude} = 1;
396 0 $self->{IncludeRelative} = 1;
397 0 $self->{IncludeDirectories} = 1;
398 0 $self->{IncludeGlob} = 1;
399 0 $self->{SlashIsDirectory} = 1;
400 0 $self->{SplitPolicy} = 'whitespace';
401 0 $self->{CComments} = 0;
402 0 $self->{UseApacheIfDefine} = 1;
403 }
404  
405 61 ▶ if ($self->{UseApacheIfDefine}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"UseApacheIfDefine"}) 3 58 61
406 3 ▶ if (exists $conf{-Define}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{"-Define"}) 3 0 3
407 3 my $ref = ref($conf{-Define});
408  
409 3 ▶ if ($ref eq '') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref eq '') { } 1 2 3
elsif ($ref eq 'SCALAR') { } 0 2 2
elsif ($ref eq 'ARRAY') { } 1 1 2
elsif ($ref eq 'HASH') { } 1 0 1
410 1 $self->{Define} = {$conf{-Define} => 1};
411 }
412 elsif ($ref eq 'SCALAR') {
413 0 $self->{Define} = {${$conf{-Define}} = 1};
414 }
415 elsif ($ref eq 'ARRAY') {
416 1 my %h = map { $_ => 1 } @{$conf{-Define}};
417 1 $self->{Define} = \%h;
418 }
419 elsif ($ref eq 'HASH') {
420 1 $self->{Define} = $conf{-Define};
421 }
422 else {
423 0 croak qq{Config::General: Unsupported ref '$ref' for 'Define'};
424 }
425  
426 3 delete $conf{-Define};
427 }
428  
429 }
430 }
431  
432 49 ▶sub getall {
Subroutine getall: called CC=2 SCAR=7.1
Pod documented
433 #
434 # just return the whole config hash
435 #
436 49 my($this) = @_;
437 49 ▶ return (exists $this->{config} ? %{$this->{config}} : () );
Branch: 1 branch
BranchTrueFalseTotal
exists $this->{'config'} ? : 49 0 49
438 }
439  
440  
441 1 ▶sub files {
Subroutine files: called CC=2 SCAR=7.1
Pod documented
442 #
443 # return a list of files opened so far
444 #
445 1 my($this) = @_;
446 1 ▶ return (exists $this->{files} ? keys %{$this->{files}} : () );
Branch: 1 branch
BranchTrueFalseTotal
exists $this->{'files'} ? : 1 0 1
447 }
448  
449  
450 72 ▶sub _open {
Subroutine _open: called CC=28 SCAR=39.9
451 #
452 # open the config file, or expand a directory or glob or include
453 #
454 72 my($this, $basefile, $basepath) = @_;
455 72 my $cont;
456  
457 72 ($cont, $basefile, $basepath) = $this->_hook('pre_open', $basefile, $basepath);
458 72 ▶ return if(!$cont);
Branch: 1 branch
BranchTrueFalseTotal
unless $cont 0 72 72
459  
460 72 my($fh, $configfile);
461  
462 72 ▶ if($basepath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basepath) { } 8 64 72
463 # if this doesn't work we can still try later the global config path to use
464 8 $configfile = catfile($basepath, $basefile);
465 }
466 else {
467 64 $configfile = $basefile;
468 }
469  
470 72 my $glob = qr/[*?\[\{\\]/;
471 72 ▶ if ($^O =~ /win/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /win/i) 0 72 72
472 # fix for rt.cpan.org#116340: do only consider a backslash
473 # as meta escape char, but not if it appears on it's own,
474 # as it happens on windows platforms.
475 0 $glob = qr/(\\[*?\[\{\\]|[*?\[\{])/;
476 }
477  
478 72 ▶ if ($this->{IncludeGlob} and $configfile =~ /$glob/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"IncludeGlob"} and $configfile =~ /$glob/) 4 68 72
Condition100%3/3
$this->{"IncludeGlob"} and $configfile =~ /$glob/
!ll&&!rl&&r
5994
MC/DC100%2/2
$this->{"IncludeGlob"} and $configfile =~ /$glob/
$this->{"IncludeGlob"} $configfile =~ /$glob/
Truth table100%3/3
A and B
A= $this->{"IncludeGlob"}B= $configfile =~ /$glob/
ABresult
0X0
100
111
479 # Something like: *.conf (or maybe dir/*.conf) was included; expand it and
480 # pass each expansion through this method again.
481 4 local $_;
482 4 my @include = grep { -f $_ } bsd_glob($configfile, GLOB_BRACE | GLOB_QUOTE);
483  
484 # applied patch by AlexK fixing rt.cpan.org#41030
485 4 ▶ if ( !@include && defined $this->{ConfigPath} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @include and defined $this->{"ConfigPath"}) 0 4 4
Condition66%2/3
not @include and defined $this->{"ConfigPath"}
!ll&&!rl&&r
310
MC/DC0%0/2
not @include and defined $this->{"ConfigPath"}
not @include defined $this->{"ConfigPath"}
Truth table66%2/3
not(A) and B
A= not @includeB= defined $this->{"ConfigPath"}
ABresult
0X0
100
111
486 0 foreach my $dir (@{$this->{ConfigPath}}) {
487 0 my ($volume, $path, undef) = splitpath($basefile);
488 0 ▶ if ( -d catfile( $dir, $path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d catfile($dir, $path)) 0 0 0
489 0 push @include, grep { -f $_ } bsd_glob(catfile($dir, $basefile), GLOB_BRACE | GLOB_QUOTE);
490 0 last;
491 }
492 }
493 }
494  
495 # Multiple results or no expansion results (which is fine,
496 # include foo/* shouldn't fail if there isn't anything matching)
497 # rt.cpan.org#79869: local $this->{IncludeGlob};
498 4 foreach my $file (@include) {
499 6 $this->_open($file);
500 }
501 4 return;
502 }
503  
504 68 ▶ if (!-e $configfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $configfile) 3 65 68
505 3 my $found;
506 3 ▶ if (defined $this->{ConfigPath}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"ConfigPath"}) 0 3 3
507 # try to find the file within ConfigPath
508 0 foreach my $dir (@{$this->{ConfigPath}}) {
509 0 ▶ if( -e catfile($dir, $basefile) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e catfile($dir, $basefile)) 0 0 0
510 0 $configfile = catfile($dir, $basefile);
511 0 $found = 1;
512 0 last; # found it
513 }
514 }
515 }
516 3 ▶ if (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 3 0 3
517 0 ▶ my $path_message = defined $this->{ConfigPath} ? q( within ConfigPath: ) . join(q(.), @{$this->{ConfigPath}}) : q();
Branch: 1 branch
BranchTrueFalseTotal
defined $this->{'ConfigPath'} ? : 0 3 3
518 3 croak qq{Config::General The file "$basefile" does not exist$path_message!};
519 }
520 }
521  
522 65 local ($RS) = $RS;
523 65 ▶ if (! $RS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Config::General::RS) 0 65 65
524 0 carp(q(\$RS (INPUT_RECORD_SEPARATOR) is undefined. Guessing you want a line feed character));
525 0 $RS = "\n";
526 }
527  
528 65 ▶ if (-d $configfile and $this->{IncludeDirectories}) {
Branch: 3 branches
BranchTrueFalseTotal
if (-d $configfile and $this->{'IncludeDirectories'}) { } 1 64 65
elsif (-d $configfile) { } 0 64 64
elsif (-e _) { } 64 0 64
Condition66%2/3
-d $configfile and $this->{"IncludeDirectories"}
!ll&&!rl&&r
6401
MC/DC50%1/2
-d $configfile and $this->{"IncludeDirectories"}
-d $configfile $this->{"IncludeDirectories"}
Truth table66%2/3
A and B
A= -d $configfileB= $this->{"IncludeDirectories"}
ABresult
0X0
100
111
529 # A directory was included; include all the files inside that directory in ASCII order
530 1 local *INCLUDEDIR;
531 1 ▶ opendir INCLUDEDIR, $configfile or croak "Config::General: Could not open directory $configfile!($!)\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir INCLUDEDIR, $configfile or croak("Config::General: Could not open directory $configfile!($!)\n") 1 0 1
532 #my @files = sort grep { -f catfile($configfile, $_) } catfile($configfile, $_), readdir INCLUDEDIR;
533 # fixes rt.cpan.org#139261
534 1 my @files = sort grep { -f catfile($configfile, $_) } readdir INCLUDEDIR;
535 1 closedir INCLUDEDIR;
536 1 local $this->{CurrentConfigFilePath} = $configfile;
537 1 for (@files) {
538 5 my $file = catfile($configfile, $_);
539 5 ▶ if (! exists $this->{files}->{$file} or $this->{IncludeAgain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $this->{'files'}{$file} or $this->{'IncludeAgain'}) { } 5 0 5
Condition33%1/3
not exists $this->{"files"}{$file} or $this->{"IncludeAgain"}
l!l&&r!l&&!r
500
MC/DC0%0/2
not exists $this->{"files"}{$file} or $this->{"IncludeAgain"}
not exists $this->{"files"}{$file} $this->{"IncludeAgain"}
Truth table33%1/3
not(A) or B
A= not exists $this->{"files"}{$file}B= $this->{"IncludeAgain"}
ABresult
1X1
011
000
540 # support re-read if used urged us to do so, otherwise ignore the file
541 5 $fh = $this->_openfile_for_read($file);
542 5 $this->{files}->{"$file"} = 1;
543 5 $this->_read($fh);
544 }
545 else {
546 0 warn "File $file already loaded. Use -IncludeAgain to load it again.\n";
547 }
548 }
549 }
550 elsif (-d $configfile) {
551 0 croak "Config::General: config file argument is a directory, expecting a file!\n";
552 }
553 elsif (-e _) {
554 64 ▶ if (exists $this->{files}->{$configfile} and not $this->{IncludeAgain}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{'files'}{$configfile} and not $this->{'IncludeAgain'}) { } 1 63 64
Condition100%3/3
exists $this->{"files"}{$configfile} and not $this->{"IncludeAgain"}
!ll&&!rl&&r
6031
MC/DC100%2/2
exists $this->{"files"}{$configfile} and not $this->{"IncludeAgain"}
exists $this->{"files"}{$configfile} not $this->{"IncludeAgain"}
Truth table100%3/3
A and not(B)
A= exists $this->{"files"}{$configfile}B= not $this->{"IncludeAgain"}
ABresult
0X0
100
111
555 # do not read the same file twice, just return
556 1 warn "File $configfile already loaded. Use -IncludeAgain to load it again.\n";
557 1 return;
558 }
559 else {
560 63 $fh = $this->_openfile_for_read($configfile);
561 63 $this->{files}->{$configfile} = 1;
562  
563 63 my ($volume, $path, undef) = splitpath($configfile);
564 63 local $this->{CurrentConfigFilePath} = catpath($volume, $path, q());
565  
566 63 $this->_read($fh);
567 }
568 }
569 63 return;
570 }
571  
572  
573 68 ▶sub _openfile_for_read {
Subroutine _openfile_for_read: called CC=7 SCAR=19.6
574 #
575 # actually open a file, turn on utf8 mode if requested by bom
576 #
577 68 my ($this, $file) = @_;
578  
579 68 ▶ my $fh = IO::File->new( $file, 'r')
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file, "r") or croak("Config::General: Could not open $file!($!)\n") 68 0 68
580 or croak "Config::General: Could not open $file!($!)\n";
581  
582 # attempt to read an initial utf8 byte-order mark (BOM)
583 68 my $n_read = sysread $fh, my $read_BOM, length(_UTF8_BOM);
584 68 ▶ my $has_BOM = $n_read == length(_UTF8_BOM) && $read_BOM eq _UTF8_BOM;
Condition66%2/3
$n_read == 3 && $read_BOM eq "\357\273\277"
!ll&&!rl&&r
0644
MC/DC50%1/2
$n_read == 3 && $read_BOM eq "\357\273\277"
$n_read == 3 $read_BOM eq "\357\273\277"
Truth table66%2/3
A && B
A= $n_read == 3B= $read_BOM eq "\357\273\277"
ABresult
0X0
100
111
585  
586 # set utf8 perlio layer if BOM was found or if option -UTF8 is turned on
587 68 ▶ binmode $fh, ":utf8" if $this->{UTF8} || $has_BOM;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"UTF8"} or $has_BOM 4 64 68
Condition100%3/3
$this->{"UTF8"} or $has_BOM
l!l&&r!l&&!r
2264
MC/DC100%2/2
$this->{"UTF8"} or $has_BOM
$this->{"UTF8"} $has_BOM
Truth table100%3/3
A or B
A= $this->{"UTF8"}B= $has_BOM
ABresult
1X1
011
000
588  
589 # rewind to beginning of file if we read chars that were not the BOM
590 68 ▶ sysseek $fh, 0, 0 if $n_read && !$has_BOM;
Branch: 1 branch
BranchTrueFalseTotal
if $n_read and not $has_BOM 64 4 68
Condition66%2/3
$n_read and not $has_BOM
!ll&&!rl&&r
0464
MC/DC50%1/2
$n_read and not $has_BOM
$n_read not $has_BOM
Truth table66%2/3
A and not(B)
A= $n_readB= not $has_BOM
ABresult
0X0
100
111
591  
592 68 return $fh;
593 }
594  
595  
596  
597 83 ▶sub _read {
Subroutine _read: called CC=49 SCAR=41.5
598 #
599 # store the config contents in @content
600 # and prepare it somewhat for easier parsing later
601 # (comments, continuing lines, and stuff)
602 #
603 83 my($this, $fh, $flag) = @_;
604  
605  
606 83 my(@stuff, @content, $c_comment, $longline, $hier, $hierend, @hierdoc);
607 83 local $_ = q();
608  
609 83 ▶ if ($flag && $flag eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag and $flag eq 'SCALAR') { } 15 68 83
Condition66%2/3
$flag and $flag eq "SCALAR"
!ll&&!rl&&r
68015
MC/DC50%1/2
$flag and $flag eq "SCALAR"
$flag $flag eq "SCALAR"
Truth table66%2/3
A and B
A= $flagB= $flag eq "SCALAR"
ABresult
0X0
100
111
610 15 ▶ if (ref($fh) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq 'ARRAY') { } 0 15 15
611 0 @stuff = @{$fh};
612 }
613 else {
614 15 @stuff = split /\n/, $fh;
615 }
616 }
617 else {
618 68 @stuff = <$fh>;
619 }
620  
621 83 my $cont;
622 83 ($cont, $fh, @stuff) = $this->_hook('pre_read', $fh, @stuff);
623 83 ▶ return if(!$cont);
Branch: 1 branch
BranchTrueFalseTotal
unless $cont 0 83 83
624  
625 83 ▶ if ($this->{UseApacheIfDefine}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"UseApacheIfDefine"}) 3 80 83
626 3 $this->_process_apache_ifdefine(\@stuff);
627 }
628  
629 83 foreach (@stuff) {
630 767 ▶ if ($this->{AutoLaunder}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"AutoLaunder"}) 0 767 767
631 0 ▶ if (m/^(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(.*)$/) 0 0 0
632 0 $_ = $1;
633 }
634 }
635  
636 767 chomp;
637  
638  
639 767 ▶ if ($hier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hier) 25 742 767
640 # inside here-doc, only look for $hierend marker
641 25 ▶ if (/^(\s*)\Q$hierend\E\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\s*)\Q$hierend\E\s*$/) { } 7 18 25
642 7 my $indent = $1; # preserve indentation
643 7 $hier .= ' ' . $this->{EOFseparator}; # bugfix of rt.40925
644 # _parse will also preserver indentation
645 7 ▶ if ($indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent) { } 0 7 7
646 0 foreach (@hierdoc) {
647 0 s/^$indent//; # i.e. the end was: " EOF" then we remove " " from every here-doc line
648 0 $hier .= $_ . "\n"; # and store it in $hier
649 }
650 }
651 else {
652 7 $hier .= join "\n", @hierdoc; # there was no indentation of the end-string, so join it 1:1
653 }
654 7 push @{$this->{content}}, $hier; # push it onto the content stack
655 7 @hierdoc = ();
656 7 undef $hier;
657 7 undef $hierend;
658 }
659 else {
660 # everything else onto the stack
661 18 push @hierdoc, $_;
662 }
663 25 next;
664 }
665  
666 742 ▶ if ($this->{CComments}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"CComments"}) 742 0 742
667 # look for C-Style comments, if activated
668 742 ▶ if (/(\s*\/\*.*\*\/\s*)/) {
Branch: 3 branches
BranchTrueFalseTotal
if (m[(\s*/\*.*\*/\s*)]) { } 2 740 742
elsif (m[^\s*/\*]) { } 15 725 740
elsif (m[\*/]) { } 15 710 725
669 # single c-comment on one line
670 2 s/\s*\/\*.*\*\/\s*//;
671 }
672 elsif (/^\s*\/\*/) {
673 # the beginning of a C-comment ("/*"), from now on ignore everything.
674 15 ▶ if (/\*\/\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[\*/\s*$]) { } 0 15 15
675 # C-comment end is already there, so just ignore this line!
676 0 $c_comment = 0;
677 }
678 else {
679 15 $c_comment = 1;
680 }
681 }
682 elsif (/\*\//) {
683 15 ▶ if (!$c_comment) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c_comment) 0 15 15
684 0 warn "invalid syntax: found end of C-comment without previous start!\n";
685 }
686 15 $c_comment = 0; # the current C-comment ends here, go on
687 15 s/^.*\*\///; # if there is still stuff, it will be read
688 }
689 742 ▶ next if($c_comment); # ignore EVERYTHING from now on, IF it IS a C-Comment
Branch: 1 branch
BranchTrueFalseTotal
if $c_comment 152 590 742
690 }
691  
692 # Remove comments and empty lines
693 590 s/(?<!\\)#.*$//; # .+ => .* bugfix rt.cpan.org#44600
694 590 ▶ next if /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ 0 590 590
695  
696 # look for multiline option, indicated by a trailing backslash
697 590 ▶ if (/(?<!\\)\\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/(?<!\\)\\$/) 9 581 590
698 9 chop; # remove trailing backslash
699 9 s/^\s*//;
700 9 $longline .= $_;
701 9 next;
702 }
703  
704 # transform explicit-empty blocks to conforming blocks
705 # rt.cpan.org#80006 added \s* before $/
706 581 ▶ if (!$this->{ApacheCompatible} && /\s*<([^\/]+?.*?)\/>\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->{"ApacheCompatible"} and m[\s*<([^/]+?.*?)/>\s*$]) 3 578 581
Condition66%2/3
not $this->{"ApacheCompatible"} and m[\s*<([^/]+?.*?)/>\s*$]
!ll&&!rl&&r
05783
MC/DC50%1/2
not $this->{"ApacheCompatible"} and m[\s*<([^/]+?.*?)/>\s*$]
not $this->{"ApacheCompatible"} m[\s*<([^/]+?.*?)/>\s*$]
Truth table66%2/3
not(A) and B
A= not $this->{"ApacheCompatible"}B= m[\s*<([^/]+?.*?)/>\s*$]
ABresult
0X0
100
111
707 3 my $block = $1;
708 3 ▶ if ($block !~ /\"/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block =~ /\"/) 3 0 3
709 3 ▶ if ($block !~ /\s[^\s]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block =~ /\s[^\s]/) 3 0 3
710 # fix of bug 7957, add quotation to pure slash at the
711 # end of a block so that it will be considered as directory
712 # unless the block is already quoted or contains whitespaces
713 # and no quotes.
714 3 ▶ if ($this->{SlashIsDirectory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"SlashIsDirectory"}) 3 0 3
715 3 push @{$this->{content}}, '<' . $block . '"/">';
716 3 next;
717 }
718 }
719 }
720 0 my $orig = $_;
721 0 $orig =~ s/\/>$/>/;
722 0 $block =~ s/\s\s*.*$//;
723 0 push @{$this->{content}}, $orig, "</${block}>";
724 0 next;
725 }
726  
727  
728 # look for here-doc identifier
729 578 ▶ if ($this->{SplitPolicy} eq 'guess') {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'SplitPolicy'} eq 'guess') { } 508 70 578
730 508 ▶ if (/^\s*([^=]+?)\s*=\s*<<\s*(.+?)\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s*([^=]+?)\s*=\s*<<\s*(.+?)\s*$/) { } 2 506 508
elsif (/^\s*(\S+?)\s+<<\s*(.+?)\s*$/) { } 5 501 506
731 # try equal sign (fix bug rt#36607)
732 2 $hier = $1; # the actual here-doc variable name
733 2 $hierend = $2; # the here-doc identifier, i.e. "EOF"
734 2 next;
735 }
736 elsif (/^\s*(\S+?)\s+<<\s*(.+?)\s*$/) {
737 # try whitespace
738 5 $hier = $1; # the actual here-doc variable name
739 5 $hierend = $2; # the here-doc identifier, i.e. "EOF"
740 5 next;
741 }
742 }
743 else {
744 # no guess, use one of the configured strict split policies
745 70 ▶ if (/^\s*(.+?)($this->{SplitDelimiter})<<\s*(.+?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(.+?)($this->{"SplitDelimiter"})<<\s*(.+?)\s*$/) 1 69 70
746 1 $hier = $1; # the actual here-doc variable name
747 1 $hierend = $3; # the here-doc identifier, i.e. "EOF"
748 1 next;
749 }
750 }
751  
752  
753  
754 ###
755 ### any "normal" config lines from now on
756 ###
757  
758 570 ▶ if ($longline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($longline) { } 5 565 570
759 # previous stuff was a longline and this is the last line of the longline
760 5 s/^\s*//;
761 5 $longline .= $_;
762 5 push @{$this->{content}}, $longline; # push it onto the content stack
763 5 undef $longline;
764 5 next;
765 }
766 else {
767 # ignore empty lines
768 565 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 131 434 565
769  
770 # look for include statement(s)
771 434 my $incl_file;
772 434 my $path = '';
773 434 ▶ if ( $this->{IncludeRelative} and defined $this->{CurrentConfigFilePath}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'IncludeRelative'} and defined $this->{'CurrentConfigFilePath'}) { } 87 347 434
elsif (defined $this->{'ConfigPath'}) { } 0 347 347
Condition66%2/3
$this->{"IncludeRelative"} and defined $this->{"CurrentConfigFilePath"}
!ll&&!rl&&r
347087
MC/DC50%1/2
$this->{"IncludeRelative"} and defined $this->{"CurrentConfigFilePath"}
$this->{"IncludeRelative"} defined $this->{"CurrentConfigFilePath"}
Truth table66%2/3
A and B
A= $this->{"IncludeRelative"}B= defined $this->{"CurrentConfigFilePath"}
ABresult
0X0
100
111
774 87 $path = $this->{CurrentConfigFilePath};
775 }
776 elsif (defined $this->{ConfigPath}) {
777 # fetch pathname of base config file, assuming the 1st one is the path of it
778 0 $path = $this->{ConfigPath}->[0];
779 }
780  
781 # bugfix rt.cpan.org#38635: support quoted filenames
782 434 ▶ if ($this->{UseApacheInclude}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'UseApacheInclude'}) { } 16 418 434
783 16 my $opt = '';
784 16 ▶ if (/^\s*(include|includeoptional)\s*(["'])(.*?)(?<!\\)\2$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s*(include|includeoptional)\s*(["'])(.*?)(?<!\\)\2$/i) { } 1 15 16
elsif (/^\s*(include|includeoptional)\s+(.+?)\s*$/i) { } 3 12 15
785 1 $incl_file = $3;
786 1 $opt = $1;
787 }
788 elsif (/^\s*(include|includeoptional)\s+(.+?)\s*$/i) {
789 3 $incl_file = $2;
790 3 $opt = $1;
791 }
792 16 ▶ if ($incl_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($incl_file) 4 12 16
793 4 ▶ if ($this->{IncludeGlob} && $opt =~ /opt/i && $incl_file !~ /[*?\[\{\\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"IncludeGlob"} and $opt =~ /opt/i and not $incl_file =~ /[*?\[\{\\]/) 2 2 4
Condition66%4/6
$this->{"IncludeGlob"} and $opt =~ /opt/i and not $incl_file =~ /[*?\[\{\\]/
!ll&&!rl&&r
202
$this->{"IncludeGlob"} and $opt =~ /opt/i
!ll&&!rl&&r
202
MC/DC33%1/3
$this->{"IncludeGlob"} and $opt =~ /opt/i and not $incl_file =~ /[*?\[\{\\]/
$this->{"IncludeGlob"} $opt =~ /opt/i not $incl_file =~ /[*?\[\{\\]/
Truth table50%2/4
A and B and not(C)
A= $this->{"IncludeGlob"}B= $opt =~ /opt/iC= not $incl_file =~ /[*?\[\{\\]/
ABCresult
0XX0
10X0
1100
1111
794 # fix rt#107108
795 # glob enabled && optional include && file is not already a glob:
796 # turn it into a singular matching glob, like:
797 # "file" => "[f][i][l][e]" and:
798 # "dir/file" => "dir/[f][i][l][e]"
799 # which IS a glob but only matches that particular file. if it
800 # doesn't exist, it will be ignored by _open(), just what
801 # we'd like to have when using IncludeOptional.
802 2 my ($vol,$dirs,$file) = splitpath( $incl_file );
803 2 $incl_file = catpath($vol, $dirs, join '', map { "[$_]" } split //, $file);
804 }
805 }
806 }
807 else {
808 418 ▶ if (/^\s*<<include\s+(["'])(.+?)>>\\s*$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s*<<include\s+(["'])(.+?)>>\\s*$/i) { } 0 418 418
elsif (/^\s*<<include\s+(.+?)>>\s*$/i) { } 17 401 418
809 0 $incl_file = $2;
810 }
811 elsif (/^\s*<<include\s+(.+?)>>\s*$/i) {
812 17 $incl_file = $1;
813 }
814 }
815  
816 434 ▶ if ($incl_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($incl_file) { } 21 413 434
817 21 ▶ if ( $this->{IncludeRelative} && $path && !file_name_is_absolute($incl_file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'IncludeRelative'} and $path and not file_name_is_absolute($incl_file)) { } 8 13 21
Condition66%4/6
$this->{"IncludeRelative"} and $path and not file_name_is_absolute($incl_file)
!ll&&!rl&&r
1308
$this->{"IncludeRelative"} and $path
!ll&&!rl&&r
1308
MC/DC33%1/3
$this->{"IncludeRelative"} and $path and not file_name_is_absolute($incl_file)
$this->{"IncludeRelative"} $path not file_name_is_absolute($incl_file)
Truth table50%2/4
A and B and not(C)
A= $this->{"IncludeRelative"}B= $pathC= not file_name_is_absolute($incl_file)
ABCresult
0XX0
10X0
1100
1111
818 # include the file from within location of $this->{configfile}
819 8 $this->_open( $incl_file, $path );
820 }
821 else {
822 # include the file from within pwd, or absolute
823 13 $this->_open($incl_file);
824 }
825 }
826 else {
827 # standard entry, (option = value)
828 413 push @{$this->{content}}, $_;
829 }
830  
831 }
832  
833 }
834  
835 82 ($cont, $this->{content}) = $this->_hook('post_read', $this->{content});
836 82 return 1;
837 }
838  
839  
840 3 ▶sub _process_apache_ifdefine {
Subroutine _process_apache_ifdefine: called CC=9 SCAR=24.0
841 #
842 # Loop trough config lines and exclude all those entries
843 # for which there's no IFDEF but which reside inside an IFDEF.
844 #
845 # Called from _read(), if UseApacheIfDefine is enabled, returns
846 # the modified array.
847 3 my($this, $rawlines) = @_;
848  
849 3 my @filtered;
850 3 my @includeFlag = (1);
851  
852 3 foreach (@{$rawlines}) {
853 9 ▶ if (/^\s*<\s*IfDefine\s+([!]*)("[^"]+"|\S+)\s*>/i) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^\s*<\s*IfDefine\s+([!]*)("[^"]+"|\S+)\s*>/i) { } 3 6 9
elsif (m[^\s*<\s*/IfDefine\s*>]i) { } 3 3 6
elsif ($includeFlag[-1] and /^\s*Define\s+("[^"]+"|\S+)/i) { } 0 3 3
elsif ($includeFlag[-1]) { } 3 0 3
854 # new IFDEF block, mark following content to be included if
855 # the DEF is known, otherwise skip it til end of IFDEF
856 3 my ($negate, $define) = ($1 eq '!',$2);
857  
858 push(@includeFlag,
859 $includeFlag[-1] &
860 3 ((not $negate) & (exists $this->{Define}{$define}))
861 );
862 }
863 elsif (/^\s*<\s*\/IfDefine\s*>/i) {
864 3 ▶ if (scalar(@includeFlag) <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @includeFlag <= 1) 0 3 3
865 0 croak qq(Config::General: </IfDefine> without a <IfDefine>!\n);
866 }
867 3 pop(@includeFlag);
868 }
869 ▶ elsif ($includeFlag[-1] && /^\s*Define\s+("[^"]+"|\S+)/i) {
Condition33%1/3
$includeFlag[-1] and /^\s*Define\s+("[^"]+"|\S+)/i
!ll&&!rl&&r
030
MC/DC0%0/2
$includeFlag[-1] and /^\s*Define\s+("[^"]+"|\S+)/i
$includeFlag[-1] /^\s*Define\s+("[^"]+"|\S+)/i
Truth table33%1/3
A and B
A= $includeFlag[-1]B= /^\s*Define\s+("[^"]+"|\S+)/i
ABresult
0X0
100
111
870 # inline Define, add it to our list
871 0 $this->{Define}{$1} = 1;
872 }
873 elsif ($includeFlag[-1]) {
874 3 push @filtered, $_;
875 }
876 }
877  
878 3 ▶ if (scalar(@includeFlag) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @includeFlag > 1) 0 3 3
879 0 croak qq(Config::General: Block <IfDefine> has no EndBlock statement!\n);
880 }
881  
882 3 @$rawlines = @filtered; # replace caller array
883 }
884  
885  
886 139 ▶sub _parse {
Subroutine _parse: called CC=53 SCAR=44.6
887 #
888 # parse the contents of the file
889 #
890 139 my($this, $config, $content) = @_;
891 139 my(@newcontent, $block, $blockname, $chunk,$block_level);
892 139 local $_;
893  
894 139 foreach (@{$content}) { # loop over content stack
895 622 chomp;
896 622 $chunk++;
897 622 $_ =~ s/^\s+//; # strip spaces @ end and begin
898 622 $_ =~ s/\s+$//;
899  
900 #
901 # build option value assignment, split current input
902 # using whitespace, equal sign or optionally here-doc
903 # separator EOFseparator
904 622 my ($option,$value);
905 622 ▶ if (/$this->{EOFseparator}/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$this->{'EOFseparator'}/) { } 7 615 622
906 7 ($option,$value) = split /\s*$this->{EOFseparator}\s*/, $_, 2; # separated by heredoc-finding in _open()
907 }
908 else {
909 615 ▶ if ($this->{SplitPolicy} eq 'guess') {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'SplitPolicy'} eq 'guess') { } 500 115 615
910 # again the old regex. use equalsign SplitPolicy to get the
911 # 2.00 behavior. the new regexes were too odd.
912 500 ($option,$value) = split /\s*=\s*|\s+/, $_, 2;
913 }
914 else {
915 # no guess, use one of the configured strict split policies
916 115 ($option,$value) = split /$this->{SplitDelimiter}/, $_, 2;
917 }
918 }
919  
920 622 ▶ if($this->{NormalizeOption}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"NormalizeOption"}) 0 622 622
921 0 $option = $this->{NormalizeOption}($option);
922 }
923  
924 622 ▶ if ($value && $value =~ /^"/ && $value =~ /"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $value =~ /^"/ and $value =~ /"$/) 10 612 622
Condition100%6/6
$value and $value =~ /^"/ and $value =~ /"$/
!ll&&!rl&&r
610210
$value and $value =~ /^"/
!ll&&!rl&&r
18142912
MC/DC100%3/3
$value and $value =~ /^"/ and $value =~ /"$/
$value $value =~ /^"/ $value =~ /"$/
Truth table100%4/4
A and B and C
A= $valueB= $value =~ /^"/C= $value =~ /"$/
ABCresult
0XX0
10X0
1100
1111
925 10 $value =~ s/^"//; # remove leading and trailing "
926 10 $value =~ s/"$//;
927 }
928 622 ▶ if (! defined $block) { # not inside a block @ the moment
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $block) { } 336 286 622
elsif (m[^<([^/]+?.*?)>$]) { } 21 265 286
elsif (m[^</(.+?)>$]) { } 107 158 265
929 336 ▶ if (/^<([^\/]+?.*?)>$/) { # look if it is a block
Branch: 2 branches
BranchTrueFalseTotal
if (m[^<([^/]+?.*?)>$]) { } 86 250 336
elsif (m[^</(.+?)>$]) { } 0 250 250
930 86 $block = $1; # store block name
931 86 ▶ if ($block =~ /^"([^"]+)"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /^"([^"]+)"$/) { } 1 85 86
932 # quoted block, unquote it and do not split
933 1 $block =~ s/"//g;
934 }
935 else {
936 # If it is a named block store the name separately; allow the block and name to each be quoted
937 85 ▶ if ($block =~ /^(?:"([^"]+)"|(\S+))(?:\s+(?:"([^"]+)"|(.*)))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /^(?:"([^"]+)"|(\S+))(?:\s+(?:"([^"]+)"|(.*)))?$/) 85 0 85
938 85 ▶ $block = $1 || $2;
Condition66%2/3
$1 || $2
l!l&&r!l&&!r
1840
MC/DC0%0/2
$1 || $2
$1 $2
Truth table66%2/3
A || B
A= $1B= $2
ABresult
1X1
011
000
939 85 ▶ $blockname = $3 || $4;
Condition100%3/3
$3 || $4
l!l&&r!l&&!r
73246
MC/DC100%2/2
$3 || $4
$3 $4
Truth table100%3/3
A || B
A= $3B= $4
ABresult
1X1
011
000
940 }
941 }
942 86 ▶ if($this->{NormalizeBlock}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"NormalizeBlock"}) 0 86 86
943 0 $block = $this->{NormalizeBlock}($block);
944 0 ▶ if (defined $blockname) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $blockname) 0 0 0
945 0 $blockname = $this->{NormalizeBlock}($blockname);
946 0 ▶ if($blockname eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($blockname eq "") 0 0 0
947 # if, after normalization no blockname is left, remove it
948 0 $blockname = undef;
949 }
950 }
951 }
952 86 ▶ if ($this->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"InterPolateVars"}) 30 56 86
953 # interpolate block(name), add "<" and ">" to the key, because
954 # it is sure that such keys does not exist otherwise.
955 30 $block = $this->_interpolate($config, "<$block>", $block);
956 30 ▶ if (defined $blockname) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $blockname) 12 18 30
957 12 $blockname = $this->_interpolate($config, "<$blockname>", "$blockname");
958 }
959 }
960 86 ▶ if ($this->{LowerCaseNames}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"LowerCaseNames"}) 0 86 86
961 0 $block = lc $block; # only for blocks lc(), if configured via new()
962 }
963 86 $this->{level} += 1;
964 86 undef @newcontent;
965 86 next;
966 }
967 elsif (/^<\/(.+?)>$/) { # it is an end block, but we don't have a matching block!
968 0 croak "Config::General: EndBlock \"<\/$1>\" has no StartBlock statement (level: $this->{level}, chunk $chunk)!\n";
969 }
970 else { # insert key/value pair into actual node
971 250 ▶ if ($this->{LowerCaseNames}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"LowerCaseNames"}) 0 250 250
972 0 $option = lc $option;
973 }
974  
975 250 ▶ if (exists $config->{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{$option}) { } 25 225 250
976 25 ▶ if ($this->{MergeDuplicateOptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'MergeDuplicateOptions'}) { } 5 20 25
977 5 $config->{$option} = $this->_parse_value($config, $option, $value);
978  
979 # bugfix rt.cpan.org#33216
980 5 ▶ if ($this->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"InterPolateVars"}) 2 3 5
981 # save pair on local stack
982 2 $config->{__stack}->{$option} = $config->{$option};
983 }
984 }
985 else {
986 20 ▶ if (! $this->{AllowMultiOptions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->{'AllowMultiOptions'}) { } 0 20 20
987 # no, duplicates not allowed
988 0 croak "Config::General: Option \"$option\" occurs more than once (level: $this->{level}, chunk $chunk)!\n";
989 }
990 else {
991 # yes, duplicates allowed
992 20 ▶ if (ref($config->{$option}) ne 'ARRAY') { # convert scalar to array
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config->{$option} ne "ARRAY") 13 7 20
993 13 my $savevalue = $config->{$option};
994 13 delete $config->{$option};
995 13 push @{$config->{$option}}, $savevalue;
996 }
997 20 eval {
998 # check if arrays are supported by the underlying hash
999 20 my $i = scalar @{$config->{$option}};
1000 };
1001 20 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config::General::EVAL_ERROR) { } 0 20 20
1002 0 $config->{$option} = $this->_parse_value($config, $option, $value);
1003 }
1004 else {
1005 # it's already an array, just push
1006 20 push @{$config->{$option}}, $this->_parse_value($config, $option, $value);
1007 }
1008 }
1009 }
1010 }
1011 else {
1012 225 ▶ if($this->{ForceArray} && defined $value && $value =~ /^\[\s*(.+?)\s*\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'ForceArray'} and defined $value and $value =~ /^\[\s*(.+?)\s*\]$/) { } 2 223 225
Condition66%4/6
$this->{"ForceArray"} and defined $value and $value =~ /^\[\s*(.+?)\s*\]$/
!ll&&!rl&&r
22302
$this->{"ForceArray"} and defined $value
!ll&&!rl&&r
22302
MC/DC33%1/3
$this->{"ForceArray"} and defined $value and $value =~ /^\[\s*(.+?)\s*\]$/
$this->{"ForceArray"} defined $value $value =~ /^\[\s*(.+?)\s*\]$/
Truth table50%2/4
A and B and C
A= $this->{"ForceArray"}B= defined $valueC= $value =~ /^\[\s*(.+?)\s*\]$/
ABCresult
0XX0
10X0
1100
1111
1013 # force single value array entry
1014 2 push @{$config->{$option}}, $this->_parse_value($config, $option, $1);
1015 }
1016 else {
1017 # standard config option, insert key/value pair into node
1018 223 $config->{$option} = $this->_parse_value($config, $option, $value);
1019  
1020 223 ▶ if ($this->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"InterPolateVars"}) 72 151 223
1021 # save pair on local stack
1022 72 $config->{__stack}->{$option} = $config->{$option};
1023 }
1024 }
1025 }
1026 }
1027 }
1028 elsif (/^<([^\/]+?.*?)>$/) { # found a start block inside a block, don't forget it
1029 21 $block_level++; # $block_level indicates wether we are still inside a node
1030 21 push @newcontent, $_; # push onto new content stack for later recursive call of _parse()
1031 }
1032 elsif (/^<\/(.+?)>$/) {
1033 107 ▶ if ($block_level) { # this endblock is not the one we are searching for, decrement and push
Branch: 1 branch
BranchTrueFalseTotal
if ($block_level) { } 21 86 107
1034 21 $block_level--; # if it is 0, then the endblock was the one we searched for, see below
1035 21 push @newcontent, $_; # push onto new content stack
1036 }
1037 else { # calling myself recursively, end of $block reached, $block_level is 0
1038 86 ▶ if (defined $blockname) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $blockname) { } 42 44 86
1039 # a named block, make it a hashref inside a hash within the current node
1040  
1041 42 ▶ if (! exists $config->{$block}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $config->{$block}) 30 12 42
1042 # Make sure that the hash is not created implicitly
1043 30 $config->{$block} = $this->_hashref();
1044  
1045 30 ▶ if ($this->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"InterPolateVars"}) 9 21 30
1046 # inherit current __stack to new block
1047 9 $config->{$block}->{__stack} = $this->_copy($config->{__stack});
1048 }
1049 }
1050  
1051 42 ▶ if (ref($config->{$block}) eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $config->{$block} eq '') { } 1 41 42
elsif (ref $config->{$block} eq 'ARRAY') { } 2 39 41
elsif (exists $config->{$block}{$blockname}) { } 0 39 39
1052 1 croak "Config::General: Block <$block> already exists as scalar entry!\n";
1053 }
1054 elsif (ref($config->{$block}) eq 'ARRAY') {
1055 2 croak "Config::General: Cannot append named block <$block $blockname> to array of scalars!\n"
1056 ."Block <$block> or scalar '$block' occurs more than once.\n"
1057 ."Turn on -MergeDuplicateBlocks or make sure <$block> occurs only once in the config.\n";
1058 }
1059 elsif (exists $config->{$block}->{$blockname}) {
1060 # the named block already exists, make it an array
1061 0 ▶ if ($this->{MergeDuplicateBlocks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'MergeDuplicateBlocks'}) { } 0 0 0
1062 # just merge the new block with the same name as an existing one into
1063 # this one.
1064 0 $config->{$block}->{$blockname} = $this->_parse($config->{$block}->{$blockname}, \@newcontent);
1065 }
1066 else {
1067 0 ▶ if (! $this->{AllowMultiOptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->{'AllowMultiOptions'}) { } 0 0 0
1068 0 croak "Config::General: Named block \"<$block $blockname>\" occurs more than once (level: $this->{level}, chunk $chunk)!\n";
1069 }
1070 else { # preserve existing data
1071 0 my $savevalue = $config->{$block}->{$blockname};
1072 0 delete $config->{$block}->{$blockname};
1073 0 my @ar;
1074 0 ▶ if (ref $savevalue eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $savevalue eq 'ARRAY') { } 0 0 0
1075 0 push @ar, @{$savevalue}; # preserve array if any
1076 }
1077 else {
1078 0 push @ar, $savevalue;
1079 }
1080 0 push @ar, $this->_parse( $this->_hashref(), \@newcontent); # append it
1081 0 $config->{$block}->{$blockname} = \@ar;
1082 }
1083 }
1084 }
1085 else {
1086 # the first occurrence of this particular named block
1087 39 my $tmphash = $this->_hashref();
1088  
1089 39 ▶ if ($this->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"InterPolateVars"}) 12 27 39
1090 # inherit current __stack to new block
1091 12 $tmphash->{__stack} = $this->_copy($config->{__stack});
1092 }
1093  
1094 39 $config->{$block}->{$blockname} = $this->_parse($tmphash, \@newcontent);
1095 }
1096 }
1097 else {
1098 # standard block
1099 44 ▶ if (exists $config->{$block}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{$block}) { } 5 39 44
1100 5 ▶ if (ref($config->{$block}) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config->{$block} eq "") 1 4 5
1101 1 croak "Config::General: Cannot create hashref from <$block> because there is\n"
1102 ."already a scalar option '$block' with value '$config->{$block}'\n";
1103 }
1104  
1105 # the block already exists, make it an array
1106 4 ▶ if ($this->{MergeDuplicateBlocks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'MergeDuplicateBlocks'}) { } 2 2 4
1107 # just merge the new block with the same name as an existing one into
1108 # this one.
1109 2 $config->{$block} = $this->_parse($config->{$block}, \@newcontent);
1110 }
1111 else {
1112 2 ▶ if (! $this->{AllowMultiOptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->{'AllowMultiOptions'}) { } 0 2 2
1113 0 croak "Config::General: Block \"<$block>\" occurs more than once (level: $this->{level}, chunk $chunk)!\n";
1114 }
1115 else {
1116 2 my $savevalue = $config->{$block};
1117 2 delete $config->{$block};
1118 2 my @ar;
1119 2 ▶ if (ref $savevalue eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $savevalue eq 'ARRAY') { } 0 2 2
1120 0 push @ar, @{$savevalue};
1121 }
1122 else {
1123 2 push @ar, $savevalue;
1124 }
1125  
1126 # fixes rt#31529
1127 2 my $tmphash = $this->_hashref();
1128 2 ▶ if ($this->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"InterPolateVars"}) 0 2 2
1129 # inherit current __stack to new block
1130 0 $tmphash->{__stack} = $this->_copy($config->{__stack});
1131 }
1132  
1133 2 push @ar, $this->_parse( $tmphash, \@newcontent);
1134  
1135 2 $config->{$block} = \@ar;
1136 }
1137 }
1138 }
1139 else {
1140 # the first occurrence of this particular block
1141 39 my $tmphash = $this->_hashref();
1142  
1143 39 ▶ if ($this->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"InterPolateVars"}) 16 23 39
1144 # inherit current __stack to new block
1145 16 $tmphash->{__stack} = $this->_copy($config->{__stack});
1146 }
1147  
1148 39 $config->{$block} = $this->_parse($tmphash, \@newcontent);
1149 }
1150 }
1151 82 undef $blockname;
1152 82 undef $block;
1153 82 $this->{level} -= 1;
1154 82 next;
1155 }
1156 }
1157 else { # inside $block, just push onto new content stack
1158 158 push @newcontent, $_;
1159 }
1160 }
1161 135 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) 0 135 135
1162 # $block is still defined, which means, that it had
1163 # no matching endblock!
1164 0 croak "Config::General: Block \"<$block>\" has no EndBlock statement (level: $this->{level}, chunk $chunk)!\n";
1165 }
1166 135 return $config;
1167 }
1168  
1169  
1170 39 ▶sub _copy {
Subroutine _copy: called CC=2 SCAR=6.9
1171 #
1172 # copy the contents of one hash into another
1173 # to circumvent invalid references
1174 # fixes rt.cpan.org bug #35122
1175 39 my($this, $source) = @_;
1176 39 my %hash = ();
1177 39 ▶ while (my ($key, $value) = each %{$source}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$source;}) 404 39 443
1178 404 $hash{$key} = $value;
1179 }
1180 39 return \%hash;
1181 }
1182  
1183  
1184 250 ▶sub _parse_value {
Subroutine _parse_value: called CC=13 SCAR=25.7
1185 #
1186 # parse the value if value parsing is turned on
1187 # by either -AutoTrue and/or -FlagBits
1188 # otherwise just return the given value unchanged
1189 #
1190 250 my($this, $config, $option, $value) =@_;
1191  
1192 250 my $cont;
1193 250 ($cont, $option, $value) = $this->_hook('pre_parse_value', $option, $value);
1194 250 ▶ return $value if(!$cont);
Branch: 1 branch
BranchTrueFalseTotal
unless $cont 0 250 250
1195  
1196 # avoid "Use of uninitialized value"
1197 250 ▶ if (! defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 4 246 250
1198 # patch fix rt#54583
1199 # Return an input undefined value without trying transformations
1200 4 return $value;
1201 }
1202  
1203 246 ▶ if($this->{NormalizeValue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"NormalizeValue"}) 0 246 246
1204 0 $value = $this->{NormalizeValue}($value);
1205 }
1206  
1207 246 ▶ if ($this->{InterPolateVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"InterPolateVars"}) 73 173 246
1208 73 $value = $this->_interpolate($config, $option, $value);
1209 }
1210  
1211 # make true/false values to 1 or 0 (-AutoTrue)
1212 246 ▶ if ($this->{AutoTrue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"AutoTrue"}) 46 200 246
1213 46 ▶ if ($value =~ /$this->{AutoTrueFlags}->{true}/io) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /$this->{'AutoTrueFlags'}{'true'}/io) { } 7 39 46
elsif ($value =~ /$this->{'AutoTrueFlags'}{'false'}/io) { } 9 30 39
1214 7 $value = 1;
1215 }
1216 elsif ($value =~ /$this->{AutoTrueFlags}->{false}/io) {
1217 9 $value = 0;
1218 }
1219 }
1220  
1221 # assign predefined flags or undef for every flag | flag ... (-FlagBits)
1222 246 ▶ if ($this->{FlagBits}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"FlagBits"}) 35 211 246
1223 35 ▶ if (exists $this->{FlagBitsFlags}->{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"FlagBitsFlags"}{$option}) 2 33 35
1224 2 my %__flags = map { $_ => 1 } split /\s*\|\s*/, $value;
1225 2 foreach my $flag (keys %{$this->{FlagBitsFlags}->{$option}}) {
1226 6 ▶ if (exists $__flags{$flag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $__flags{$flag}) { } 5 1 6
1227 5 $__flags{$flag} = $this->{FlagBitsFlags}->{$option}->{$flag};
1228 }
1229 else {
1230 1 $__flags{$flag} = undef;
1231 }
1232 }
1233 2 $value = \%__flags;
1234 }
1235 }
1236  
1237 246 ▶ if (!$this->{NoEscape}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"NoEscape"}) 245 1 246
1238 # are there any escaped characters left? put them out as is
1239 245 $value =~ s/\\([\$\\\"#])/$1/g;
1240 }
1241  
1242 246 ($cont, $option, $value) = $this->_hook('post_parse_value', $option, $value);
1243  
1244 246 return $value;
1245 }
1246  
1247  
1248  
1249 733 ▶sub _hook {
Subroutine _hook: called CC=2 SCAR=9.2
1250 733 my ($this, $hook, @arguments) = @_;
1251 733 ▶ if(exists $this->{Plug}->{$hook}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"Plug"}{$hook}) 0 733 733
1252 0 my $sub = $this->{Plug}->{$hook};
1253 0 my @hooked = &$sub(@arguments);
1254 0 return @hooked;
1255 }
1256 733 return (1, @arguments);
1257 }
1258  
1259  
1260  
1261 0 ▶sub save {
Subroutine save: not called CC=4 SCAR=30.0
Pod undocumented
1262 #
1263 # this is the old version of save() whose API interface
1264 # has been changed. I'm very sorry 'bout this.
1265 #
1266 # I'll try to figure out, if it has been called correctly
1267 # and if yes, feed the call to Save(), otherwise croak.
1268 #
1269 0 my($this, $one, @two) = @_;
1270  
1271 0 ▶ if ( (@two && $one) && ( (scalar @two) % 2 == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@two and $one and scalar(@two) % 2 == 0) { } 0 0 0
Condition0%0/6
@two and $one and scalar(@two) % 2 == 0
!ll&&!rl&&r
000
@two and $one
!ll&&!rl&&r
000
MC/DC0%0/3
@two and $one and scalar(@two) % 2 == 0
@two $one scalar(@two) % 2 == 0
Truth table0%0/4
A and B and C
A= @twoB= $oneC= scalar(@two) % 2 == 0
ABCresult
0XX0
10X0
1100
1111
1272 # @two seems to be a hash
1273 0 my %h = @two;
1274 0 $this->save_file($one, \%h);
1275 }
1276 else {
1277 0 croak q(Config::General: The save() method is deprecated. Use the new save_file() method instead!);
1278 }
1279 0 return;
1280 }
1281  
1282  
1283 7 ▶sub save_file {
Subroutine save_file: called CC=8 SCAR=24.5
Pod documented
1284 #
1285 # save the config back to disk
1286 #
1287 7 my($this, $file, $config) = @_;
1288 7 my $fh;
1289 my $config_string;
1290  
1291 7 ▶ if (!$file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file) { } 0 7 7
1292 0 croak "Config::General: Filename is required!";
1293 }
1294 else {
1295 7 ▶ if ($this->{UTF8}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'UTF8'}) { } 0 7 7
1296 0 $fh = IO::File->new;
1297 0 ▶ open($fh, ">:utf8", $file)
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">:utf8", $file or croak("Config::General: Could not open $file in UTF8 mode!($!)\n") 0 0 0
1298 or croak "Config::General: Could not open $file in UTF8 mode!($!)\n";
1299 }
1300 else {
1301 7 ▶ $fh = IO::File->new( "$file", 'w')
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new("$file", "w") or croak("Config::General: Could not open $file!($!)\n") 7 0 7
1302 or croak "Config::General: Could not open $file!($!)\n";
1303 }
1304 7 ▶ if (!$config) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $config) { } 5 2 7
1305 5 ▶ if (exists $this->{config}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{'config'}) { } 5 0 5
1306 5 $config_string = $this->_store(0, $this->{config});
1307 }
1308 else {
1309 0 croak "Config::General: No config hash supplied which could be saved to disk!\n";
1310 }
1311 }
1312 else {
1313 2 $config_string = $this->_store(0, $config);
1314 }
1315  
1316 6 ▶ if ($config_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_string) { } 6 0 6
1317 6 print {$fh} $config_string;
1318 }
1319 else {
1320 # empty config for whatever reason, I don't care
1321 0 print {$fh} q();
1322 }
1323  
1324 6 close $fh;
1325 }
1326 6 return;
1327 }
1328  
1329  
1330  
1331 2 ▶sub save_string {
Subroutine save_string: called CC=4 SCAR=14.8
Pod documented
1332 #
1333 # return the saved config as a string
1334 #
1335 2 my($this, $config) = @_;
1336  
1337 2 ▶ if (!$config || ref($config) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $config or ref $config ne 'HASH') { } 1 1 2
Condition66%2/3
not $config or ref $config ne "HASH"
l!l&&r!l&&!r
101
MC/DC50%1/2
not $config or ref $config ne "HASH"
not $config ref $config ne "HASH"
Truth table66%2/3
not(A) or B
A= not $configB= ref $config ne "HASH"
ABresult
1X1
011
000
1338 1 ▶ if (exists $this->{config}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{'config'}) { } 1 0 1
1339 1 return $this->_store(0, $this->{config});
1340 }
1341 else {
1342 0 croak "Config::General: No config hash supplied which could be saved to disk!\n";
1343 }
1344 }
1345 else {
1346 1 return $this->_store(0, $config);
1347 }
1348 0 return;
1349 }
1350  
1351  
1352  
1353 32 ▶sub _store {
Subroutine _store: called CC=12 SCAR=25.0
1354 #
1355 # internal sub for saving a block
1356 #
1357 32 my($this, $level, $config) = @_;
1358 32 local $_;
1359 32 my $indent = q( ) x $level;
1360  
1361 32 my $config_string = q();
1362  
1363 32 ▶ foreach my $entry ( $this->{SaveSorted} ? sort keys %$config : keys %$config ) {
Branch: 1 branch
BranchTrueFalseTotal
$this->{'SaveSorted'} ? : 15 17 32
1364 # fix rt#104548
1365 94 ▶ if ($entry =~ /[<>\n\r]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /[<>\n\r]/) 1 93 94
1366 1 croak "Config::General: current key contains invalid characters: $entry!\n";
1367 }
1368  
1369 93 ▶ if (ref($config->{$entry}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $config->{$entry} eq 'ARRAY') { } 5 88 93
elsif (ref $config->{$entry} eq 'HASH') { } 21 67 88
1370 5 ▶ if( $this->{ForceArray} && scalar @{$config->{$entry}} == 1 && ! ref($config->{$entry}->[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'ForceArray'} and scalar @{$config->{$entry};} == 1 and not ref $config->{$entry}[0]) { } 1 4 5
Condition66%4/6
$this->{"ForceArray"} and scalar @{$config->{$entry};} == 1 and not ref $config->{$entry}[0]
!ll&&!rl&&r
401
$this->{"ForceArray"} and scalar @{$config->{$entry};} == 1
!ll&&!rl&&r
401
MC/DC33%1/3
$this->{"ForceArray"} and scalar @{$config->{$entry};} == 1 and not ref $config->{$entry}[0]
$this->{"ForceArray"} scalar @{$config->{$entry};} == 1 not ref $config->{$entry}[0]
Truth table50%2/4
A and B and not(C)
A= $this->{"ForceArray"}B= scalar @{$config->{$entry};} == 1C= not ref $config->{$entry}[0]
ABCresult
0XX0
10X0
1100
1111
1371 # a single value array forced to stay as array
1372 1 $config_string .= $this->_write_scalar($level, $entry, '[' . $config->{$entry}->[0] . ']');
1373 }
1374 else {
1375 0 ▶ foreach my $line ( $this->{SaveSorted} ? sort @{$config->{$entry}} : @{$config->{$entry}} ) {
Branch: 1 branch
BranchTrueFalseTotal
$this->{'SaveSorted'} ? : 0 4 4
1376 10 ▶ if (ref($line) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $line eq 'HASH') { } 2 8 10
1377 2 $config_string .= $this->_write_hash($level, $entry, $line);
1378 }
1379 else {
1380 8 $config_string .= $this->_write_scalar($level, $entry, $line);
1381 }
1382 }
1383 }
1384 }
1385 elsif (ref($config->{$entry}) eq 'HASH') {
1386 21 $config_string .= $this->_write_hash($level, $entry, $config->{$entry});
1387 }
1388 else {
1389 67 $config_string .= $this->_write_scalar($level, $entry, $config->{$entry});
1390 }
1391 }
1392  
1393 31 return $config_string;
1394 }
1395  
1396  
1397 76 ▶sub _write_scalar {
Subroutine _write_scalar: called CC=11 SCAR=24.3
1398 #
1399 # internal sub, which writes a scalar
1400 # it returns it, in fact
1401 #
1402 76 my($this, $level, $entry, $line) = @_;
1403  
1404 76 my $indent = q( ) x $level;
1405  
1406 76 my $config_string;
1407  
1408 # patch fix rt#54583
1409 76 ▶ if ( ! defined $line ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $line) { } 0 76 76
elsif ($line =~ /\n/ or $line =~ /\\$/) { } 3 73 76
1410 0 $config_string .= $indent . $entry . "\n";
1411 }
1412 ▶ elsif ($line =~ /\n/ || $line =~ /\\$/) {
Condition66%2/3
$line =~ /\n/ or $line =~ /\\$/
l!l&&r!l&&!r
3073
MC/DC50%1/2
$line =~ /\n/ or $line =~ /\\$/
$line =~ /\n/ $line =~ /\\$/
Truth table66%2/3
A or B
A= $line =~ /\n/B= $line =~ /\\$/
ABresult
1X1
011
000
1413 # it is a here doc
1414 3 my $delimiter;
1415 3 my $tmplimiter = 'EOF';
1416 3 ▶ while (!$delimiter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($delimiter) 3 3 6
1417 # create a unique here-doc identifier
1418 3 ▶ if ($line =~ /$tmplimiter/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$tmplimiter/s) { } 0 3 3
1419 0 $tmplimiter .= '%';
1420 }
1421 else {
1422 3 $delimiter = $tmplimiter;
1423 }
1424 }
1425 3 my @lines = split /\n/, $line;
1426 3 $config_string .= $indent . $entry . $this->{StoreDelimiter} . "<<$delimiter\n";
1427 3 foreach (@lines) {
1428 8 $config_string .= $indent . $_ . "\n";
1429 }
1430 3 $config_string .= $indent . "$delimiter\n";
1431 }
1432 else {
1433 # a simple stupid scalar entry
1434  
1435 73 ▶ if (!$this->{NoEscape}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"NoEscape"}) 73 0 73
1436 # re-escape contained $ or # or \ chars
1437 73 $line =~ s/([#\$\\\"])/\\$1/g;
1438 }
1439  
1440 73 ▶ if ($line =~ /^\s/ || $line =~ /\s$/ || $this->{AlwaysQuoteOutput}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s/ or $line =~ /\s$/ or $this->{"AlwaysQuoteOutput"}) 3 70 73
Condition100%6/6
$line =~ /^\s/ or $line =~ /\s$/ or $this->{"AlwaysQuoteOutput"}
l!l&&r!l&&!r
2170
$line =~ /^\s/ or $line =~ /\s$/
l!l&&r!l&&!r
1171
MC/DC100%3/3
$line =~ /^\s/ or $line =~ /\s$/ or $this->{"AlwaysQuoteOutput"}
$line =~ /^\s/ $line =~ /\s$/ $this->{"AlwaysQuoteOutput"}
Truth table100%4/4
A or B or C
A= $line =~ /^\s/B= $line =~ /\s$/C= $this->{"AlwaysQuoteOutput"}
ABCresult
1XX1
01X1
0011
0000
1441 # quote lines containing whitespace
1442 3 $line = "\"$line\"";
1443 }
1444  
1445 73 $config_string .= $indent . $entry . $this->{StoreDelimiter} . $line . "\n";
1446 }
1447  
1448 76 return $config_string;
1449 }
1450  
1451 23 ▶sub _write_hash {
Subroutine _write_hash: called CC=4 SCAR=13.9
1452 #
1453 # internal sub, which writes a hash (block)
1454 # it returns it, in fact
1455 #
1456 23 my($this, $level, $entry, $line) = @_;
1457  
1458 23 my $indent = q( ) x $level;
1459 23 my $config_string;
1460  
1461 23 ▶ if ($entry =~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\s/) 0 23 23
1462 # quote the entry if it contains whitespaces
1463 0 $entry = q(") . $entry . q(");
1464 }
1465  
1466 # check if the next level key points to a hash and is the only one
1467 # in this case put out a named block
1468 # fixes rt.77667
1469 23 my $num = scalar keys %{$line};
1470 23 ▶ if($num == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num == 1) 17 6 23
1471 17 my $key = (keys %{$line})[0];
1472 17 ▶ if(ref($line->{$key}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $line->{$key} eq "HASH") 11 6 17
1473 11 $config_string .= $indent . qq(<$entry $key>\n);
1474 11 $config_string .= $this->_store($level + 1, $line->{$key});
1475 11 $config_string .= $indent . qq(</) . $entry . ">\n";
1476 11 return $config_string;
1477 }
1478 }
1479
1480 12 $config_string .= $indent . q(<) . $entry . ">\n";
1481 12 $config_string .= $this->_store($level + 1, $line);
1482 12 $config_string .= $indent . q(</) . $entry . ">\n";
1483  
1484 12 return $config_string
1485 }
1486  
1487  
1488 155 ▶sub _hashref {
Subroutine _hashref: called CC=3 SCAR=11.1
1489 #
1490 # return a probably tied new empty hash ref
1491 #
1492 155 my($this) = @_;
1493 155 ▶ if ($this->{Tie}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'Tie'}) { } 1 154 155
1494 1 eval {
1495 1 eval qq{require $this->{Tie}};
1496 };
1497 1 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config::General::EVAL_ERROR) 0 1 1
1498 0 croak q(Config::General: Could not create a tied hash of type: ) . $this->{Tie} . q(: ) . $EVAL_ERROR;
1499 }
1500 1 my %hash;
1501 1 tie %hash, $this->{Tie};
1502 1 return \%hash;
1503 }
1504 else {
1505 154 return {};
1506 }
1507 }
1508  
1509 45 ▶sub _validate_input_hash {
Subroutine _validate_input_hash: called CC=12 SCAR=25.0
1510 #
1511 # Checks if a given input hash contains invalid keys or values.
1512 # Fixes https://codeberg.org/scip/Config-General/issues/9.
1513 #
1514 45 my($this, $hash) = @_;
1515  
1516 45 ▶ if (ref($hash) eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash eq '') { } 8 37 45
elsif (ref $hash eq 'ARRAY') { } 0 37 37
1517 8 return $this->_validate_input_hash_value($hash);
1518 }
1519 elsif (ref($hash) eq 'ARRAY') {
1520 0 foreach my $sub (@{$hash}) {
1521 0 return $this->_validate_input_hash($sub);
1522 }
1523 }
1524
1525 37 foreach my $key (keys %{$hash}) {
1526 50 ▶ if (! $this->_validate_input_hash_value($key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->_validate_input_hash_value($key)) 6 44 50
1527 6 return 0;
1528 }
1529  
1530 44 ▶ if (ref($hash->{$key}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash->{$key} eq 'ARRAY') { } 3 41 44
elsif (ref $hash->{$key} eq 'HASH') { } 11 30 41
1531 3 foreach my $sub (@{$hash->{$key}}) {
1532 7 ▶ if (! $this->_validate_input_hash($sub)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->_validate_input_hash($sub)) 1 6 7
1533 1 return 0;
1534 }
1535 }
1536 }
1537 elsif(ref($hash->{$key}) eq 'HASH') {
1538 11 ▶ if (! $this->_validate_input_hash($hash->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->_validate_input_hash($hash->{$key})) 3 8 11
1539 3 return 0;
1540 }
1541 }
1542 else {
1543 30 ▶ if (! $this->_validate_input_hash_value($hash->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->_validate_input_hash_value($hash->{$key})) 7 23 30
1544 7 return 0;
1545 }
1546 }
1547 }
1548  
1549 20 return 1;
1550 }
1551  
1552 88 ▶sub _validate_input_hash_value {
Subroutine _validate_input_hash_value: called CC=2 SCAR=6.9
1553 #
1554 # Checks a given key or value against a deny regex
1555 88 my ($this, $value) = @_;
1556  
1557 88 ▶ if ($value =~ /(<<|>>|\*\/|\/\*|#)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ m[(<<|>>|\*/|/\*|#)]) 14 74 88
1558 14 return 0;
1559 }
1560  
1561 74 return 1;
1562 }
1563  
1564  
1565 #
1566 # Procedural interface
1567 #
1568 3 ▶sub ParseConfig {
Subroutine ParseConfig: called CC=1 SCAR=0.0
Pod documented
1569 #
1570 # @_ may contain everything which is allowed for new()
1571 #
1572 3 return (new Config::General(@_))->getall();
1573 }
1574  
1575 1 ▶sub SaveConfig {
Subroutine SaveConfig: called CC=4 SCAR=16.6
Pod documented
1576 #
1577 # 2 parameters are required, filename and hash ref
1578 #
1579 1 my ($file, $hash) = @_;
1580  
1581 1 ▶ if (!$file || !$hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $file && $hash) { } 0 1 1
Condition33%1/3
$file && $hash
!ll&&!rl&&r
001
MC/DC0%0/2
$file && $hash
$file $hash
Truth table33%1/3
A && B
A= $fileB= $hash
ABresult
0X0
100
111
1582 0 croak q{Config::General::SaveConfig(): filename and hash argument required.};
1583 }
1584 else {
1585 1 ▶ if (ref($hash) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne 'HASH') { } 0 1 1
1586 0 croak q(Config::General::SaveConfig() The second parameter must be a reference to a hash!);
1587 }
1588 else {
1589 1 (new Config::General(-ConfigHash => $hash))->save_file($file);
1590 }
1591 }
1592 1 return;
1593 }
1594  
1595 0 ▶sub SaveConfigString {
Subroutine SaveConfigString: not called CC=3 SCAR=24.8
Pod documented
1596 #
1597 # same as SaveConfig, but return the config,
1598 # instead of saving it
1599 #
1600 0 my ($hash) = @_;
1601  
1602 0 ▶ if (!$hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hash) { } 0 0 0
1603 0 croak q{Config::General::SaveConfigString(): Hash argument required.};
1604 }
1605 else {
1606 0 ▶ if (ref($hash) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash ne 'HASH') { } 0 0 0
1607 0 croak q(Config::General::SaveConfigString() The parameter must be a reference to a hash!);
1608 }
1609 else {
1610 0 return (new Config::General(-ConfigHash => $hash))->save_string();
1611 }
1612 }
1613 0 return;
1614 }
1615  
1616  
1617  
1618 # keep this one
1619 1 1;
1620 __END__
↑ Summary General/Extended.pm »