blib/lib/Config/Natural.pm

filter: Statementstmt 93.3% 322 / 345 Branchbran 72.6% 125 / 172 Conditioncond 33.3% 24 / 72 MC/DCmcdc 12.1% 4 / 33 Subroutinesub 97.6% 42 / 43 Podpod 100% 19 / 19 totaltotal 78.3% 536 / 684 CC 126 SCAR 26.4
CC
126
Cov
80%
read_source
39.9
options
30.4
_parse_args
17.7
CRAP 14.1
↑ Summary
1 package Config::Natural;
2  
3 948 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 948 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 196 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶{ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 $VERSION = '1.01';
10 }
11  
12 # class option
13 20 my %options = (
14 quiet => 0,
15 );
16  
17  
18 #
19 # new()
20 # ---
21 26 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
22 26 my $class = shift;
23 26 my $self = bless {
24 options => {
25 'comment_line_symbol' => '#',
26 'affectation_symbol' => '=',
27 'append_symbol' => '+',
28 'multiline_begin_symbol' => '-',
29 'multiline_end_symbol' => '.',
30 'array_begin_symbol' => '(',
31 'array_end_symbol' => ')',
32 'list_begin_symbol' => '{',
33 'list_end_symbol' => '}',
34 'include_symbol' => 'include',
35 'case_sensitive' => 1,
36 'auto_create_surrounding_list' => 0,
37 'read_hidden_files' => 0,
38 'strip_indentation' => 0,
39 },
40 state => { },
41 param => { },
42 handlers => { },
43 prefilter => 0,
44 filter => 0,
45 }, $class;
46
47 26 ▶ if(ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 4 950 954
48 4 my $opts = shift;
49 4 for my $option (keys %$opts) {
50 10 ▶ $options{$option} = $opts->{$option} if exists $options{$option};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{$option} 1 185 186
51 10 $self->{'options'}{$option} = $opts->{$option};
52 10 ▶ $self->filter($opts->{$option}) if $option eq 'filter';
Branch: 1 branch
BranchTrueFalseTotal
if $option eq "filter" 2 8 10
53 10 ▶ $self->prefilter($opts->{$option}) if $option eq 'prefilter';
Branch: 1 branch
BranchTrueFalseTotal
if $option eq "prefilter" 0 10 10
54 }
55 }
56 26 ▶ $self->read_source(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 21 26
57 26 return $self;
58 }
59  
60  
61 #
62 # AUTOLOAD()
63 # --------
64 209 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=14.0
65 20 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 209 my $self = $_[0];
67 209 ▶ my $type = ref $self || croak "I am not an object, so don't call me that way.";
Condition50%1/2
ref $self || croak(q[I am not an object, so don't call me that way.])
l!l
2090
MC/DC0%0/1
ref $self || croak(q[I am not an object, so don't call me that way.])
ref $self
Truth table50%1/2
A
A= ref $self
Aresult
11
00
68 209 my $name = $AUTOLOAD;
69 209 $name =~ s/.*:://;
70
71 carp "Unknown option '$name'"
72 209 ▶ unless exists $options{$name} or defined $self->{options}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{$name} or defined $self->{"options"}{$name} 0 209 209
Condition33%1/3
exists $options{$name} or defined $self->{"options"}{$name}
l!l&&r!l&&!r
02090
MC/DC0%0/2
exists $options{$name} or defined $self->{"options"}{$name}
exists $options{$name} defined $self->{"options"}{$name}
Truth table33%1/3
A or B
A= exists $options{$name}B= defined $self->{"options"}{$name}
ABresult
1X1
011
000
73  
74 209 my $code = q{
75 sub {
76 my $self = shift;
77 my $value = $self->{options}{METHOD};
78 $self->{options}{METHOD} = shift if @_;
79 return $value
80 }
81 };
82 209 $code =~ s/METHOD/$name/g;
83  
84 209 ▶ *$AUTOLOAD = eval $code;
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 11 branches
BranchTrueFalseTotal
if @_ 2 275 277
if @_ 0 194 194
if @_ 0 109 109
if @_ 0 112 112
if @_ 0 112 112
if @_ 0 115 115
if @_ 0 115 115
if @_ 0 137 137
if @_ 0 137 137
if @_ 0 109 109
if @_ 0 392 392
85 209 goto &$AUTOLOAD;
86 }
87  
88  
89 #
90 # DESTROY()
91 # -------
92 26 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
93 26 my $self = shift;
94 26 $self->clear_params;
95 26 $self->delete_all;
96 }
97  
98  
99 #
100 # options()
101 # -------
102 15 ▶sub options {
Subroutine options: called CC=10 SCAR=34.1
Pod documented
103 15 my $self = shift;
104 15 my $args = _parse_args(@_);
105 15 my @ret_list = ();
106
107 15 for my $arg (@{$args->{'get'}}) {
108 carp "Class option '$arg' does not exist" and next
109 0 ▶ unless exists $options{$arg} or $options{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{$arg} or $options{"quiet"} 0 0 0
Condition0%0/8
exists $options{$arg} || $options{'quiet'} || carp("Class option '${arg}' does not exist") && (next)
l!l&&r!l&&!r
000
exists $options{$arg} or $options{"quiet"}
l!l&&r!l&&!r
000
carp("Class option '${arg}' does not exist") and next
!ll
00
MC/DC0%0/3
exists $options{$arg} || $options{'quiet'} || carp("Class option '${arg}' does not exist") && (next)
exists $options{$arg} $options{"quiet"} carp("Class option '${arg}' does not exist")
Truth table0%0/4
A or B || C
A= exists $options{$arg}B= $options{"quiet"}C= carp("Class option '${arg}' does not exist")
ABCresult
1XX1
01X1
0011
0000
110 0 push @ret_list, $options{$arg};
111 }
112
113 15 for my $arg (keys %{$args->{'set'}}) {
114 carp "Class option '$arg' does not exist" and next
115 15 ▶ unless exists $options{$arg} or $options{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{$arg} or $options{"quiet"} 0 15 15
Condition25%2/8
exists $options{$arg} || $options{'quiet'} || carp("Class option '${arg}' does not exist") && (next)
l!l&&r!l&&!r
1500
exists $options{$arg} or $options{"quiet"}
l!l&&r!l&&!r
1500
carp("Class option '${arg}' does not exist") and next
!ll
00
MC/DC0%0/3
exists $options{$arg} || $options{'quiet'} || carp("Class option '${arg}' does not exist") && (next)
exists $options{$arg} $options{"quiet"} carp("Class option '${arg}' does not exist")
Truth table25%1/4
A or B || C
A= exists $options{$arg}B= $options{"quiet"}C= carp("Class option '${arg}' does not exist")
ABCresult
1XX1
01X1
0011
0000
116 15 $options{$arg} = $args->{'set'}{$arg};
117 }
118
119 15 ▶ return wantarray ? @ret_list : $ret_list[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 15 15
120 }
121  
122  
123 #
124 # _read_dir()
125 # ---------
126 # Recursively walk through the given directory and read
127 # all the files encountered
128 #
129 20 ▶sub _read_dir {
Subroutine _read_dir: called CC=6 SCAR=18.0
130 20 my $self = shift;
131 20 my $dir = shift;
132
133 20 ▶ return $self->read_source($dir) if -f $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -f $dir 0 20 20
134
135 20 ▶ opendir(DIR, $dir) or croak "Can't read directory '$dir': $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or croak("Can't read directory '${dir}': $!") 20 0 20
136 20 my @list = grep {!/^\.\.?$/} readdir(DIR); # remove . and ..
137 20 ▶ @list = grep {!/^\./} @list unless $self->read_hidden_files;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->read_hidden_files 18 2 20
138 20 closedir(DIR);
139
140 20 for my $file (@list) {
141 81 my $path = File::Spec->catfile($dir, $file);
142  
143 81 ▶ if(-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 10 71 81
144 10 $self->_read_dir($path)
145 } else {
146 71 $self->read_source($path)
147 }
148 }
149 }
150  
151  
152 #
153 # read_source()
154 # -----------
155 # Read the data from the given file or filehandle
156 #
157 118 ▶sub read_source {
Subroutine read_source: called CC=30 SCAR=36.9
Pod documented
158 118 my $self = shift;
159 118 my $file = shift;
160 118 local $_;
161
162 # go to recursive mode if the argument is a directory
163 118 ▶ if(-d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 10 108 118
164 10 unshift @_, $self, $file;
165 10 goto &_read_dir
166 }
167
168 # ... else open the file
169 108 ▶ my $fh = _file_or_handle($file) or croak "Can't open file '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = _file_or_handle($file) or croak("Can't open file '${file}': $!") 108 0 108
170
171 # keep local copy of the properties we'll use
172 108 my $comment = $self->comment_line_symbol;
173 108 my $aff_sym = $self->affectation_symbol;
174 108 my $app_sym = $self->append_symbol;
175 108 my $multiline = $self->multiline_begin_symbol;
176 108 my $multi_end = $self->multiline_end_symbol;
177 108 my $array = $self->array_begin_symbol;
178 108 my $array_end = $self->array_end_symbol;
179 108 my $list = $self->list_begin_symbol;
180 108 my $list_end = $self->list_end_symbol;
181 108 my $include = $self->include_symbol;
182 108 my $state = $self->{'state'};
183
184 # store the name of the last opened file
185 108 $state->{'filename'} = $file;
186
187 108 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 735 108 843
188 ## execute the prefilter if present
189 735 ▶ $self->{'prefilter'} and $_ = &{$self->{'prefilter'}}($self, $_);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"prefilter"} and $_ = &{$self->{"prefilter"};}($self, $_) 20 715 735
190
191 735 ▶ next if /^\s*$/; # skip empty lines
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 86 649 735
192 649 ▶ next if /^\s*$comment/; # skip comments
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$comment/ 16 633 649
193 633 chomp;
194
195 ## include statement
196 633 ▶ if(/^\s*\Q${include}\E\s+(\S+)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\Q$include\E\s+(\S+)\s*$/) 20 613 633
197 20 my $included = $1;
198 20 my @path = File::Spec->splitdir($state->{'filename'});
199 20 pop @path; # remove the current file name from the path
200 20 $included = File::Spec->catdir(@path, $included);
201 20 $self->read_source($included);
202 next
203 20 }
204
205 ## begin of a new list
206 613 ▶ if(/^\s*(\S+)\s*\Q${list}\E\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(\S+)\s*\Q$list\E\s*$/) 99 514 613
207 99 push @{$state->{lists_names}}, $1;
208 99 push @{$state->{lists_stacks}}, {};
209 next
210 99 }
211
212 ## end of the current list
213 514 ▶ if(/^\s*\Q${list_end}\E\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\Q$list_end\E\s*$/) 99 415 514
214 99 my $lists_stacks = $state->{'lists_stacks'};
215 99 my $curlistname = pop @{$state->{'lists_names'}};
216 99 my $curlistref = pop @$lists_stacks;
217
218 99 ▶ if(@$lists_stacks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lists_stacks) { } 68 31 99
219 68 push @{ $$lists_stacks[-1] ->{ $curlistname } }, $curlistref
220 } else {
221 31 push @{$self->{'param'}{$curlistname}}, $curlistref
222 }
223
224 next
225 99 }
226
227 ## parameter affectation
228 415 my($field,$value) = (/^\s*(\S+)\s*\Q${app_sym}\E?\s*\Q${aff_sym}\E\s*(.*)$/);
229
230 ## detect append mode
231 415 my $append = /^\s*(\S+)\s*\Q${app_sym}\E\s*\Q${aff_sym}\E\s*(.*)$/;
232 415 my $prev_value = ${$state->{'lists_names'}}[-1] ?
233 415 ▶ $state->{'lists_stacks'}[-1]{$field} : $self->{'param'}{$field};
Branch: 1 branch
BranchTrueFalseTotal
do { $$state{'lists_names'} }->[-1] ? : 274 141 415
234
235 ## multiline case
236 415 ▶ if($value =~ /^\s*\Q${multiline}\E\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\s*\Q$multiline\E\s*$/) 17 398 415
237 17 $value = '';
238 17 ▶ $value = $prev_value . $value and $append = 0 if $append;
Branch: 1 branch
BranchTrueFalseTotal
if $append 0 17 17
Condition16%1/6
$append && (($value = $prev_value . $value) && ($append = 0))
!ll&&!rl&&r
1700
$value = $prev_value . $value and $append = 0
!ll&&!rl&&r
000
MC/DC0%0/3
$append && (($value = $prev_value . $value) && ($append = 0))
$append $value = $prev_value . $value $append = 0
Truth table0%0/4
A && B and C
A= $appendB= $value = $prev_value . $valueC= $append = 0
ABCresult
0XX0
10X0
1100
1111
239 17 $_ = <$fh>;
240
241 17 ▶ $self->strip_indentation and my($indent) = (/^(\s*)/);
Branch: 1 branch
BranchTrueFalseTotal
$self->strip_indentation and my($indent) = /^(\s*)/ 0 17 17
242
243 17 ▶ while(not /^\s*\Q${multi_end}\E\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\s*\Q$multi_end\E\s*$/) 52 14 66
244 52 ▶ $indent and s/^$indent//;
Branch: 1 branch
BranchTrueFalseTotal
$indent and s/^$indent// 0 52 52
245 52 $value .= $_;
246 52 $_ = <$fh>;
247 52 ▶ last if eof($fh)
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 3 49 52
248 }
249 }
250
251 ## array case
252 415 ▶ if($value =~ /^\s*\Q${array}\E\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\s*\Q$array\E\s*$/) 25 390 415
253 25 $value = '';
254 25 $_ = <$fh>;
255
256 25 ▶ while(not /^\s*\Q${array_end}\E\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\s*\Q$array_end\E\s*$/) 103 23 126
257 103 s/^\s*//;
258 103 $value .= $_;
259 103 $_ = <$fh>;
260 103 ▶ last if eof($fh)
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 2 101 103
261 }
262
263 $append ?
264 25 ▶ ( push @$prev_value, split($/, $value) and $append = 0 )
Branch: 1 branch
BranchTrueFalseTotal
$append ? : 1 24 25
Condition33%1/3
push @$prev_value, split(m[$/], $value, 0) and $append = 0
!ll&&!rl&&r
010
MC/DC0%0/2
push @$prev_value, split(m[$/], $value, 0) and $append = 0
push @$prev_value, split(m[$/], $value, 0) $append = 0
Truth table33%1/3
A and B
A= push @$prev_value, split(m[$/], $value, 0)B= $append = 0
ABresult
0X0
100
111
265 : $self->param({ $field => [ split $/, $value ] });
266 next
267 25 }
268
269 ## create a surrounding list if the parameter already exists
270 390 ▶ if($self->auto_create_surrounding_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_create_surrounding_list) 0 390 390
271 0 my $surrlist = "${field}s";
272 0 my $root_param = $self->{'param'};
273 0 my $curlistref = ${$self->{'state'}{'lists_stacks'}}[-1];
274
275 0 ▶ if($curlistref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curlistref) 0 0 0
276 0 $root_param = $curlistref
277 }
278
279 ## the surrounding list doesn't already exist
280 0 ▶ if(exists $root_param->{$field} and not exists $root_param->{$surrlist}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $root_param->{$field} and not exists $root_param->{$surrlist}) 0 0 0
Condition0%0/3
exists $root_param->{$field} and not exists $root_param->{$surrlist}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $root_param->{$field} and not exists $root_param->{$surrlist}
exists $root_param->{$field} not exists $root_param->{$surrlist}
Truth table0%0/3
A and not(B)
A= exists $root_param->{$field}B= not exists $root_param->{$surrlist}
ABresult
0X0
100
111
281 0 $root_param->{$surrlist} = [ { $field => $root_param->{$field} } ];
282 0 delete $root_param->{$field};
283 }
284
285 ## add the new parameter to the list
286 0 ▶ if(exists $root_param->{$surrlist}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $root_param->{$surrlist}) 0 0 0
287 0 push @{$root_param->{$surrlist}}, { $field => $value };
288 next
289 0 }
290 }
291
292 ## add the new value to the object parameters
293 390 ▶ $value = $prev_value . $value if $append;
Branch: 1 branch
BranchTrueFalseTotal
if $append 6 384 390
294 390 $self->param({ $field => $value });
295 }
296 }
297  
298  
299 #
300 # _file_or_handle()
301 # ---------------
302 108 ▶sub _file_or_handle {
Subroutine _file_or_handle: called CC=3 SCAR=11.0
303 108 my $file = shift;
304
305 108 ▶ unless(ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 102 6 108
306 102 ▶ my $mode = shift || 'r';
Condition50%1/2
shift() || 'r'
l!l
0102
MC/DC0%0/1
shift() || 'r'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
307 102 my $fh = new FileHandle $file, $mode;
308 102 return $fh
309 }
310
311 6 return $file
312 }
313  
314  
315 #
316 # param()
317 # -----
318 738 ▶sub param {
Subroutine param: called CC=11 SCAR=24.3
Pod documented
319 738 my $self = shift;
320 738 ▶ return $self->all_parameters unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 9 729 738
321
322 729 my $args = _parse_args(@_);
323
324 729 my @retlist = (); # return list
325
326 ## get the value of the desired parameters
327 729 for my $arg (@{$args->{'get'}}) {
328 carp "Parameter '$arg' does not exist" and next
329 139 ▶ if not exists $self->{'param'}{_case_($self, $arg)} and not $options{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"param"}{_case_($self, $arg)} or $options{"quiet"} 0 139 139
Condition37%3/8
exists $self->{'param'}{_case_($self, $arg)} || $options{'quiet'} || carp("Parameter '${arg}' does not exist") && (next)
l!l&&r!l&&!r
13900
exists $self->{"param"}{_case_($self, $arg)} or $options{"quiet"}
l!l&&r!l&&!r
13630
carp("Parameter '${arg}' does not exist") and next
!ll
00
MC/DC0%0/3
exists $self->{'param'}{_case_($self, $arg)} || $options{'quiet'} || carp("Parameter '${arg}' does not exist") && (next)
exists $self->{"param"}{_case_($self, $arg)} $options{"quiet"} carp("Parameter '${arg}' does not exist")
Truth table50%2/4
A or B || C
A= exists $self->{"param"}{_case_($self, $arg)}B= $options{"quiet"}C= carp("Parameter '${arg}' does not exist")
ABCresult
1XX1
01X1
0011
0000
330
331 139 push @retlist, $self->{'param'}{_case_($self, $arg)}
332 }
333
334 ## set the named parameters to new values
335 729 my $param;
336 729 my $current_list = ${$self->{'state'}{'lists_names'}}[-1];
337 729 my @arg_list = keys %{$args->{'set'}};
338
339 729 ▶ if($current_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_list) { } 274 455 729
340 274 $param = ${$self->{'state'}{'lists_stacks'}}[-1];
341
342 } else {
343 455 $param = $self->{'param'};
344 }
345
346 729 for my $arg (@arg_list) {
347 609 my $value = $args->{'set'}{$arg};
348
349 ## use the filter if present
350 $self->{'filter'} and
351 609 ▶ $value = &{$self->{'filter'}}($self, $value);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"filter"} and $value = &{$self->{"filter"};}($self, $value) 92 517 609
352
353 ## use the handler if present
354 609 ▶ $self->{'handlers'}{$arg} and
Branch: 1 branch
BranchTrueFalseTotal
$self->{"handlers"}{$arg} and $value = $self->exec_handler($arg, $value) 5 604 609
355 $value = $self->exec_handler($arg, $value);
356
357 609 $param->{_case_($self, $arg)} = $value
358 }
359
360 729 ▶ return wantarray ? @retlist : $retlist[0]
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 724 729
361 }
362  
363  
364 #
365 # _case_()
366 # ------
367 # Check for the case
368 #
369 1231 ▶sub _case_ {
Subroutine _case_: called CC=2 SCAR=6.9
370 1231 my $self = shift;
371 1231 my $param = shift;
372 1231 ▶ return ($self->case_sensitive ? $param : lc $param)
Branch: 1 branch
BranchTrueFalseTotal
$self->case_sensitive ? : 1202 29 1231
373 }
374  
375  
376 #
377 # _parse_args()
378 # -----------
379 747 ▶sub _parse_args {
Subroutine _parse_args: called CC=12 SCAR=28.8
380 747 my %args = ( get => [], set => {} );
381
382 747 ▶ while(my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) 759 747 1506
383 759 ▶ if(my $ref_type = ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref_type = ref $arg) { } 598 161 759
384
385 ## setting multiples parameters using a hashref
386 598 ▶ if($ref_type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_type eq 'HASH') { } 598 0 598
387 598 local $_;
388 598 for (keys %$arg) {
389 602 ▶ $args{'set'}{$_} = $arg->{$_} if $_
Branch: 1 branch
BranchTrueFalseTotal
if $_ 602 0 602
390 }
391
392 } else {
393 0 ▶ carp "Bad ref $ref_type; ignoring it" unless $options{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"quiet"} 0 0 0
394 next
395 0 }
396
397 } else {
398 ## setting a parameter to a new value
399 161 ▶ if(substr($arg, 0, 1) eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($arg, 0, 1) eq '-') { } 22 139 161
400 22 $arg = substr($arg, 1);
401 22 my $val = shift;
402 carp "Undefined value for parameter '$arg'" and next
403 22 ▶ if not defined $val and not $options{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val or $options{"quiet"} 0 22 22
Condition25%2/8
defined $val || $options{'quiet'} || carp("Undefined value for parameter '${arg}'") && (next)
l!l&&r!l&&!r
2200
defined $val or $options{"quiet"}
l!l&&r!l&&!r
2200
carp("Undefined value for parameter '${arg}'") and next
!ll
00
MC/DC0%0/3
defined $val || $options{'quiet'} || carp("Undefined value for parameter '${arg}'") && (next)
defined $val $options{"quiet"} carp("Undefined value for parameter '${arg}'")
Truth table25%1/4
A or B || C
A= defined $valB= $options{"quiet"}C= carp("Undefined value for parameter '${arg}'")
ABCresult
1XX1
01X1
0011
0000
404 22 ▶ $args{'set'}{$arg} = $val if $arg
Branch: 1 branch
BranchTrueFalseTotal
if $arg 22 0 22
405
406 ## getting the value of a parameter
407 } else {
408 139 push @{$args{'get'}}, $arg
409 }
410 }
411 }
412
413 747 return \%args
414 }
415  
416  
417 #
418 # prefilter()
419 # ---------
420 # Set a new prefilter.
421 #
422 1 ▶sub prefilter {
Subroutine prefilter: called CC=2 SCAR=7.0
Pod documented
423 1 my $self = shift;
424 1 my $code = shift;
425 1 ▶ croak "Not a CODEREF" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 1 1
426 1 $self->{'prefilter'} = $code;
427 }
428  
429  
430 #
431 # filter()
432 # ------
433 # Set a new filter.
434 #
435 3 ▶sub filter {
Subroutine filter: called CC=2 SCAR=7.0
Pod documented
436 3 my $self = shift;
437 3 my $code = shift;
438 3 ▶ croak "Not a CODEREF" unless ref $code eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 3 3
439 3 $self->{'filter'} = $code;
440 }
441  
442  
443 #
444 # set_handler()
445 # -----------
446 # Set a new handler for a parameter
447 #
448 2 ▶sub set_handler {
Subroutine set_handler: called CC=1 SCAR=0.0
Pod documented
449 2 my $self = shift;
450 2 my $param = shift;
451 2 my $code = shift;
452 2 $self->{'handlers'}{$param} = $code;
453 }
454  
455  
456 #
457 # delete_handler()
458 # --------------
459 # Delete the handler of the given parameter
460 #
461 1 ▶sub delete_handler {
Subroutine delete_handler: called CC=1 SCAR=0.0
Pod documented
462 1 my $self = shift;
463 1 my $param = shift;
464 1 delete $self->{'handlers'}{$param};
465 }
466  
467  
468 #
469 # has_handler()
470 # -----------
471 # Check if the given parameter has a handler
472 #
473 3 ▶sub has_handler {
Subroutine has_handler: called CC=1 SCAR=0.0
Pod documented
474 3 my $self = shift;
475 3 my $param = shift;
476 3 return exists $self->{'handlers'}{$param}
477 }
478  
479  
480 #
481 # exec_handler()
482 # ------------
483 # Execute the handler of a parameter
484 #
485 5 ▶sub exec_handler {
Subroutine exec_handler: called CC=1 SCAR=0.0
Pod documented
486 5 my $self = shift;
487 5 my $param = shift;
488 5 my $value = shift;
489 5 return &{$self->{'handlers'}{$param}}($param, $value)
490 }
491  
492  
493 #
494 # all_parameters()
495 # --------------
496 # Return the list of all the parameters at the root level
497 #
498 64 ▶sub all_parameters {
Subroutine all_parameters: called CC=1 SCAR=0.0
Pod documented
499 64 my $self = shift;
500 64 return keys %{$self->{'param'}}
501 }
502  
503  
504 #
505 # param_tree()
506 # --------------
507 # Return the hash tree of all parameters
508 #
509 1 ▶sub param_tree {
Subroutine param_tree: called CC=2 SCAR=6.9
Pod documented
510 1 my $self = shift;
511 1 my $tree = {};
512 1 $tree->{$_} = $self->param($_) for($self->param());
513 1 return $tree;
514 }
515  
516  
517 #
518 # value_of()
519 # --------
520 # Return the value of the specified parameter
521 #
522 24 ▶sub value_of {
Subroutine value_of: called CC=10 SCAR=23.0
Pod documented
523 24 my $self = shift;
524 24 my $param_path = shift;
525
526 # handle simple cases simply...
527 24 ▶ return $self->{'param'}{$param_path} if $self->{'param'}{$param_path};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"param"}{$param_path} 1 23 24
528
529 # handle more complex cases nicely.
530 23 my @path = split '/', $param_path;
531 23 ▶ not $path[0] and shift @path;
Branch: 1 branch
BranchTrueFalseTotal
$path[0] or shift @path 0 23 23
532
533 23 my($name,$index) = ( (shift @path) =~ /^([^[]+)(?:\[([+-]?\d+|\*)\])?$/ );
534 23 ▶ my $node = $self->param($name); $index ||= 0;
Condition100%2/2
$index ||= 0
l!l
518
MC/DC100%1/1
$index ||= 0
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
535 23 ▶ return $node if $index eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $index eq "*" 1 22 23
536
537 22 ▶ if(ref $node) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node) 22 0 22
538 22 $node = $node->[int($index)];
539 22 for my $p (@path) {
540 40 ($name,$index) = ( ($p) =~ /^([^[]+)(?:\[([+-]?\d+|\*)\])?$/ );
541 40 ▶ $node = $node->{$name}; $index ||= 0;
Condition100%2/2
$index ||= 0
l!l
1228
MC/DC100%1/1
$index ||= 0
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
542 40 ▶ ref $node and $index ne '*' and $node = $node->[int($index)];
Branch: 1 branch
BranchTrueFalseTotal
ref $node and $index ne "*" and $node = $$node[int $index] 18 22 40
Condition100%3/3
ref $node and $index ne "*"
!ll&&!rl&&r
18418
MC/DC100%2/2
ref $node and $index ne "*"
ref $node $index ne "*"
Truth table100%3/3
A and B
A= ref $nodeB= $index ne "*"
ABresult
0X0
100
111
543 }
544 }
545
546 22 return $node
547 }
548  
549  
550 #
551 # delete()
552 # ------
553 # Delete the given parameters
554 #
555 28 ▶sub delete {
Subroutine delete: called CC=5 SCAR=20.9
Pod documented
556 28 my $self = shift;
557
558 28 for my $param (@_) {
559 carp "Parameter '$param' does not exist" and next
560 172 ▶ if not exists $self->{'param'}{_case_($self, $param)} and not $options{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"param"}{_case_($self, $param)} or $options{"quiet"} 0 172 172
Condition25%2/8
exists $self->{'param'}{_case_($self, $param)} || $options{'quiet'} || carp("Parameter '${param}' does not exist") && (next)
l!l&&r!l&&!r
17200
exists $self->{"param"}{_case_($self, $param)} or $options{"quiet"}
l!l&&r!l&&!r
17200
carp("Parameter '${param}' does not exist") and next
!ll
00
MC/DC0%0/3
exists $self->{'param'}{_case_($self, $param)} || $options{'quiet'} || carp("Parameter '${param}' does not exist") && (next)
exists $self->{"param"}{_case_($self, $param)} $options{"quiet"} carp("Parameter '${param}' does not exist")
Truth table25%1/4
A or B || C
A= exists $self->{"param"}{_case_($self, $param)}B= $options{"quiet"}C= carp("Parameter '${param}' does not exist")
ABCresult
1XX1
01X1
0011
0000
561 172 delete $self->{'param'}{_case_($self, $param)}
562 }
563 }
564  
565  
566 #
567 # delete_all()
568 # ----------
569 27 ▶sub delete_all {
Subroutine delete_all: called CC=1 SCAR=0.0
Pod documented
570 27 my $self = shift;
571 27 $self->delete($self->all_parameters)
572 }
573  
574  
575 #
576 # clear()
577 # -----
578 2 ▶sub clear {
Subroutine clear: called CC=2 SCAR=6.9
Pod documented
579 2 my $self = shift;
580 2 for my $param (@_) {
581 5 $self->param({$param => ''})
582 }
583 }
584  
585  
586 #
587 # clear_params()
588 # ------------
589 27 ▶sub clear_params {
Subroutine clear_params: called CC=2 SCAR=6.9
Pod documented
590 27 my $self = shift;
591 27 for my $param ($self->all_parameters) {
592 172 $self->param({$param => ''})
593 }
594 }
595  
596  
597 #
598 # dump_param()
599 # ----------
600 3 ▶sub dump_param {
Subroutine dump_param: called CC=4 SCAR=14.5
Pod documented
601 3 my $self = shift;
602 3 my $args = _parse_args(@_);
603 3 ▶ my $prefix = $args->{'set'}{'prefix'} || '';
Condition50%1/2
$args->{'set'}{'prefix'} || ''
l!l
03
MC/DC0%0/1
$args->{'set'}{'prefix'} || ''
$args->{'set'}{'prefix'}
Truth table50%1/2
A
A= $args->{'set'}{'prefix'}
Aresult
11
00
604 3 ▶ my $suffix = $args->{'set'}{'suffix'} || '';
Condition50%1/2
$args->{'set'}{'suffix'} || ''
l!l
03
MC/DC0%0/1
$args->{'set'}{'suffix'} || ''
$args->{'set'}{'suffix'}
Truth table50%1/2
A
A= $args->{'set'}{'suffix'}
Aresult
11
00
605 3 ▶ my $nospace = $args->{'set'}{'nospace'} || 0;
Condition50%1/2
$args->{'set'}{'nospace'} || 0
l!l
03
MC/DC0%0/1
$args->{'set'}{'nospace'} || 0
$args->{'set'}{'nospace'}
Truth table50%1/2
A
A= $args->{'set'}{'nospace'}
Aresult
11
00
606  
607 3 return _dump_tree($self, $self->{'param'}, 0,
608 prefix => $prefix, suffix => $suffix, nospace => $nospace)
609 }
610  
611  
612 #
613 # _dump_tree()
614 # ----------
615 48 ▶sub _dump_tree {
Subroutine _dump_tree: called CC=13 SCAR=25.7
616 48 my $self = shift;
617 48 my $tree = shift;
618 48 my $level = shift;
619 48 my %state = @_;
620 48 my $str = '';
621 48 ▶ my $sp = $state{'nospace'} ? '' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$state{'nospace'} ? : 0 48 48
622
623 48 ▶ if(ref $tree eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $tree eq 'HASH') { } 30 18 48
elsif (ref $tree eq 'ARRAY') { } 18 0 18
624
625 # add the list name and symbol
626 $state{'list_name'} and
627 $str .= join '',
628 30 ▶ $/, $sp x (($level-1)*2), $state{'list_name'}, $sp,
Branch: 1 branch
BranchTrueFalseTotal
$state{"list_name"} and $str .= join("", $/, $sp x (($level - 1) * 2), $state{"list_name"}, $sp, $self->list_begin_symbol, $/) 27 3 30
629 $self->list_begin_symbol, $/;
630
631 30 for my $param (sort keys %$tree) {
632 96 ▶ if(ref($tree->{$param})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tree->{$param}) { } 18 78 96
633 18 $str .= _dump_tree($self, $tree->{$param}, $level+1, %state, list_name => $param)
634
635 } else {
636 ## multi-line value?
637 78 ▶ my $multiline = 1 if $tree->{$param} =~ /\n|\r/;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->{$param} =~ /\n|\r/ 3 75 78
638
639 $str .= join '',
640 $sp x ($level*2),
641 $state{'prefix'}, $param, $sp, $self->affectation_symbol, $sp,
642 ($multiline ? $self->multiline_begin_symbol . $/ : ''),
643 $tree->{$param},
644 ($multiline ? $self->multiline_end_symbol . $/ : ''),
645 78 ▶ $state{'suffix'}, $/;
Branch: 2 branches
BranchTrueFalseTotal
$multiline ? : 3 75 78
$multiline ? : 3 75 78
646 }
647 }
648
649 # add the list end symbol
650 30 ▶ $state{'list_name'} and
Branch: 1 branch
BranchTrueFalseTotal
$state{"list_name"} and $str .= join("", $sp x (($level - 1) * 2), $self->list_end_symbol, $/) 27 3 30
651 $str .= join '',
652 $sp x (($level-1)*2), $self->list_end_symbol, $/;
653
654 } elsif(ref $tree eq 'ARRAY') {
655 18 ▶ if(ref $tree->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tree->[0]) { } 12 6 18
656 12 for my $list (@$tree) { $str .= _dump_tree($self, $list, $level, %state) }
657 } else {
658 $str .= join '',
659 $sp x ($level*2),
660 $state{'prefix'}, $state{'list_name'}, $sp,
661 $self-> affectation_symbol, $sp, $self->array_begin_symbol, $/,
662 6 (map {($sp x (($level+1)*2)) . $_ . $/} @$tree),
663 $sp x ($level*2), $self->array_end_symbol, $/
664 }
665
666 } else {
667 0 warn "unexpected reference type ", ref($tree)
668 }
669
670 48 return $str
671 }
672  
673  
674 #
675 # write_source()
676 # ------------
677 # Write the current state of the object to a file
678 #
679 0 ▶sub write_source {
Subroutine write_source: not called CC=3 SCAR=24.8
Pod documented
680 0 my $self = shift;
681
682 # use the last filename given to read_source() if no arg
683 0 ▶ push @_, $self->{'state'}{'filename'} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
684
685 0 my $file = shift;
686 0 my $fh = _file_or_handle($file, 'w');
687 0 ▶ print $fh $self->dump_param(@_) or croak "Error while writing to '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $self->dump_param(@_) or croak("Error while writing to '${file}': $!") 0 0 0
688 }
689  
690  
691 20 1;
692  
693 __END__
↑ Summary