blib/lib/Config/Std.pm

filter: Statementstmt 91.9% 274 / 298 Branchbran 74.5% 76 / 102 Conditioncond 55.3% 36 / 65 MC/DCmcdc 26.3% 10 / 38 Subroutinesub 82.5% 33 / 40 Podpod n/a totaltotal 79.0% 429 / 543 CC 89 SCAR 25.3
CC
89
Cov
83%
extend
33.0
write_config
23.4
_load_config_for
20.5
CRAP 12.6
↑ Summary
1 package Config::Std;
2  
3 10 our $VERSION = '0.903';
4  
5 10 ▶use 5.007_003; # Testing with 5.8.1 since that's cpanm minimum :-)
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 my %global_def_sep;
10 my %global_inter_gap;
11  
12 9 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
13 9 my ($package, $opt_ref) = @_;
14 9 my $caller = caller();
15 9 $global_def_sep{$caller} = $opt_ref->{def_sep};
16 9 $global_inter_gap{$caller} = $opt_ref->{def_gap};
17 9 for my $sub_name (qw( read_config write_config )) {
18 18 ▶ $opt_ref->{$sub_name} ||= $sub_name;
Condition66%2/3
$opt_ref->{$sub_name} ||= $sub_name
l!l&&r!l&&!r
1170
MC/DC0%0/2
$opt_ref->{$sub_name} ||= $sub_name
$opt_ref->{$sub_name} $sub_name
Truth table66%2/3
A ||= B
A= $opt_ref->{$sub_name}B= $sub_name
ABresult
1X1
011
000
19 }
20 10 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 *{$caller.'::'.$opt_ref->{read_config}} = \&Config::Std::Hash::read_config;
22 9 *{$caller.'::'.$opt_ref->{write_config}} = \&Config::Std::Hash::write_config;
23 }
24  
25 package Config::Std::Gap;
26 10 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 {
28 39 ▶ sub serialize { return "\n" }
Subroutine serialize: called CC=1 SCAR=0.0
29 26 ▶ sub update {}
Subroutine update: called CC=1 SCAR=0.0
30 0 ▶ sub extend {}
Subroutine extend: not called CC=1 SCAR=0.0
31 42 ▶ sub copy_to {}
Subroutine copy_to: called CC=1 SCAR=0.0
32 }
33  
34 10 package Config::Std::Comment;
35 10 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 {
37 10 my %text_of : ATTR( :init_arg<text> );
38  
39 0 ▶ sub serialize {
Subroutine serialize: not called CC=1 SCAR=6.9
40 0 my ($self) = @_;
41 0 return $text_of{ident $self};
42 }
43  
44 0 ▶ sub append_comment {
Subroutine append_comment: not called CC=1 SCAR=6.9
45 0 my ($self, $new_text) = @_;
46 0 $text_of{ident $self} .= $new_text;
47 }
48  
49 0 ▶ sub update {}
Subroutine update: not called CC=1 SCAR=0.0
50 0 ▶ sub extend {}
Subroutine extend: not called CC=1 SCAR=0.0
51 0 ▶ sub copy_to {}
Subroutine copy_to: not called CC=1 SCAR=0.0
52 }
53  
54 10 package Config::Std::Keyval;
55 10 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 {
57 10 my %key_of : ATTR( :init_arg<key> :get<key> );
58 10 my %vals_of : ATTR;
59 10 my %deleted_of : ATTR;
60  
61 53 ▶ sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
62 53 my ($self, $ident, $arg_ref) = @_;
63  
64 53 $vals_of{$ident} = [ { %{$arg_ref} } ];
65 }
66  
67 10 my %SEPARATOR = ( ':' => ': ', '=' => ' = ' );
68  
69 10 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 36 ▶ sub serialize {
Subroutine serialize: called CC=9 SCAR=22.1
72 36 my ($self, $def_sep, $block_name) = @_;
73 36 my $ident = ident $self;
74  
75 36 ▶ return "" if $deleted_of{$ident};
Branch: 1 branch
BranchTrueFalseTotal
if $deleted_of{$ident} 0 36 36
76  
77 36 my ($key, $vals) = ($key_of{$ident}, $vals_of{$ident});
78  
79 36 my $keyspace = q{ } x length($key);
80  
81 36 my $serialization = q{};
82  
83 36 for my $n (0..$#{$vals}) {
84 47 my ($val,$sep,$comm) = @{$vals->[$n]}{qw(val sep comm)};
85  
86 47 my $val_type = ref $val;
87 47 ▶ croak qq{Can't save \L$val_type\E ref as value for key {'$block_name'}{'$key'} (only scalars or array refs)}
Branch: 1 branch
BranchTrueFalseTotal
if $val_type and $val_type ne "ARRAY" 0 47 47
Condition66%2/3
$val_type and $val_type ne "ARRAY"
!ll&&!rl&&r
4610
MC/DC0%0/2
$val_type and $val_type ne "ARRAY"
$val_type $val_type ne "ARRAY"
Truth table66%2/3
A and B
A= $val_typeB= $val_type ne "ARRAY"
ABresult
0X0
100
111
88 if $val_type && $val_type ne 'ARRAY';
89  
90 47 ▶ $sep = $SEPARATOR{$sep || $def_sep};
Condition66%2/3
$sep || $def_sep
l!l&&r!l&&!r
34130
MC/DC0%0/2
$sep || $def_sep
$sep $def_sep
Truth table66%2/3
A || B
A= $sepB= $def_sep
ABresult
1X1
011
000
91  
92 47 ▶ my @vals = $val_type eq 'ARRAY' ? @{$val} : $val;
Branch: 1 branch
BranchTrueFalseTotal
$val_type eq 'ARRAY' ? : 1 46 47
93 47 s/ (?!\Z) \n /\n$keyspace$sep/gxms for @vals;
94  
95 47 ▶ $serialization .= $comm || q{};
Condition100%2/2
$comm || ''
l!l
443
MC/DC100%1/1
$comm || ''
$comm
Truth table100%2/2
A
A= $comm
Aresult
11
00
96  
97 47 $serialization .= join q{}, map {"$key$sep$_\n"} @vals;
98 }
99  
100 36 return $serialization;
101 }
102  
103 25 ▶ sub update {
Subroutine update: called CC=4 SCAR=13.9
104 25 my ($self, $hash_ref, $updated_ref) = @_;
105 25 my $ident = ident $self;
106  
107 25 my $key = $key_of{$ident};
108  
109 25 ▶ if (!exists $hash_ref->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $hash_ref->{$key}) { } 0 25 25
110 0 $deleted_of{$ident} = 1;
111 }
112 else {
113 25 my $val = $hash_ref->{$key};
114 25 ▶ my @newvals = ref $val eq 'ARRAY' ? @{$val} : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2 23 25
115 25 for my $n (0..$#newvals) {
116 36 $vals_of{$ident}[$n]{val} = $newvals[$n];
117 }
118 25 splice @{$vals_of{$ident}}, scalar @newvals;
119 }
120  
121 25 $updated_ref->{$key} = 1;
122  
123 25 return 1;
124 }
125  
126 41 ▶ sub copy_to {
Subroutine copy_to: called CC=2 SCAR=6.9
127 41 my ($self, $hash_ref) = @_;
128 41 my $ident = ident $self;
129 41 my @vals = map $_->{val}, @{$vals_of{$ident}};
130 41 ▶ $hash_ref->{$key_of{$ident}} = @vals > 1 ? \@vals : $vals[0];
Branch: 1 branch
BranchTrueFalseTotal
@vals > 1 ? : 4 37 41
131 }
132  
133 17 ▶ sub multivalue {
Subroutine multivalue: called CC=1 SCAR=0.0
134 17 my ($self, $sep, $val, $comm) = @_;
135 17 push @{$vals_of{ident $self}}, {val=>$val, sep=>$sep, comm=>$comm};
136 }
137 }
138  
139 10 package Config::Std::Block;
140 10 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 {
142 10 my %name_of : ATTR( :init_arg<name> :get<name> default => '' );
143 10 my %sep_count_of : ATTR;
144 10 my %precomm_of : ATTR( :init_arg<precomm> default => '' );
145 10 my %parcomm_of : ATTR( :init_arg<parcomm> default => '' );
146 10 my %components_of : ATTR;
147 10 my %deleted_of : ATTR;
148 10 my %seen : ATTR;
149 10 my %is_first : ATTR( :init_arg<first> default => '' );
150  
151 24 ▶ sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
152 24 my ($self, $ident) = @_;
153 24 @{$sep_count_of{$ident}}{':','='} = (0,0);
154 24 $components_of{$ident} = [];
155 24 $seen{$ident} = {};
156 }
157  
158 18 ▶ sub copy_to {
Subroutine copy_to: called CC=3 SCAR=11.0
159 18 my ($self, $hash_ref) = @_;
160 18 my $ident = ident $self;
161  
162 18 ▶ my $keyvals = $hash_ref->{$name_of{$ident}} ||= {};
Condition50%1/2
$$hash_ref{$name_of{$ident}} ||= {}
l!l
018
MC/DC0%0/1
$$hash_ref{$name_of{$ident}} ||= {}
$$hash_ref{$name_of{$ident}}
Truth table50%1/2
A
A= $$hash_ref{$name_of{$ident}}
Aresult
11
00
163  
164 18 for my $comp ( @{$components_of{$ident}} ) {
165 83 $comp->copy_to($keyvals);
166 }
167  
168 18 $hash_ref->{$name_of{$ident}} = $keyvals;
169 }
170  
171 18 ▶ sub serialize {
Subroutine serialize: called CC=11 SCAR=24.6
172 18 my ($self, $first, $caller, $post_gap, $inter_gap) = @_;
173 18 my $ident = ident $self;
174  
175 18 ▶ return q{} if $deleted_of{$ident};
Branch: 1 branch
BranchTrueFalseTotal
if $deleted_of{$ident} 2 16 18
176  
177 16 ▶ my $is_anon = $first && length($name_of{$ident}) == 0;
Condition66%2/3
$first && length $name_of{$ident} == 0
!ll&&!rl&&r
1105
MC/DC50%1/2
$first && length $name_of{$ident} == 0
$first length $name_of{$ident} == 0
Truth table66%2/3
A && B
A= $firstB= length $name_of{$ident} == 0
ABresult
0X0
100
111
178  
179 16 my $serialization = q{};
180 16 ▶ if (!$is_anon) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_anon) 11 5 16
181 $serialization = ($precomm_of{$ident} || q{})
182 . "[$name_of{$ident}]"
183 11 ▶ . (defined $parcomm_of{$ident}?$parcomm_of{$ident}:q{})
Branch: 1 branch
BranchTrueFalseTotal
defined $parcomm_of{$ident} ? : 4 7 11
Condition50%1/2
$precomm_of{$ident} || ''
l!l
011
MC/DC0%0/1
$precomm_of{$ident} || ''
$precomm_of{$ident}
Truth table50%1/2
A
A= $precomm_of{$ident}
Aresult
11
00
184 . "\n";
185 }
186  
187 16 my $gds = $global_def_sep{$caller};
188 my $def_sep
189 = defined $gds ? $gds
190 16 ▶ : $sep_count_of{$ident}{':'} >= $sep_count_of{$ident}{'='} ? ':'
Branch: 2 branches
BranchTrueFalseTotal
defined $gds ? : 5 11 16
$sep_count_of{$ident}{':'} >= $sep_count_of{$ident}{'='} ? : 11 0 11
191 : '='
192 ;
193  
194 16 ▶ $self->ensure_gap() if $inter_gap && !$is_anon;
Branch: 1 branch
BranchTrueFalseTotal
if $inter_gap and not $is_anon 0 16 16
Condition33%1/3
$inter_gap and not $is_anon
!ll&&!rl&&r
1600
MC/DC0%0/2
$inter_gap and not $is_anon
$inter_gap not $is_anon
Truth table33%1/3
A and not(B)
A= $inter_gapB= not $is_anon
ABresult
0X0
100
111
195  
196 16 for my $comp ( @{$components_of{$ident}} ) {
197 75 $serialization .= $comp->serialize($def_sep, $name_of{$ident});
198 }
199  
200 16 return $serialization;
201 }
202  
203 13 ▶ sub update {
Subroutine update: called CC=4 SCAR=13.9
204 13 my ($self, $hash_ref, $updated_ref) = @_;
205 13 my $ident = ident $self;
206  
207 13 ▶ if (!defined $hash_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash_ref) 2 11 13
208 2 $deleted_of{$ident} = 1;
209 2 return;
210 }
211  
212 11 for my $comp ( @{$components_of{$ident}} ) {
213 51 ▶ $comp->update($hash_ref, $updated_ref) or next;
Branch: 1 branch
BranchTrueFalseTotal
$comp->update($hash_ref, $updated_ref) or next 25 26 51
214 }
215 }
216  
217 13 ▶ sub extend {
Subroutine extend: called CC=8 SCAR=35.0
218 13 my ($self, $hash_ref, $updated_ref, $post_gap, $inter_gap) = @_;
219  
220 # Only the first occurrence of a block has new keys added...
221 13 ▶ return unless $is_first{ident $self};
Branch: 1 branch
BranchTrueFalseTotal
unless $is_first{builtin::refaddr($self)} 0 13 13
222  
223 13 my $first = 1;
224 # RT 85956
225 13 for my $key ( sort grep {!$updated_ref->{$_}} keys %{$hash_ref}) {
226 0 my $value = $hash_ref->{$key};
227 0 ▶ my $separate = ref $value || $value =~ m/\n./xms;
Condition0%0/3
ref $value || $value =~ /\n./msx
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $value || $value =~ /\n./msx
ref $value $value =~ /\n./msx
Truth table0%0/3
A || B
A= ref $valueB= $value =~ /\n./msx
ABresult
1X1
011
000
228 0 ▶ $self->ensure_gap() if ($first ? $post_gap : $inter_gap)
Branch: 2 branches
BranchTrueFalseTotal
if $first ? $post_gap : $inter_gap or $separate 0 0 0
$first ? : 0 0 0
Condition0%0/3
$first ? $post_gap : $inter_gap or $separate
l!l&&r!l&&!r
000
MC/DC0%0/2
$first ? $post_gap : $inter_gap or $separate
$first ? $post_gap : $inter_gap $separate
Truth table0%0/3
A or B
A= $first ? $post_gap : $inter_gapB= $separate
ABresult
1X1
011
000
229 || $separate;
230 0 $self->add_keyval($key, undef, $hash_ref->{$key});
231 0 ▶ $self->add_gap() if $separate;
Branch: 1 branch
BranchTrueFalseTotal
if $separate 0 0 0
232 0 $first = 0;
233 }
234 }
235  
236 13 ▶ sub ensure_gap {
Subroutine ensure_gap: called CC=3 SCAR=11.0
237 13 my ($self) = @_;
238 13 my $comp_ref = $components_of{ident $self};
239 13 ▶ return if @{$comp_ref} && $comp_ref->[-1]->isa('Config::Std::Gap');
Branch: 1 branch
BranchTrueFalseTotal
if @{$comp_ref;} and $comp_ref->[-1]->isa("Config::Std::Gap") 1 12 13
Condition100%3/3
@{$comp_ref;} and $comp_ref->[-1]->isa("Config::Std::Gap")
!ll&&!rl&&r
661
MC/DC100%2/2
@{$comp_ref;} and $comp_ref->[-1]->isa("Config::Std::Gap")
@{$comp_ref;} $comp_ref->[-1]->isa("Config::Std::Gap")
Truth table100%3/3
A and B
A= @{$comp_ref;}B= $comp_ref->[-1]->isa("Config::Std::Gap")
ABresult
0X0
100
111
240 12 push @{$comp_ref}, Config::Std::Gap->new();
241 }
242  
243 44 ▶ sub add_gap {
Subroutine add_gap: called CC=1 SCAR=0.0
244 44 my ($self) = @_;
245 44 push @{$components_of{ident $self}}, Config::Std::Gap->new();
246 }
247  
248 0 ▶ sub add_comment {
Subroutine add_comment: not called CC=4 SCAR=30.0
249 0 my ($self, $text) = @_;
250 0 my $comp_ref = $components_of{ident $self};
251 0 ▶ if ($comp_ref && @{$comp_ref} && $comp_ref->[-1]->isa('Config::Std::Comment') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp_ref and @{$comp_ref;} and $comp_ref->[-1]->isa('Config::Std::Comment')) { } 0 0 0
Condition0%0/6
$comp_ref and @{$comp_ref;} and $comp_ref->[-1]->isa("Config::Std::Comment")
!ll&&!rl&&r
000
$comp_ref and @{$comp_ref;}
!ll&&!rl&&r
000
MC/DC0%0/3
$comp_ref and @{$comp_ref;} and $comp_ref->[-1]->isa("Config::Std::Comment")
$comp_ref @{$comp_ref;} $comp_ref->[-1]->isa("Config::Std::Comment")
Truth table0%0/4
A and B and C
A= $comp_refB= @{$comp_ref;}C= $comp_ref->[-1]->isa("Config::Std::Comment")
ABCresult
0XX0
10X0
1100
1111
252 0 $comp_ref->[-1]->append_comment($text);
253 }
254 else {
255 0 push @{$comp_ref}, Config::Std::Comment->new({text=>$text});
256 }
257 }
258  
259 70 ▶ sub add_keyval {
Subroutine add_keyval: called CC=3 SCAR=11.0
260 70 my ($self, $key, $sep, $val, $comm) = @_;
261 70 my $ident = ident $self;
262  
263 70 ▶ $sep_count_of{$ident}{$sep}++ if $sep;
Branch: 1 branch
BranchTrueFalseTotal
if $sep 58 12 70
264  
265 70 my $seen = $seen{$ident};
266  
267 70 ▶ if ($seen->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen->{$key}) 17 53 70
268 17 $seen->{$key}->multivalue($sep, $val, $comm);
269 17 return;
270 }
271  
272 53 my $keyval
273 = Config::Std::Keyval->new({key=>$key, sep=>$sep, val=>$val, comm=>$comm});
274 53 push @{$components_of{$ident}}, $keyval;
275 53 $seen->{$key} = $keyval;
276 }
277 }
278  
279 10 package Config::Std::Hash;
280 10 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
281 {
282  
283 10 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
284 10 ▶ use Fcntl ':flock'; # import LOCK_* constants
Subroutine BEGIN: called CC=1 SCAR=0.0
285  
286 10 my %post_section_gap_for :ATTR;
287 10 my %array_rep_for :ATTR;
288 10 my %filename_for :ATTR;
289  
290 8 ▶ sub write_config (\[%$];$) {
Subroutine write_config: called CC=23 SCAR=31.5
291 8 my ($hash_ref, $filename) = @_;
292 8 ▶ $hash_ref = ${$hash_ref} if ref $hash_ref eq 'REF';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash_ref eq "REF" 1 7 8
293  
294 8 ▶ $filename = $filename_for{$hash_ref} if @_<2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 4 4 8
295  
296 8 ▶ croak "Missing filename for call to write_config()"
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 8 8
297 unless $filename;
298  
299 8 my $caller = caller;
300  
301 my $inter_gap
302 8 ▶ = exists $global_inter_gap{$caller} ? $global_inter_gap{$caller}
Branch: 1 branch
BranchTrueFalseTotal
exists $global_inter_gap{$caller} ? : 8 0 8
303 : 1;
304 my $post_gap
305 = $post_section_gap_for{$hash_ref}
306 8 ▶ || (defined $global_inter_gap{$caller} ? $global_inter_gap{$caller}
Branch: 1 branch
BranchTrueFalseTotal
defined $global_inter_gap{$caller} ? : 0 4 4
Condition66%2/3
$post_section_gap_for{$hash_ref} || (defined $global_inter_gap{$caller} ? $global_inter_gap{$caller} : 1)
l!l&&r!l&&!r
440
MC/DC0%0/2
$post_section_gap_for{$hash_ref} || (defined $global_inter_gap{$caller} ? $global_inter_gap{$caller} : 1)
$post_section_gap_for{$hash_ref} (defined $global_inter_gap{$caller} ? $global_inter_gap{$caller} : 1)
Truth table66%2/3
A || B
A= $post_section_gap_for{$hash_ref}B= (defined $global_inter_gap{$caller} ? $global_inter_gap{$caller} : 1)
ABresult
1X1
011
000
307 : 1
308 );
309  
310 # Update existing keyvals in each block...
311 8 my %updated;
312 8 for my $block ( @{$array_rep_for{$hash_ref}} ) {
313 13 my $block_name = $block->get_name();
314 13 ▶ $block->update($hash_ref->{$block_name}, $updated{$block_name}||={});
Condition50%1/2
$updated{$block_name} ||= {}
l!l
013
MC/DC0%0/1
$updated{$block_name} ||= {}
$updated{$block_name}
Truth table50%1/2
A
A= $updated{$block_name}
Aresult
11
00
315 }
316  
317 # Add new keyvals to the first section of block...
318 8 for my $block ( @{$array_rep_for{$hash_ref}} ) {
319 13 my $block_name = $block->get_name();
320 13 $block->extend($hash_ref->{$block_name}, $updated{$block_name},
321 $post_gap, $inter_gap
322 );
323 }
324  
325 # Add new blocks at the end...
326 8 for my $block_name ( sort grep {!$updated{$_}} keys %{$hash_ref} ) {
327 6 my $block = Config::Std::Block->new({name=>$block_name});
328 6 my $subhash = $hash_ref->{$block_name};
329 6 my $first = 1;
330 # RT 85956
331 6 for my $key ( sort keys %{$subhash} ) {
332 13 ▶ if (!defined $subhash->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subhash->{$key}) 1 12 13
333 1 croak "Can't save undefined value for key {'$block_name'}{'$key'} (only scalars or array refs)";
334 }
335 12 my $value = $subhash->{$key};
336 12 ▶ my $separate = ref $value || $value =~ m/\n./xms;
Condition100%3/3
ref $value || $value =~ /\n./msx
l!l&&r!l&&!r
1110
MC/DC100%2/2
ref $value || $value =~ /\n./msx
ref $value $value =~ /\n./msx
Truth table100%3/3
A || B
A= ref $valueB= $value =~ /\n./msx
ABresult
1X1
011
000
337 12 ▶ $block->ensure_gap() if ($first ? $post_gap : $inter_gap)
Branch: 2 branches
BranchTrueFalseTotal
if $first ? $post_gap : $inter_gap or $separate 8 4 12
$first ? : 6 6 12
Condition100%3/3
$first ? $post_gap : $inter_gap or $separate
l!l&&r!l&&!r
624
MC/DC100%2/2
$first ? $post_gap : $inter_gap or $separate
$first ? $post_gap : $inter_gap $separate
Truth table100%3/3
A or B
A= $first ? $post_gap : $inter_gapB= $separate
ABresult
1X1
011
000
338 || $separate;
339 12 $block->add_keyval($key, undef, $value);
340 12 ▶ $block->add_gap() if $separate;
Branch: 1 branch
BranchTrueFalseTotal
if $separate 2 10 12
341 12 $first = 0;
342 }
343 5 $block->ensure_gap();
344 5 push @{$array_rep_for{$hash_ref}}, $block;
345 }
346  
347 7 ▶ open my $fh, '>', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or croak("Can't open config file '${filename}' for writing (\L$!\E)") 7 0 7
348 or croak "Can't open config file '$filename' for writing (\L$!\E)";
349  
350 7 ▶ flock($fh,LOCK_EX|LOCK_NB)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $filename 5 2 7
Condition60%3/5
ref $filename || (flock($fh, 6) || croak("Can't write to locked config file '${filename}'"))
l!l&&r!l&&!r
250
flock $fh, 6 or croak("Can't write to locked config file '${filename}'")
l!l
50
MC/DC0%0/2
ref $filename || (flock($fh, 6) || croak("Can't write to locked config file '${filename}'"))
ref $filename flock $fh, 6
Truth table66%2/3
A || B
A= ref $filenameB= flock $fh, 6
ABresult
1X1
011
000
351 || croak "Can't write to locked config file '$filename'"
352 if ! ref $filename;
353  
354 7 my $first = 1;
355 7 for my $block ( @{$array_rep_for{$hash_ref}} ) {
356 18 print {$fh} $block->serialize($first, scalar caller, $post_gap);
357 18 $first = 0;
358 }
359  
360 7 ▶ flock($fh,LOCK_UN) if ! ref $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $filename 5 2 7
361  
362 7 return 1;
363 }
364  
365 7 ▶ sub read_config ($\[%$]) {
Subroutine read_config: called CC=7 SCAR=19.6
366 7 my ($filename, $var_ref, $opt_ref) = @_;
367 7 ▶ my $var_type = ref($var_ref) || q{};
Condition50%1/2
ref $var_ref || ''
l!l
70
MC/DC0%0/1
ref $var_ref || ''
ref $var_ref
Truth table50%1/2
A
A= ref $var_ref
Aresult
11
00
368 7 my $hash_ref;
369 7 ▶ if ($var_type eq 'SCALAR' && !defined ${$var_ref} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($var_type eq 'SCALAR' and not defined ${$var_ref;}) { } 1 6 7
elsif ($var_type eq 'HASH') { } 6 0 6
Condition66%2/3
$var_type eq "SCALAR" and not defined ${$var_ref;}
!ll&&!rl&&r
601
MC/DC50%1/2
$var_type eq "SCALAR" and not defined ${$var_ref;}
$var_type eq "SCALAR" not defined ${$var_ref;}
Truth table66%2/3
A and not(B)
A= $var_type eq "SCALAR"B= not defined ${$var_ref;}
ABresult
0X0
100
111
370 1 ${$var_ref} = $hash_ref = {};
371 }
372 elsif ($var_type eq 'HASH') {
373 6 $hash_ref = $var_ref;
374 }
375 else {
376 0 croak q{Scalar second argument to 'read_config' must be empty};
377 }
378  
379 7 bless $hash_ref, 'Config::Std::Hash';
380  
381 7 my $blocks = $array_rep_for{$hash_ref}
382 = _load_config_for($filename, $hash_ref);
383  
384 7 for my $block ( @{$blocks} ) {
385 18 $block->copy_to($hash_ref);
386 }
387  
388 7 $filename_for{$hash_ref} = $filename;
389  
390 # Remove initial empty section if no data...
391 7 ▶ if (!keys %{ $hash_ref->{q{}} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$$hash_ref{""};}) 2 5 7
392 2 delete $hash_ref->{q{}};
393 }
394  
395 7 return 1;
396 }
397  
398 7 ▶ sub _load_config_for {
Subroutine _load_config_for: called CC=18 SCAR=30.2
399 7 my ($filename, $hash_ref) = @_;
400  
401 7 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or croak("Can't open config file '${filename}' (\L$!\E)") 7 0 7
402 or croak "Can't open config file '$filename' (\L$!\E)";
403 7 ▶ flock($fh,LOCK_SH|LOCK_NB)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $filename 6 1 7
Condition60%3/5
ref $filename || (flock($fh, 5) || croak("Can't read from locked config file '${filename}'"))
l!l&&r!l&&!r
160
flock $fh, 5 or croak("Can't read from locked config file '${filename}'")
l!l
60
MC/DC0%0/2
ref $filename || (flock($fh, 5) || croak("Can't read from locked config file '${filename}'"))
ref $filename flock $fh, 5
Truth table66%2/3
A || B
A= ref $filenameB= flock $fh, 5
ABresult
1X1
011
000
404 || croak "Can't read from locked config file '$filename'"
405 if !ref $filename;
406 7 my $text = do{local $/; <$fh>};
407 7 ▶ flock($fh,LOCK_UN) if !ref $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $filename 6 1 7
408  
409 7 my @config_file = Config::Std::Block->new({ name=>q{}, first=>1 });
410 7 my $comment = q{};
411 7 my %seen;
412  
413 # Start tracking whether section markers have gaps after them...
414 7 $post_section_gap_for{$hash_ref} = 0;
415  
416 7 for ($text) {
417 7 pos = 0;
418 7 ▶ while (pos() < length() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $_ < length $_) 113 7 120
419 # Gap...
420 113 ▶ if (m/\G (?: [^\S\n]* (?:\n|\z)+)/gcxms) {
Branch: 4 branches
BranchTrueFalseTotal
if (/\G (?: [^\S\n]* (?:\n|\z)+)/cgmsx) { } 42 71 113
elsif (/\G (\s* [#;] [^\n]* (?:\n|\z) )/cgmsx) { } 2 69 71
elsif (/\G ([^\S\n]*) [[] ( [^]\n]* ) []] ( ([^\S\n]*) [#;] [^\n]* )? [^\S\n]* (?:\n|\z)/cgmsx) { } 11 58 69
elsif (/\G [^\S\n]* ([^=:\n]+?) [^\S\n]* ([:=] [^\S\n]*) ([^\n]*) (?:\n|\z)/cgmsx) { } 58 0 58
421 ### Found gap
422 42 ▶ $config_file[-1]->add_comment($comment) if $comment;
Branch: 1 branch
BranchTrueFalseTotal
if $comment 0 42 42
423 42 $config_file[-1]->add_gap();
424 42 $comment = q{};
425 }
426  
427 # Comment...
428 elsif (m/\G (\s* [#;] [^\n]* (?:\n|\z) )/gcxms) {
429 ### Found comment: $1
430 2 $comment .= $1;
431 }
432  
433 # Block...
434 elsif (m/\G ([^\S\n]*) [[] ( [^]\n]* ) []] ( ([^\S\n]*) [#;] [^\n]* )? [^\S\n]* (?:\n|\z)/gcxms) {
435 11 my ($pre, $name, $parcomm, $ws) = ($1, $2, $3, $4);
436 ### Found block: $name
437 11 ▶ if ($parcomm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parcomm) 0 11 11
438 0 $pre = 2 + length($pre) + length($name) + length($ws);
439 0 ▶ if (m/\G ( (?: \n? [ ]{$pre,} [#] [^\n]* )+ )/gcxms) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G ( (?: \n? [ ]{$pre,} [#] [^\n]* )+ )/cgmsx) 0 0 0
440 0 $parcomm .= "\n$1";
441 }
442 }
443 push @config_file,
444 Config::Std::Block->new({
445 name => $name,
446 precomm => $comment,
447 parcomm => $parcomm,
448 11 first => !$seen{$name}++,
449 });
450 11 $comment = q{};
451  
452 # Check for trailing gap...
453 11 ▶ $post_section_gap_for{$hash_ref}
Branch: 1 branch
BranchTrueFalseTotal
/\G (?= [^\S\n]* (?:\n|\z) )/msx ? : 11 0 11
454 += m/\G (?= [^\S\n]* (?:\n|\z) )/xms ? +1 : -1;
455 }
456  
457 # Key/value...
458 elsif (m/\G [^\S\n]* ([^=:\n]+?) [^\S\n]* ([:=] [^\S\n]*) ([^\n]*) (?:\n|\z)/gcxms) {
459 58 my ($key, $sep, $val) = ($1, $2, $3);
460  
461 58 my $pure_sep = $sep;
462 58 $pure_sep =~ s/\s*//g;
463  
464 # Continuation lines...
465 58 my $continued = 0;
466 58 ▶ while (m/\G [^\S\n]* \Q$sep\E ([^\n]*) (?:\n|\z) /gcxms
Branch: 1 branch
BranchTrueFalseTotal
if (/\G [^\S\n]* \Q$sep\E ([^\n]*) (?:\n|\z) /cgmsx or /\G [^\S\n]* \Q$pure_sep\E ([^\n]*) (?:\n|\z) /cgmsx) 8 58 66
Condition66%2/3
/\G [^\S\n]* \Q$sep\E ([^\n]*) (?:\n|\z) /cgmsx or /\G [^\S\n]* \Q$pure_sep\E ([^\n]*) (?:\n|\z) /cgmsx
l!l&&r!l&&!r
8058
MC/DC50%1/2
/\G [^\S\n]* \Q$sep\E ([^\n]*) (?:\n|\z) /cgmsx or /\G [^\S\n]* \Q$pure_sep\E ([^\n]*) (?:\n|\z) /cgmsx
/\G [^\S\n]* \Q$sep\E ([^\n]*) (?:\n|\z) /cgmsx /\G [^\S\n]* \Q$pure_sep\E ([^\n]*) (?:\n|\z) /cgmsx
Truth table66%2/3
A or B
A= /\G [^\S\n]* \Q$sep\E ([^\n]*) (?:\n|\z) /cgmsxB= /\G [^\S\n]* \Q$pure_sep\E ([^\n]*) (?:\n|\z) /cgmsx
ABresult
1X1
011
000
467 || m/\G [^\S\n]* \Q$pure_sep\E ([^\n]*) (?:\n|\z) /gcxms
468 ) {
469 8 $val .= "\n$1";
470 8 $continued = 1;
471 }
472  
473 58 ▶ $val =~ s/\A \s*|\s* \z//gxms if !$continued;
Branch: 1 branch
BranchTrueFalseTotal
unless $continued 54 4 58
474  
475 ### Found kv: $key, $val
476  
477 58 $config_file[-1]->add_keyval($key, $pure_sep, $val,
478 58 $comment); $comment = q{}; }
479  
480 # Mystery...
481 else {
482 0 my ($problem) = m/\G ([^\n]{10,40}|.{10}) /gcxms;
483 0 die "Error in config file '$filename' near:\n\n\t$problem\n";
484 }
485 }
486 }
487  
488 7 return \@config_file;
489 }
490  
491 7 ▶ sub DEMOLISH {
Subroutine DEMOLISH: called CC=1 SCAR=0.0
492 7 my ($self, $ident) = @_;
493  
494 # Do nothing. Defined to suppress use warnings in Class::Std destructor.
495 }
496  
497  
498 }
499  
500  
501 10 1; # Magic true value required at end of module
502 __END__
↑ Summary