INI/Writer.pm

filter: Statementstmt 100% 95 / 95 Branchbran 89.4% 34 / 38 Conditioncond 72.2% 13 / 18 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 22 / 22 Podpod 94.4% 17 / 18 totaltotal 92.0% 184 / 200 CC 28 SCAR 15.6
CC
28
Cov
94%
validate_input
9.1
preprocess_input
9.0
stringify_section
5.0
CRAP 4.8
↑ Summary INI/Reader.pm »
1 1 ▶use v5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Config::INI::Writer 0.029;
4  
5 1 ▶use Mixin::Linewise::Writers;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: a subclassable .ini-file emitter
7  
8 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 our @CARP_NOT = qw(Mixin::Linewise::Writers);
10  
11 #pod =head1 SYNOPSIS
12 #pod
13 #pod If C<$hash> contains:
14 #pod
15 #pod {
16 #pod '_' => { admin => 'rjbs' },
17 #pod rjbs => {
18 #pod awesome => 'yes',
19 #pod height => q{5' 10"},
20 #pod },
21 #pod mj => {
22 #pod awesome => 'totally',
23 #pod height => '23"',
24 #pod },
25 #pod }
26 #pod
27 #pod Then when your program contains:
28 #pod
29 #pod Config::INI::Writer->write_file($hash, 'family.ini');
30 #pod
31 #pod F<family.ini> will contains:
32 #pod
33 #pod admin = rjbs
34 #pod
35 #pod [rjbs]
36 #pod awesome = yes
37 #pod height = 5' 10"
38 #pod
39 #pod [mj]
40 #pod awesome = totally
41 #pod height = 23"
42 #pod
43 #pod =head1 DESCRIPTION
44 #pod
45 #pod Config::INI::Writer is I<yet another> config module implementing I<yet another>
46 #pod slightly different take on the undeniably easy to read L<".ini" file
47 #pod format|Config::INI>. Its default behavior is quite similar to that of
48 #pod L<Config::Tiny>, on which it is based.
49 #pod
50 #pod The chief difference is that Config::INI::Writer is designed to be subclassed
51 #pod to allow for side-effects and self-reconfiguration to occur during the course
52 #pod of reading its input.
53 #pod
54 #pod =head1 METHODS FOR WRITING CONFIG
55 #pod
56 #pod There are three writer methods, C<write_string>, C<write_file>, and
57 #pod C<write_handle>. The first two are implemented in terms of the third. It
58 #pod iterates over a collection of data, emitting lines to the filehandle as it
59 #pod goes. The lines are generated by events produced by iterating over the data.
60 #pod Those events are detailed below in the L</METHODS FOR SUBCLASSING> section.
61 #pod
62 #pod The given data should be a hashref of hashrefs:
63 #pod
64 #pod {
65 #pod section_name_1 => { prop1 => 'value1', prop2 => 'value2' },
66 #pod section_name_2 => ...
67 #pod }
68 #pod
69 #pod ...or an arrayref of section name and arrayref pairs:
70 #pod
71 #pod [
72 #pod section_name_1 => [ prop1 => 'value1', prop2 => 'value2' ],
73 #pod section_name_2 => ...
74 #pod ]
75 #pod
76 #pod ...or a combination of those:
77 #pod
78 #pod [
79 #pod section_name_1 => { prop1 => 'value1', prop2 => 'value2' },
80 #pod section_name_2 => [ prop3 => 'value3', prop4 => 'value4' ],
81 #pod section_name_3 => ...
82 #pod ]
83 #pod
84 #pod
85 #pod All the reader methods throw an exception when they encounter an error.
86 #pod
87 #pod =head2 write_file
88 #pod
89 #pod Config::INI::Writer->write_file($input, $filename);
90 #pod
91 #pod This method writes out the configuration represented by C<$data> to the file
92 #pod named by C<$filename>. If a file by that name exists, it is overwritten.
93 #pod
94 #pod This method will either succeed or raise an exception. (Its return value is
95 #pod not defined.)
96 #pod
97 #pod =head2 write_string
98 #pod
99 #pod my $string = Config::INI::Writer->write_string($input);
100 #pod
101 #pod This method returns a string containing the INI content describing the given
102 #pod data.
103 #pod
104 #pod =head2 write_handle
105 #pod
106 #pod Config::INI::Writer->write_handle($input, $handle);
107 #pod
108 #pod This method writes the data in C<$data> to the IO::Handle-like object in
109 #pod C<$handle>. This method should either succeed or throw an exception.
110 #pod
111 #pod =cut
112  
113 16 ▶sub write_handle {
Subroutine write_handle: called CC=4 SCAR=13.9
Pod documented
114 16 my ($invocant, $input, $handle) = @_;
115  
116 16 ▶ my $self = ref $invocant ? $invocant : $invocant->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $invocant ? : 3 13 16
117  
118 16 $input = $self->preprocess_input($input);
119  
120 15 $self->validate_input($input);
121  
122 12 my $starting_section_name = $self->starting_section;
123  
124 12 ▶ SECTION: for (my $i = 0; $i < $#$input; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $input->$#*) 21 12 33
125 21 my ($section_name, $section_data) = @$input[ $i, $i + 1 ];
126  
127 21 $self->change_section($section_name);
128 21 ▶ $handle->print($self->stringify_section($section_data))
Branch: 1 branch
BranchTrueFalseTotal
$handle->print($self->stringify_section($section_data)) or Carp::croak("error writing section ${section_name}: $!") 21 0 21
129 or Carp::croak "error writing section $section_name: $!";
130 21 $self->finish_section;
131 }
132 }
133  
134 #pod =head1 METHODS FOR SUBCLASSING
135 #pod
136 #pod These are the methods you need to understand and possibly change when
137 #pod subclassing Config::INI::Reader to handle a different format of input.
138 #pod
139 #pod =head2 preprocess_input
140 #pod
141 #pod my $processed_input = $writer->preprocess_input($input_data);
142 #pod
143 #pod This method is called to ensure that the data given to the C<write_*> methods
144 #pod are in a canonical form for processing and emitting. The default
145 #pod implementation converts hashrefs to arrayrefs and, if the input is a hashref,
146 #pod moves the L</starting_section> to the beginning of the produced arrayref.
147 #pod
148 #pod In other words, given:
149 #pod
150 #pod {
151 #pod section_1 => { a => 1, b => 2 },
152 #pod section_2 => { c => 3, c => 4 },
153 #pod _ => { d => 5, e => 6 },
154 #pod }
155 #pod
156 #pod This method will return:
157 #pod
158 #pod [
159 #pod _ => [ d => 5, e => 6 ],
160 #pod section_2 => [ c => 3, c => 4 ],
161 #pod section_1 => [ a => 1, b => 2 ],
162 #pod ]
163 #pod
164 #pod The only guaranteed ordering when hashes are provided as input is that the
165 #pod starting section will appear first.
166 #pod
167 #pod =cut
168  
169 16 ▶sub preprocess_input {
Subroutine preprocess_input: called CC=9 SCAR=22.0
Pod documented
170 16 my ($self, $data) = @_;
171  
172 16 my @new_data;
173  
174 16 ▶ if (ref $data eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 8 8 16
elsif (ref $data eq 'ARRAY') { } 7 1 8
175 8 my $starting_section_name = $self->starting_section;
176  
177 8 for my $name (
178 $starting_section_name,
179 11 grep { $_ ne $starting_section_name } keys %$data
180 ) {
181 16 my $props = $data->{ $name };
182 16 ▶ next unless defined $props;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $props 5 11 16
183 11 ▶ push @new_data,
Branch: 1 branch
BranchTrueFalseTotal
(ref $props || '') eq 'HASH' ? : 11 0 11
Condition50%1/2
ref $props || ''
l!l
110
MC/DC0%0/1
ref $props || ''
ref $props
Truth table50%1/2
A
A= ref $props
Aresult
11
00
184 $name => ((ref($props) || '') eq 'HASH') ? [ %$props ] : $props;
185 }
186 } elsif (ref $data eq 'ARRAY') {
187 7 ▶ for (my $i = 0; $i < $#$data; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $data->$#*) 14 7 21
188 14 my ($name, $props) = @$data[ $i, $i + 1 ];
189 14 ▶ push @new_data, $name, (ref $props eq 'HASH') ? [ %$props ] : $props;
Branch: 1 branch
BranchTrueFalseTotal
ref $props eq 'HASH' ? : 1 13 14
190 }
191 } else {
192 1 my $class = ref $self;
193 1 Carp::croak "can't output $data via $class";
194 }
195  
196 15 return \@new_data;
197 }
198  
199 #pod =head2 validate_section_name
200 #pod
201 #pod Carp::croak "section name contains illegal character"
202 #pod if not $writer->is_valid_section_name($name);
203 #pod
204 #pod =cut
205  
206 25 ▶sub is_valid_section_name {
Subroutine is_valid_section_name: called CC=1 SCAR=0.0
Pod undocumented
207 25 my ($self, $name) = @_;
208 25 return $name !~ qr/(?:\n|\s;|^\s|\s$)/;
209 }
210  
211 #pod =head2 is_valid_property_name
212 #pod
213 #pod Carp::croak "property name contains illegal character"
214 #pod if not $writer->is_valid_property_name($name);
215 #pod
216 #pod =cut
217  
218 49 ▶sub is_valid_property_name {
Subroutine is_valid_property_name: called CC=1 SCAR=0.0
Pod documented
219 49 my ($self, $property) = @_;
220 49 return $property !~ qr/(?:\n|\s;|^\s|\s|=$)/;
221 }
222  
223 #pod =head2 is_valid_value
224 #pod
225 #pod Carp::croak "value contains illegal character"
226 #pod if not $writer->is_valid_value($name);
227 #pod
228 #pod =cut
229  
230 45 ▶sub is_valid_value {
Subroutine is_valid_value: called CC=1 SCAR=0.0
Pod documented
231 45 my ($self, $value) = @_;
232 45 return $value !~ qr/(?:\n|\s;|^\s|\s$)/;
233 }
234  
235 #pod =head2 validate_input
236 #pod
237 #pod $writer->validate_input($input);
238 #pod
239 #pod This method is called on the input data once they've been preprocessed by
240 #pod C<L</preprocess_input>>.
241 #pod
242 #pod It ensures that the processed input is structurally sound before beginning to
243 #pod output it. For example, it ensures that no property is ever assigned more than
244 #pod once in a given section.
245 #pod
246 #pod This method either raises an exception or it doesn't.
247 #pod
248 #pod =cut
249  
250 15 ▶sub validate_input {
Subroutine validate_input: called CC=9 SCAR=22.1
Pod documented
251 15 my ($self, $input) = @_;
252  
253 15 my %seen;
254 15 ▶ for (my $i = 0; $i < $#$input; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $input->$#*) 25 12 37
255 25 my ($name, $props) = @$input[ $i, $i + 1 ];
256 25 ▶ $seen{ $name } ||= {};
Condition100%2/2
$seen{$name} ||= {}
l!l
322
MC/DC100%1/1
$seen{$name} ||= {}
$seen{$name}
Truth table100%2/2
A
A= $seen{$name}
Aresult
11
00
257  
258 25 ▶ Carp::croak "illegal section name '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_section_name($name) 2 23 25
259 if not $self->is_valid_section_name($name);
260  
261 23 ▶ for (my $j = 0; $j < $#$props; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $props->$#*) 49 22 71
262 49 my $property = $props->[ $j ];
263 49 my $value = $props->[ $j + 1 ];
264  
265 49 ▶ Carp::croak "property name '$property' contains illegal character"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_property_name($property) 0 49 49
266 if not $self->is_valid_property_name($property);
267  
268 49 ▶ Carp::croak "value for $name.$property contains illegal character"
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and not $self->is_valid_value($value) 0 49 49
Condition66%2/3
defined $value and not $self->is_valid_value($value)
!ll&&!rl&&r
4450
MC/DC0%0/2
defined $value and not $self->is_valid_value($value)
defined $value not $self->is_valid_value($value)
Truth table66%2/3
A and not(B)
A= defined $valueB= not $self->is_valid_value($value)
ABresult
0X0
100
111
269 if defined $value and not $self->is_valid_value($value);
270  
271 49 ▶ if ( $seen{ $name }{ $property }++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$name}{$property}++) 1 48 49
272 1 Carp::croak "multiple assignments found for $name.$property";
273 }
274 }
275 }
276 }
277  
278 #pod =head2 change_section
279 #pod
280 #pod $writer->change_section($section_name);
281 #pod
282 #pod This method is called each time a new section is going to be written out. If
283 #pod the same section appears twice in a row in the input, this method will still be
284 #pod called between instances of that section.
285 #pod
286 #pod In other words, given this input:
287 #pod
288 #pod [
289 #pod section_1 => [ a => 1 ],
290 #pod section_1 => [ b => 2 ],
291 #pod ]
292 #pod
293 #pod C<change_section> will be called twice: once before the first C<section_1> and
294 #pod once before the second C<section_1>.
295 #pod
296 #pod =cut
297  
298 21 ▶sub change_section {
Subroutine change_section: called CC=1 SCAR=0.0
Pod documented
299 21 my ($self, $section_name) = @_;
300  
301 21 $self->{current_section} = $section_name;
302 }
303  
304 #pod =head2 current_section
305 #pod
306 #pod $writer->current_section
307 #pod
308 #pod This method returns the section currently being written out.
309 #pod
310 #pod =cut
311  
312 58 ▶sub current_section {
Subroutine current_section: called CC=1 SCAR=0.0
Pod documented
313 58 my ($self) = @_;
314 58 return $self->{current_section};
315 }
316  
317 #pod =head2 finish_section
318 #pod
319 #pod $writer->finish_section
320 #pod
321 #pod This method is called after all of the current section's properties have been
322 #pod written.
323 #pod
324 #pod =cut
325  
326 21 ▶sub finish_section {
Subroutine finish_section: called CC=1 SCAR=0.0
Pod documented
327 21 my ($self) = @_;
328 21 return $self->{did_section}{ $self->current_section }++;
329 }
330  
331 #pod =head2 done_sections
332 #pod
333 #pod my @names = $writer->done_sections;
334 #pod
335 #pod This method returns a list of all sections that have been written out and
336 #pod finished. The fact that a section name is returned by C<done_sections> does
337 #pod not mean that there will be no more data for that section, but that at least
338 #pod one entire set of data has been written out for it.
339 #pod
340 #pod =cut
341  
342 23 ▶sub done_sections {
Subroutine done_sections: called CC=1 SCAR=0.0
Pod documented
343 23 my ($self) = @_;
344 23 return keys %{ $self->{did_section} };
345 }
346  
347 #pod =head2 stringify_section
348 #pod
349 #pod my $string = $writer->stringify_section($props);
350 #pod
351 #pod This method returns a string assigning all the properties set in the given
352 #pod data. This still will include the section header, if needed. (The only case
353 #pod in which it is not needed is when the C<L</explicit_starting_header>> method
354 #pod returns false, no other sections have been done, and the section about to be
355 #pod stringified is the C<L</starting_section>>.
356 #pod
357 #pod This method is implemented in terms of C<L</stringify_section_header>> and
358 #pod C<L</stringify_section_data>>.
359 #pod
360 #pod =cut
361  
362 21 ▶sub stringify_section {
Subroutine stringify_section: called CC=5 SCAR=16.2
Pod documented
363 21 my ($self, $section_data) = @_;
364  
365 21 my $output = '';
366  
367 21 my $current_section_name = $self->current_section;
368 21 my $starting_section_name = $self->starting_section;
369  
370 21 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($starting_section_name and $starting_section_name eq $current_section_name and not $self->done_sections and not $self->explicit_starting_header) 16 5 21
Condition77%7/9
$starting_section_name and $starting_section_name eq $current_section_name and not $self->done_sections and not $self->explicit_starting_header
!ll&&!rl&&r
1605
$starting_section_name and $starting_section_name eq $current_section_name and not $self->done_sections
!ll&&!rl&&r
1425
$starting_section_name and $starting_section_name eq $current_section_name
!ll&&!rl&&r
0147
MC/DC50%2/4
$starting_section_name and $starting_section_name eq $current_section_name and not $self->done_sections and not $self->explicit_starting_header
$starting_section_name $starting_section_name eq $current_section_name not $self->done_sections not $self->explicit_starting_header
Truth table60%3/5
A and B and not(C) and not(D)
A= $starting_section_nameB= $starting_section_name eq $current_section_nameC= not $self->done_sectionsD= not $self->explicit_starting_header
ABCDresult
0XXX0
10XX0
110X0
11100
11111
371 $starting_section_name
372 and $starting_section_name eq $current_section_name
373 and ! $self->done_sections
374 and ! $self->explicit_starting_header
375 ) {
376 16 $output .= $self->stringify_section_header($self->current_section);
377 }
378  
379 21 $output .= $self->stringify_section_data($section_data);
380  
381 21 return $output;
382 }
383  
384 #pod =head2 stringify_section_data
385 #pod
386 #pod my $string = $writer->stringify_section_data($props)
387 #pod
388 #pod This method returns a string containing a series of lines, each containing a
389 #pod value assignment for the given properties.
390 #pod
391 #pod =cut
392  
393 21 ▶sub stringify_section_data {
Subroutine stringify_section_data: called CC=2 SCAR=6.9
Pod documented
394 21 my ($self, $values) = @_;
395  
396 21 my $output = '';
397  
398 21 ▶ for (my $i = 0; $i < $#$values; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $values->$#*) 47 21 68
399 47 $output .= $self->stringify_value_assignment(@$values[ $i, $i + 1]);
400 }
401  
402 21 return $output;
403 }
404  
405 #pod =head2 stringify_value_assignment
406 #pod
407 #pod my $string = $writer->stringify_value_assignment($name => $value);
408 #pod
409 #pod This method returns a string that assigns a value to a named property. If the
410 #pod value is undefined, an empty string is returned.
411 #pod
412 #pod =cut
413  
414 47 ▶sub stringify_value_assignment {
Subroutine stringify_value_assignment: called CC=2 SCAR=6.9
Pod documented
415 47 my ($self, $name, $value) = @_;
416  
417 47 ▶ return '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 4 43 47
418  
419 43 return $name . ' = ' . $self->stringify_value($value) . "\n";
420 }
421  
422 #pod =head2 stringify_value
423 #pod
424 #pod my $string = $writer->stringify_value($value);
425 #pod
426 #pod This method returns the string that will represent the given value in a
427 #pod property assignment.
428 #pod
429 #pod =cut
430  
431 43 ▶sub stringify_value {
Subroutine stringify_value: called CC=2 SCAR=7.1
Pod documented
432 43 my ($self, $value) = @_;
433  
434 43 ▶ $value //= q{};
Condition50%1/2
$value //= ""
l!l
430
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
435  
436 43 return $value;
437 }
438  
439 #pod =head2 stringify_section_header
440 #pod
441 #pod my $string = $writer->stringify_section_header($section_name);
442 #pod
443 #pod This method returns the string (a line) that represents the given section name.
444 #pod Basically, this returns:
445 #pod
446 #pod [section_name]
447 #pod
448 #pod =cut
449  
450 16 ▶sub stringify_section_header {
Subroutine stringify_section_header: called CC=2 SCAR=6.9
Pod documented
451 16 my ($self, $section_name) = @_;
452  
453 16 my $output = '';
454 16 ▶ $output .= "\n" if $self->done_sections;
Branch: 1 branch
BranchTrueFalseTotal
if $self->done_sections 9 7 16
455 16 $output .= "[$section_name]\n";
456  
457 16 return $output;
458 }
459  
460 #pod =head2 starting_section
461 #pod
462 #pod This method returns the name of the starting section. If this section appears
463 #pod first (as it will, when given a hashref as input) and if
464 #pod C<L</explicit_starting_header>> returns false, its section header can be
465 #pod omitted.
466 #pod
467 #pod =cut
468  
469 41 ▶sub starting_section { return '_' }
Subroutine starting_section: called CC=1 SCAR=0.0
Pod documented
470  
471 #pod =head2 explicit_starting_header
472 #pod
473 #pod If this method returns true (which it does I<not>, by default), then the
474 #pod section header for the starting section will be emitted, even if it appears
475 #pod first.
476 #pod
477 #pod =cut
478  
479 5 ▶sub explicit_starting_header { 0 }
Subroutine explicit_starting_header: called CC=1 SCAR=0.0
Pod documented
480  
481 #pod =head2 new
482 #pod
483 #pod
484 #pod my $reader = Config::INI::Writer->new;
485 #pod
486 #pod This method returns a new writer. This generally does not need to be called by
487 #pod anything but the various C<write_*> methods, which create a writer object only
488 #pod ephemerally.
489 #pod
490 #pod =cut
491  
492 16 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
493 16 my ($class) = @_;
494  
495 16 my $self = bless { did_section => {} } => $class;
496  
497 16 return $self;
498 }
499  
500 1 1;
501  
502 __END__
↑ Summary INI/Reader.pm »