Config/IOD.pm

filter: Statementstmt 97.6% 82 / 84 Branchbran 95.6% 44 / 46 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 92.6% 139 / 150 CC 28 SCAR 31.8
CC
28
Cov
94%
_read_string
28.3
BEGIN
1.0
BEGIN
1.0
CRAP 24.1
« Config/IOD/INI.pm ↑ Summary Config/IOD/Document.pm »
1 package Config::IOD;
2  
3 18 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use parent qw(Config::IOD::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 18 our $DATE = '2022-05-02'; # DATE
11 18 our $DIST = 'Config-IOD'; # DIST
12 18 our $VERSION = '0.353'; # VERSION
13  
14 118 ▶sub _init_read {
Subroutine _init_read: called CC=1 SCAR=0.0
15 118 my $self = shift;
16  
17 118 $self->{_cur_section} = $self->{default_section};
18  
19 # for checking when allow_duplicate_key=0
20 118 $self->{_key_mem} = {}; # key=section name, value=hash of key->1
21  
22 118 $self->SUPER::_init_read;
23 }
24  
25 18 our $re_directive_abo =
26 qr/^(;?)(\s*)!
27 (\s*)(\w+)(\s*)(.*)
28 (\R?)\z/x;
29 18 our $re_directive =
30 qr/^(;)(\s*)!
31 (\s*)(\w+)(\s*)(.*)
32 (\R?)\z/x;
33  
34 122 ▶sub _read_string {
Subroutine _read_string: called CC=28 SCAR=33.4
35 122 my ($self, $str) = @_;
36  
37 122 my $res = [];
38  
39 my $directive_re = $self->{allow_bang_only} ?
40 122 ▶ $re_directive_abo : $re_directive;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'allow_bang_only'} ? : 121 1 122
41  
42 122 my @lines = split /^/, $str;
43 122 local $self->{_linum} = 0;
44 LINE:
45 122 for my $line (@lines) {
46 887 $self->{_linum}++;
47  
48 # blank line
49 887 ▶ if ($line !~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /\S/) 189 698 887
50 189 push @$res, [
51 'B',
52 $line, # RAW
53 ];
54 189 next LINE;
55 }
56  
57 # section line
58 698 ▶ if ($line =~ /^(\s*)\[(\s*)(.+?)(\s*)\]
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s*)\[(\s*)(.+?)(\s*)\] (?: (\s*)([;#])(.*))? (\R?)\z/x) 152 546 698
59 (?: (\s*)([;#])(.*))?
60 (\R?)\z/x) {
61 152 push @$res, [
62 'S',
63 $1, # COL_S_WS1
64 $2, # COL_S_WS2
65 $3, # COL_S_SECTION
66 $4, # COL_S_WS3
67 $5, # COL_S_WS4
68 $6, # COL_S_COMMENT_CHAR
69 $7, # COL_S_COMMENT
70 $8, # COL_S_NL
71 ];
72 152 $self->{_cur_section} = $3;
73 152 next LINE;
74 }
75  
76 # directive line
77 546 my $line0 = $line;
78 546 ▶ if ($self->{enable_directive} && $line =~ s/$directive_re//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"enable_directive"} and $line =~ s/$directive_re//) 29 517 546
Condition100%3/3
$self->{"enable_directive"} and $line =~ s/$directive_re//
!ll&&!rl&&r
551229
MC/DC100%2/2
$self->{"enable_directive"} and $line =~ s/$directive_re//
$self->{"enable_directive"} $line =~ s/$directive_re//
Truth table100%3/3
A and B
A= $self->{"enable_directive"}B= $line =~ s/$directive_re//
ABresult
0X0
100
111
79 29 push @$res, [
80 'D',
81 $1, # COL_D_COMMENT_CHAR
82 $2, # COL_D_WS1
83 $3, # COL_D_WS2
84 $4, # COL_D_DIRECTIVE
85 $5, # COL_D_WS3
86 $6, # COL_D_ARGS_RAW
87 $7, # COL_D_NL
88 ];
89 29 my $directive = $4;
90 29 ▶ if ($self->{allow_directives}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"allow_directives"}) 3 26 29
91 $self->_err("Directive '$directive' is not in ".
92 "allow_directives list")
93 3 unless grep { $_ eq $directive }
94 3 ▶ @{$self->{allow_directives}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $directive;} @{$$self{"allow_directives"};} 1 2 3
95 }
96 28 ▶ if ($self->{disallow_directives}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"disallow_directives"}) 3 25 28
97 $self->_err("Directive '$directive' is in ".
98 "disallow_directives list")
99 3 if grep { $_ eq $directive }
100 3 ▶ @{$self->{disallow_directives}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $directive;} @{$$self{"disallow_directives"};} 2 1 3
101 }
102 26 my $args = $self->_parse_command_line($6);
103 26 ▶ if (!defined($args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args) 1 25 26
104 1 $self->_err("Invalid arguments syntax '$6'");
105 }
106 25 ▶ if ($directive eq 'include') {
Branch: 3 branches
BranchTrueFalseTotal
if ($directive eq 'include') { } 7 18 25
elsif ($directive eq 'merge') { } 10 8 18
elsif ($directive eq 'noop') { } 5 3 8
107 7 my $path;
108 7 ▶ if (! @$args) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @$args) { } 1 6 7
elsif (@$args > 1) { } 0 6 6
109 1 $self->_err("Missing filename to include");
110 } elsif (@$args > 1) {
111 0 $self->_err("Extraneous arguments");
112 } else {
113 6 $path = $args->[0];
114 }
115 6 my $res = $self->_push_include_stack($path);
116 6 ▶ if ($res->[0] != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] != 200) 1 5 6
117 1 $self->_err("Can't include '$path': $res->[1]");
118 }
119 5 $path = $res->[2];
120 5 $self->_read_string($self->_read_file($path));
121 4 $self->_pop_include_stack;
122 } elsif ($directive eq 'merge') {
123 } elsif ($directive eq 'noop') {
124 } else {
125 3 ▶ if ($self->{ignore_unknown_directive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ignore_unknown_directive'}) { } 1 2 3
126 } else {
127 2 $self->_err("Unknown directive '$directive'");
128 }
129 }
130 20 next LINE;
131 }
132  
133 L1:
134 # comment line
135 517 ▶ if ($line =~ /^(\s*)([;#])(.*?)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s*)([;#])(.*?) (\R?)\z/x) 65 452 517
136 (\R?)\z/x) {
137 65 push @$res, [
138 'C',
139 $1, # COL_C_WS1
140 $2, # COL_C_COMMENT_CHAR
141 $3, # COL_C_COMMENT
142 $4, # COL_C_NL
143 ];
144 65 next LINE;
145 }
146  
147 # key line
148 452 ▶ if ($line =~ /^(\s*)([^=]+?)(\s*)=
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s*)([^=]+?)(\s*)= (\s*)(.*?) (\R?)\z/x) 447 5 452
149 (\s*)(.*?)
150 (\R?)\z/x) {
151 447 push @$res, [
152 'K',
153 $1, # COL_K_WS1
154 $2, # COL_K_KEY
155 $3, # COL_K_WS2
156 $4, # COL_K_WS3
157 $5, # COL_K_VALUE_RAW
158 $6, # COL_K_NL
159 ];
160  
161 447 ▶ if ($self->{warn_perl} && !$4 && substr($5, 0, 1) eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"warn_perl"} and not $4 and substr($5, 0, 1) eq ">") 0 447 447
Condition33%2/6
$self->{"warn_perl"} and not $4 and substr($5, 0, 1) eq ">"
!ll&&!rl&&r
44700
$self->{"warn_perl"} and not $4
!ll&&!rl&&r
44700
MC/DC0%0/3
$self->{"warn_perl"} and not $4 and substr($5, 0, 1) eq ">"
$self->{"warn_perl"} not $4 substr($5, 0, 1) eq ">"
Truth table25%1/4
A and not(B) and C
A= $self->{"warn_perl"}B= not $4C= substr($5, 0, 1) eq ">"
ABCresult
0XX0
10X0
1100
1111
162 0 $self->_warn("Probably using Perl syntax instead of INI: $line");
163 }
164  
165 447 ▶ if (!$self->{allow_duplicate_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"allow_duplicate_key"}) 2 445 447
166 2 my $kmem = $self->{_key_mem};
167 2 ▶ if ($kmem->{$self->{_cur_section}}{$2}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$kmem{$self->{"_cur_section"}}{$2}) 1 1 2
168 1 $self->_err(
169 "Duplicate key: $2 (section $self->{_cur_section})");
170 }
171 1 $kmem->{$self->{_cur_section}}{$2} = 1;
172 }
173 446 next LINE;
174 }
175  
176 5 $self->_err("Invalid syntax");
177 }
178  
179 # make sure we always end with newline
180 107 ▶ if (@$res) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$res) 98 9 107
181 98 ▶ $res->[-1][-1] .= "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[-1][-1] =~ /\R\z/ 2 96 98
182 unless $res->[-1][-1] =~ /\R\z/;
183 }
184  
185 107 require Config::IOD::Document;
186 107 Config::IOD::Document->new(_parser=>$self, _parsed=>$res);
187 }
188  
189 18 1;
190 # ABSTRACT: Read and write IOD/INI configuration files
191  
192 __END__
« Config/IOD/INI.pm ↑ Summary Config/IOD/Document.pm »