Parser/Ini.pm

filter: Statementstmt 75.0% 51 / 68 Branchbran 53.5% 15 / 28 Conditioncond 71.4% 5 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 69.2% 81 / 117 CC 19 SCAR 32.4
CC
19
Cov
69%
_readconfig
36.4
parse
2.0
filename
2.0
CRAP 25.6
↑ Summary Parser.pm »
1 package Config::Parser::Ini;
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use parent 'Config::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 20 ▶sub parse {
Subroutine parse: called CC=2 SCAR=7.0
Pod documented
9 20 my $self = shift;
10 20 ▶ $self->{_filename} = shift // confess "No filename given";
Condition50%1/2
shift() // confess("No filename given")
l!l
200
MC/DC0%0/1
shift() // confess("No filename given")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
11 20 local %_ = @_;
12 20 $self->debug(1, "parsing $self->{_filename}");
13 20 $self->_readconfig($self->{_filename}, %_);
14 20 return $self;
15 }
16  
17 0 ▶sub filename { shift->{_filename} }
Subroutine filename: not called CC=1 SCAR=6.9
Pod documented
18
19 # _readconfig(FILE)
20 20 ▶sub _readconfig {
Subroutine _readconfig: called CC=18 SCAR=36.0
21 20 my $self = shift;
22 20 my $file = shift;
23 20 local %_ = @_;
24 20 my $fh = delete $_{fh};
25 20 my $need_close;
26  
27 20 $self->debug(1, "reading file $file");
28 20 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 9 11 20
29 open($fh, "<", $file)
30 9 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or do { $self->error("can't open configuration file ${file}: $!"); ++$self->{"_error_count"}; return 0 } 9 0 9
31 0 $self->error("can't open configuration file $file: $!");
32 0 $self->{_error_count}++;
33 0 return 0;
34 };
35 9 $need_close = 1;
36 }
37
38 20 ▶ my $line = delete $_{line} // 0;
Condition100%2/2
delete $_{"line"} // 0
l!l
119
MC/DC100%1/1
delete $_{"line"} // 0
delete $_{"line"}
Truth table100%2/2
A
A= delete $_{"line"}
Aresult
11
00
39 20 my @path;
40 my $include;
41
42 20 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 135 20 155
43 135 ++$line;
44 135 chomp;
45 135 ▶ if (/\\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\\$/) 0 135 135
46 0 chop;
47 0 $_ .= <$fh>;
48 0 redo;
49 }
50  
51 135 s/^\s+//;
52 135 s/\s+$//;
53 135 s/#.*//;
54 135 ▶ next if ($_ eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 19 116 135
55
56 116 my $locus = new Text::Locus($file, $line);
57
58 116 ▶ if (/^\[(.+?)\]$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\[(.+?)\]$/) { } 37 79 116
elsif (/([\w_-]+)\s*=\s*(.*)/) { } 79 0 79
59 37 @path = parse_line('\s+', 0, $1);
60 37 ▶ if (@path == 1 && $path[0] eq 'include') {
Branch: 1 branch
BranchTrueFalseTotal
if (@path == 1 and $path[0] eq 'include') { } 0 37 37
Condition66%2/3
@path == 1 and $path[0] eq "include"
!ll&&!rl&&r
4330
MC/DC0%0/2
@path == 1 and $path[0] eq "include"
@path == 1 $path[0] eq "include"
Truth table66%2/3
A and B
A= @path == 1B= $path[0] eq "include"
ABresult
0X0
100
111
61 0 $include = 1;
62 } else {
63 37 $include = 0;
64 37 $self->add_node(\@path,
65 new Config::AST::Node::Section($self, locus => $locus));
66 }
67 } elsif (/([\w_-]+)\s*=\s*(.*)/) {
68 79 my ($k, $v) = ($1, $2);
69
70 79 ▶ if ($include) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include) { } 0 79 79
71 0 ▶ if ($k eq 'path') {
Branch: 3 branches
BranchTrueFalseTotal
if ($k eq 'path') { } 0 0 0
elsif ($k eq 'pathopt') { } 0 0 0
elsif ($k eq 'glob') { } 0 0 0
72 0 $self->_readconfig($v);
73 } elsif ($k eq 'pathopt') {
74 0 ▶ $self->_readconfig($v) if -f $v;
Branch: 1 branch
BranchTrueFalseTotal
if -f $v 0 0 0
75 } elsif ($k eq 'glob') {
76 0 foreach my $file (bsd_glob($v, 0)) {
77 0 $self->_readconfig($file);
78 }
79 } else {
80 0 $self->error("keyword \"$k\" is unknown", locus => $locus);
81 0 $self->{_error_count}++;
82 }
83 } else {
84 79 $self->add_value([@path, $k], $v, $locus);
85 }
86 } else {
87 0 $self->error("malformed line", locus => $locus);
88 0 $self->{_error_count}++;
89 }
90 }
91 20 ▶ close $fh if $need_close;
Branch: 1 branch
BranchTrueFalseTotal
if $need_close 9 11 20
92 }
93  
94 10 1;
95  
96 =head1 NAME
97  
98 Config::Parser::Ini - configuration file parser for ini-style files
99  
100 =head1 SYNOPSIS
101  
102 $cfg = new Config::Parser::Ini($filename);
103  
104 $val = $cfg->get('dir', 'tmp');
105  
106 print $val->value;
107  
108 print $val->locus;
109  
110 $val = $cfg->tree->Dir->Tmp;
111  
112 =head1 DESCRIPTION
113  
114 An I<ini-style configuration file> is a textual file consisting of settings
115 grouped into one or more sections. A I<setting> has the form
116  
117 KEYWORD = VALUE
118  
119 where I<KEYWORD> is the setting name and I<VALUE> is its value.
120 Syntactically, I<VALUE> is anything to the right of the equals sign and up
121 to the linefeed character terminating the line (ASCII 10), not including
122 the leading and trailing whitespace characters.
123  
124 Each setting occupies one line. Very long lines can be split over several
125 physical lines by ending each line fragment except the last with a backslash
126 character appearing right before the linefeed character.
127  
128 A I<section> begins with a section declaration in the following form:
129  
130 [NAME NAME...]
131  
132 Here, square brackets form part of the syntax. Any number of I<NAME>s
133 can be present inside the square brackets. The first I<NAME> must follow the
134 usual rules for a valid identifier name. Rest of I<NAME>s can contain any
135 characters, provided that any I<NAME> that includes non-alphanumeric characters
136 is enclosed in a pair of double-quotes. Any double-quotes and backslash
137 characters appearing within the quoted string must be escaped by prefixing
138 them with a single backslash.
139  
140 The B<Config::Parser::Ini> module is a framework for parsing such files.
141  
142 In the simplest case, the usage of this module is as simple as in the following
143 fragment:
144  
145 use Config::Parser::Ini;
146 my $cf = new Config::Parser::Ini(filename => "config.ini");
147  
148 On success, this returns a valid B<Config::Parser::Ini> object. On error,
149 the diagnostic message is issued using the B<error> method (see the description
150 of the method in L<Config::AST>(3)) and the module croaks.
151  
152 This usage, although simple, has one major drawback - no checking is performed
153 on the input file, except for the syntax check. To fix this, you can supply
154 a dictionary (or I<lexicon>) of allowed keywords along with their values.
155 Such a dictionary is itself a valid ini file, where the value of each
156 keyword describes its properties. The dictionary is placed in the B<__DATA__>
157 section of the source file which invokes the B<Config::Parser::Ini> constructor.
158  
159 Expanding the example above:
160  
161 use Config::Parser::Ini;
162 my $cf = new Config::Parser::Ini(filename => "config.ini");
163  
164 __DATA__
165 [core]
166 root = STRING :default /
167 umask = OCTAL
168 [user]
169 uid = NUMBER
170 gid = NUMBER
171  
172 This code specifies that the configuration file can contain at most two
173 sections: C<[core]> and C<[user]>. Two keywords are defined within each
174 section. Data types are specified for each keyword, so the parser will
175 bail out in case of type mismatches. If the B<core.root> setting is not
176 present in the configuration, the default one will be created with the
177 value C</>.
178  
179 It is often advisable to create a subclass of B<Config::Parser::Ini> and
180 use it for parsing. For instance:
181  
182 package App::MyConf;
183 use Config::Parser::Ini;
184 1;
185 __DATA__
186 [core]
187 root = STRING :default /
188 umask = OCTAL
189 [user]
190 uid = NUMBER
191 gid = NUMBER
192  
193 Then, to parse the configuration file, it will suffice to do:
194  
195 $cf = my App::MyConf(filename => "config.ini");
196  
197 One advantage of this approach is that it will allow you to install
198 additional validation for the configuration statements using the
199 B<:check> option. The argument to this option is the name of a
200 method which will be invoked after parsing the statement in order
201 to verify its value. It is described in detail below (see the section
202 B<SYNTAX DEFINITION> in the documentation of L<Config::Parser>).
203 For example, if you wish to ensure that the value of the C<root> setting
204 in C<core> section points to an existing directory, you would do:
205  
206 package App::MyConf;
207 use Config::Parser::Ini;
208  
209 sub dir_exists {
210 my ($self, $valref, $prev_value, $locus) = @_;
211  
212 unless (-d $$valref) {
213 $self->error("$$valref: directory does not exist",
214 locus => $locus);
215 return 0;
216 }
217 return 1;
218 }
219 1;
220 __DATA__
221 [core]
222 root = STRING :default / :check=dir_exists
223 umask = OCTAL
224 [user]
225 uid = NUMBER
226 gid = NUMBER
227  
228 =head1 CONSTRUCTOR
229  
230 $cfg = new Config::Parser::Ini(%opts)
231  
232 Creates a new parser object. Keyword arguments are:
233  
234 =over 4
235  
236 =item B<filename>
237  
238 Name of the file to parse. If not supplied, you will have to
239 call the B<$cfg-E<gt>parse> method explicitly after you are returned a
240 valid B<$cfg>.
241  
242 =item B<line>
243  
244 Optional line where the configuration starts in B<filename>. It is used to
245 keep track of statement location in the file for correct diagnostics. If
246 not supplied, B<1> is assumed.
247  
248 =item B<fh>
249  
250 File handle to read from. If it is not supplied, new handle will be
251 created by using B<open> on the supplied filename.
252  
253 =item B<lexicon>
254  
255 Dictionary of allowed configuration statements in the file. You will not
256 need this parameter. It is listed here for completeness sake. Refer to
257 the L<Config::AST> constructor for details.
258  
259 =back
260  
261 =head1 METHODS
262  
263 All methods are inherited from L<Config::Parser>. Please see its
264 documentation for details.
265  
266 =head1 SEE ALSO
267  
268 L<Config::Parser>(3), L<Config::AST>(3).
269  
270 =cut
↑ Summary Parser.pm »