lib/Config/Neat/Render.pm

filter: Statementstmt 96.7% 149 / 154 Branchbran 86.9% 80 / 92 Conditioncond 77.7% 42 / 54 MC/DCmcdc 59.3% 19 / 32 Subroutinesub 100% 13 / 13 Podpod 22.2% 2 / 9 totaltotal 86.1% 305 / 354 CC 69 SCAR 25.9
CC
69
Cov
90%
render_key_val
20.2
max_key_length
19.0
render_node_recursively
12.7
CRAP 13.3
« blib/lib/Config/Neat/Array.pm ↑ Summary lib/Config/Neat/Inheritable.pm »
1 =head1 NAME
2  
3 Config::Neat::Render - Render configs in Config::Neat format
4  
5 =head1 SYNOPSIS
6  
7 use Config::Neat::Render;
8  
9 my $r = Config::Neat::Render->new();
10  
11 my $data = {
12 'foo' => 'Hello, World!',
13 'bar' => [1, 2, 3],
14 'baz' => {
15 'etc' => ['foo bar', 'baz', '', 1]
16 }
17 };
18  
19 print $r->render($data);
20  
21 The output will be:
22  
23 bar 1 2 3
24  
25 baz
26 {
27 etc `foo bar` baz `` 1
28 }
29  
30 foo Hello, World!
31  
32 =head1 DESCRIPTION
33  
34 This module allows you to render Config::Neat-compatible structures from your data
35 (but read below for limitations). See
36 L<https://github.com/iafan/Config-Neat/blob/master/sample/readme.nconf>
37 for the detailed file syntax specification. For parsing, use L<Config::Neat>.
38  
39 =head2 METHODS
40  
41 =over 4
42  
43 =item B<< Config::Neat::Render->new([$options]) >>
44  
45 Constructs a new renderer object. $options is a reference to a hash containing
46 rendering options' overrides (see the RENDERING OPTIONS section below).
47  
48 =item B<< Config::Neat::Render->render($data[, $options]) >>
49  
50 Renders $data into a string and returns it. $options is a reference to a hash
51 containing rendering options' overrides (see the RENDERING OPTIONS section below).
52  
53 =back
54  
55 =head2 RENDERING OPTIONS
56  
57 =over 4
58  
59 =item B<< indentation >>
60  
61 A number of spaces to indent each nested block contents with.
62  
63 Default value: C<4>
64  
65 =item B<< key_spacing >>
66  
67 A number of spaces between a key and and a value.
68  
69 Default value: C<4>
70  
71 =item B<< wrap_width >>
72  
73 A suggested maximum width of each line in a multiline string or array.
74  
75 Default value: C<60>
76  
77 =item B<< brace_under >>
78  
79 If true, put the opening brace under the key name, not on the same line
80  
81 Default value: C<1> (true)
82  
83 =item B<< separate_blocks >>
84  
85 If true, surrond blocks with empty lines for better readability.
86  
87 Default value: C<1> (true)
88  
89 =item B<< align_all >>
90  
91 If true, align all values in the configuration file
92 (otherwise the values are aligned only within current block).
93  
94 Default value: C<1> (true)
95  
96 =item B<< sort >>
97  
98 Note that hashes in Perl do not guarantee the correct order, so blocks may have
99 individual parameters shuffled randomly. Set this option to a true value
100 if you want to sort keys alphabetically, or to a reference to an array holding
101 an ordered list of key names
102  
103 Default value: C<undef> (false)
104  
105 Example:
106  
107 my $data = {
108 'bar' => [1, 2, 3],
109 'baz' => {
110 'etc' => ['foo bar', 'baz', '', 1]
111 }
112 'foo' => 'Hello, World!',
113 };
114  
115 my @order = qw(foo bar baz);
116  
117 print $r->render($data, {sort => \@order});
118  
119 The output will be:
120  
121 foo Hello, World!
122 bar 1 2 3
123  
124 baz
125 {
126 etc `foo bar` baz `` 1
127 }
128  
129 =item B<< undefined_value >>
130  
131 A string representation of the value to emit for undefined values
132  
133 Default value: C<'NO'>
134  
135 =back
136  
137 =head1 LIMITATIONS
138  
139 Do not use L<Config::Neat::Render> in conjunction with L<Config::Neat> for
140 arbitrary data serialization/desrialization. JSON and YAML will work better
141 for this kind of task.
142  
143 Why? Because Config::Neat was primarily designed to allow easier configuration
144 file authoring and reading, and uses relaxed syntax where strings are treated like
145 space-separated arrays (and vice versa), and where there's no strict definition
146 for boolean types, no null values, etc.
147  
148 It's the developer's responsibility to treat any given parameter as a boolean,
149 or string, or an array. This means that once you serialize your string into
150 Config::Neat format and parse it back, it will be converted to an array,
151 and you will need to use `->as_string` method to get the value as string.
152  
153 In other words, when doing this:
154  
155 my $c = Config::Neat->new();
156 my $r = Config::Neat::Render->new();
157 my $parsed_data = $c->parse($r->render($arbitrary_data));
158  
159 $parsed_data will almost always be different from $arbitrary_data.
160  
161 However, doing this immediately after:
162  
163 my $parsed_data_2 = $c->parse($r->render($parsed_data));
164  
165 Should produce the same data structure again.
166  
167 =head1 COPYRIGHT
168  
169 Copyright (C) 2012-2015 Igor Afanasyev <igor.afanasyev@gmail.com>
170  
171 =head1 SEE ALSO
172  
173 L<https://github.com/iafan/Config-Neat>
174  
175 =cut
176  
177 package Config::Neat::Render;
178  
179 2 our $VERSION = '1.4';
180  
181 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
182  
183 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
184  
185 2 use Config::Neat::Util qw(new_ixhash is_number is_code is_hash is_array is_scalar
186 is_neat_array is_homogenous_simple_array hash_has_only_sequential_keys
187 2 ▶ hash_has_sequential_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
188 2 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 2 my $PARAM = 1;
191 2 my $BLOCK = 2;
192  
193 #
194 # Initialize object
195 #
196 sub new {
197 2 ▶ my ($class, $options) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
198  
199 2 my $default_options = {
200 indentation => 4, # number of spaces to indent each nested block contents with
201 key_spacing => 4, # number of spaces between a key and and a value
202  
203 wrap_width => 60, # a suggested maximum width of each line in a multiline string or array
204  
205 brace_under => 1, # if true, put the opening brace under the key name, not on the same line
206 separate_blocks => 1, # if true, surrond blocks with empty lines for better readability
207 align_all => 1, # if true, align all values in the configuration file
208 # (otherwise the values are aligned only within current block)
209  
210 sort => undef, # can be a true value if you want to sort keys alphabetically
211 # or a reference to an array with an ordered list of key names
212 undefined_value => 'NO' # default value to emit for undefined values
213 };
214  
215 2 ▶ $options = {} unless $options;
Branch: 1 branch
BranchTrueFalseTotal
unless $options 2 0 2
216 2 %$options = (%$default_options, %$options);
217  
218 2 my $self = {
219 _options => $options
220 };
221  
222 2 bless $self, $class;
223 2 return $self;
224 }
225  
226 # Renders a nested tree structure into a Config::Neat-compatible text representaion.
227 # @@@@@@@@
228 # CAUTION: Config::Neat::Render->render() and Config::Neat->parse()
229 # are NOT SYMMETRICAL and should not be used for arbitrary data
230 # serialization/deserialization.
231 #
232 # In other words, when doing this:
233 #
234 # my $c = Config::Neat->new();
235 # my $r = Config::Neat::Render->new();
236 # my $parsed_data = $c->parse($r->render($arbitrary_data));
237 #
238 # $parsed_data will almost always be different from $arbitrary_data.
239 # However, doing this immediately after:
240 #
241 # my $parsed_data_2 = $c->parse($r->render($parsed_data));
242 #
243 # Should produce the same data structure again.
244 #
245 # See README for more information.
246 # @@@@@@@@
247 sub render {
248 38 ▶ my ($self, $data, $options) = @_;
Subroutine render: called CC=4 SCAR=13.9
Pod documented
249  
250 38 ▶ $options = {} unless $options;
Branch: 1 branch
BranchTrueFalseTotal
unless $options 36 2 38
251 38 %$options = (%{$self->{_options}}, %$options);
252  
253 38 $options->{global_key_length} = 0;
254  
255 # convert an array into a hash with 0..n values
256 38 my $sort = $options->{sort};
257 38 ▶ if (ref($sort) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sort eq "ARRAY") 1 37 38
258 1 my %h;
259 1 @h{@$sort} = (0 .. scalar(@$sort) - 1);
260 1 $options->{sort} = \%h;
261 }
262  
263 sub max_key_length {
264 263 ▶ my ($node, $options, $indent, $recursive) = @_;
Subroutine max_key_length: called CC=19 SCAR=29.5
Pod undocumented
265  
266 263 my $len = 0;
267 263 ▶ if (is_hash($node)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_hash($node)) { } 140 123 263
elsif (is_neat_array($node) || is_array($node) and not is_homogenous_simple_array($node)) { } 8 115 123
268 140 foreach my $key (keys %$node) {
269 218 my $subnode = $node->{$key};
270  
271 218 ▶ if (is_array($subnode) && !is_homogenous_simple_array($subnode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array($subnode) and not is_homogenous_simple_array($subnode)) 1 217 218
Condition100%3/3
is_array($subnode) and not is_homogenous_simple_array($subnode)
!ll&&!rl&&r
21341
MC/DC100%2/2
is_array($subnode) and not is_homogenous_simple_array($subnode)
is_array($subnode) not is_homogenous_simple_array($subnode)
Truth table100%3/3
A and not(B)
A= is_array($subnode)B= not is_homogenous_simple_array($subnode)
ABresult
0X0
100
111
272 1 $subnode = convert_array_to_hash($subnode);
273 }
274  
275 218 my $key_len;
276 218 ▶ if (is_hash($subnode) && !exists $subnode->{''}) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash($subnode) and not exists $subnode->{''}) { } 93 125 218
Condition100%3/3
is_hash($subnode) and not exists $subnode->{""}
!ll&&!rl&&r
120593
MC/DC100%2/2
is_hash($subnode) and not exists $subnode->{""}
is_hash($subnode) not exists $subnode->{""}
Truth table100%3/3
A and not(B)
A= is_hash($subnode)B= not exists $subnode->{""}
ABresult
0X0
100
111
277 # do not take into account the length of a hash key
278 # if it doesn't contain default values (which we want to align as well)
279 } else {
280 125 $key_len = $indent + length($key);
281 # if the key contains spaces and will be wrapped
282 # with `...`, add two extra symbols
283 125 ▶ if ($key =~ m/\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\s/) 2 123 125
284 2 $key_len += 2;
285 }
286 125 ▶ $len = $key_len if $key_len > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $key_len > $len 105 20 125
287 }
288  
289 218 ▶ if ($recursive && (is_hash($subnode) || is_neat_array($subnode) || is_array($subnode))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive and is_hash($subnode) || is_neat_array($subnode) || is_array($subnode)) 109 11 120
Condition88%8/9
$recursive and is_hash($subnode) || is_neat_array($subnode) || is_array($subnode)
!ll&&!rl&&r
02135
is_hash($subnode) or is_neat_array($subnode) or is_array($subnode)
l!l&&r!l&&!r
203411
is_hash($subnode) or is_neat_array($subnode)
l!l&&r!l&&!r
9810515
MC/DC50%2/4
$recursive and is_hash($subnode) || is_neat_array($subnode) || is_array($subnode)
$recursive is_hash($subnode) is_neat_array($subnode) is_array($subnode)
Truth table60%3/5
A and B or C or D
A= $recursiveB= is_hash($subnode)C= is_neat_array($subnode)D= is_array($subnode)
ABCDresult
0XXX0
10000
11XX1
101X1
10011
290 207 ▶ my $sub_indent = is_hash($subnode) ? $options->{indentation} : 0;
Branch: 1 branch
BranchTrueFalseTotal
is_hash($subnode) ? : 98 109 207
291 207 my $child_len = max_key_length($subnode, $options, $indent + $sub_indent, $recursive);
292 207 my $key_len = $child_len;
293 207 ▶ $len = $key_len if $key_len > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $key_len > $len 63 144 207
294 }
295 }
296 ▶ } elsif ((is_neat_array($node) || is_array($node)) && !is_homogenous_simple_array($node)) {
Condition66%4/6
is_neat_array($node) || is_array($node) and not is_homogenous_simple_array($node)
!ll&&!rl&&r
01158
is_neat_array($node) or is_array($node)
l!l&&r!l&&!r
11940
MC/DC33%1/3
is_neat_array($node) || is_array($node) and not is_homogenous_simple_array($node)
is_neat_array($node) is_array($node) not is_homogenous_simple_array($node)
Truth table60%3/5
A or B and not(C)
A= is_neat_array($node)B= is_array($node)C= not is_homogenous_simple_array($node)
ABCresult
00X0
1X00
0100
1X11
0111
297 map {
298 8 my $child_len = max_key_length($_, $options, $indent + $options->{indentation}, $recursive);
299 18 my $key_len = $child_len;
300 18 ▶ $len = $key_len if $key_len > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $key_len > $len 2 16 18
301 } @$node;
302 }
303 263 return $len;
304 }
305  
306 sub convert_array_to_hash {
307 2 ▶ my $node = shift;
Subroutine convert_array_to_hash: called CC=2 SCAR=6.9
Pod undocumented
308  
309 2 my $i = 0;
310  
311 2 my $h = new_ixhash;
312  
313 2 foreach my $value (@$node) {
314 6 $h->{$i++} = $value;
315 }
316 2 return $h;
317 }
318  
319 sub render_wrapped_array {
320 126 ▶ my ($array, $options, $indent) = @_;
Subroutine render_wrapped_array: called CC=8 SCAR=20.8
Pod undocumented
321  
322 126 my $wrap_width = $options->{wrap_width};
323  
324 126 my @a;
325 126 my $line = '';
326 126 foreach my $item (@$array) {
327 250 ▶ my $l = $line ? length($line) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$line ? : 122 128 250
328  
329 250 ▶ if ($l + length($item) > $wrap_width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l + length($item) > $wrap_width) 10 240 250
330 10 ▶ push(@a, $line) if $line ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $line ne "" 8 2 10
331 10 $line = '';
332 }
333  
334 250 ▶ if (length($item) >= $wrap_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $item >= $wrap_width) { } 2 248 250
335 2 push(@a, $item);
336 } else {
337 248 ▶ $line .= ' ' if $line ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $line ne "" 114 134 248
338 248 $line .= $item;
339 }
340 }
341 126 ▶ push(@a, $line) if $line ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $line ne "" 126 0 126
342  
343 126 return join("\n".(' ' x $indent), @a);
344 }
345  
346 sub render_scalar {
347 224 ▶ my ($scalar, $options, $indent, $should_escape) = @_;
Subroutine render_scalar: called CC=9 SCAR=22.1
Pod undocumented
348  
349 # dereference scalar
350 224 ▶ $scalar = $$scalar if ref($scalar) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $scalar eq "SCALAR" 0 224 224
351  
352 224 $scalar =~ s/`/\\`/g;
353  
354 224 ▶ if ($scalar =~ m/(\n|\s{2,})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalar =~ /(\n|\s{2,})/) 0 224 224
355 0 $should_escape = 1;
356 }
357  
358 224 ▶ if (!defined $scalar) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scalar) 1 223 224
359 1 $scalar = $options->{undefined_value};
360 }
361  
362 224 ▶ if ($scalar eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalar eq "") 8 216 224
363 8 $scalar = '``';
364 }
365  
366 224 ▶ if ($should_escape and $scalar =~ m/\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_escape and $scalar =~ /\s/) 8 216 224
Condition100%3/3
$should_escape and $scalar =~ /\s/
!ll&&!rl&&r
112058
MC/DC100%2/2
$should_escape and $scalar =~ /\s/
$should_escape $scalar =~ /\s/
Truth table100%3/3
A and B
A= $should_escapeB= $scalar =~ /\s/
ABresult
0X0
100
111
367 8 $scalar = '`'.$scalar.'`';
368 }
369  
370 224 ▶ if (!$should_escape and $scalar ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $should_escape and $scalar ne "") 11 213 224
Condition66%2/3
not $should_escape and $scalar ne ""
!ll&&!rl&&r
213011
MC/DC50%1/2
not $should_escape and $scalar ne ""
not $should_escape $scalar ne ""
Truth table66%2/3
not(A) and B
A= not $should_escapeB= $scalar ne ""
ABresult
0X0
100
111
371 11 my @a = split(/\s+/, $scalar);
372 11 return render_wrapped_array(\@a, $options, $indent);
373 }
374  
375 213 return $scalar;
376 }
377  
378 sub pad {
379 126 ▶ my ($s, $width) = @_;
Subroutine pad: called CC=2 SCAR=6.9
Pod undocumented
380 126 my $spaces = $width - length($s);
381 126 ▶ return ($spaces <= 0) ? $s : $s . ' ' x $spaces;
Branch: 1 branch
BranchTrueFalseTotal
$spaces <= 0 ? : 66 60 126
382 }
383  
384 sub render_key_val {
385 236 ▶ my ($options, $key_length, $indent, $wasref, $array_mode, $sequential_keys, $key, $val) = @_;
Subroutine render_key_val: called CC=19 SCAR=30.1
Pod undocumented
386  
387 236 my $text = '';
388 236 my $space_indent = (' ' x $indent);
389  
390 # if the key name contains whitespace, wrap it in backticks
391 236 ▶ if ($key =~ m/\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\s/) 2 234 236
392 2 $key = "`$key`";
393 }
394  
395 236 ▶ if (is_scalar($val)) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_scalar($val)) { } 11 225 236
elsif (is_homogenous_simple_array($val)) { } 115 110 225
elsif (is_neat_array($val)) { } 8 102 110
396 11 ▶ $text .= "\n" if ($$wasref == $BLOCK) and $options->{separate_blocks};
Branch: 1 branch
BranchTrueFalseTotal
if $$wasref == $BLOCK and $options->{"separate_blocks"} 0 11 11
Condition33%1/3
$$wasref == $BLOCK and $options->{"separate_blocks"}
!ll&&!rl&&r
1100
MC/DC0%0/2
$$wasref == $BLOCK and $options->{"separate_blocks"}
$$wasref == $BLOCK $options->{"separate_blocks"}
Truth table33%1/3
A and B
A= $$wasref == $BLOCKB= $options->{"separate_blocks"}
ABresult
0X0
100
111
397  
398 $text .= $space_indent .
399 pad($key, $key_length - $indent) .
400 (' ' x $options->{key_spacing}) .
401 11 render_scalar($val, $options, $key_length + $options->{key_spacing}) .
402 "\n";
403  
404 11 $$wasref = $PARAM;
405  
406 } elsif (is_homogenous_simple_array($val)) {
407 # escape individual array items
408 115 my @a = map { render_scalar($_, $options, undef, 1) } @$val;
409  
410 115 ▶ $text .= "\n" if ($$wasref == $BLOCK) and $options->{separate_blocks};
Branch: 1 branch
BranchTrueFalseTotal
if $$wasref == $BLOCK and $options->{"separate_blocks"} 2 113 115
Condition66%2/3
$$wasref == $BLOCK and $options->{"separate_blocks"}
!ll&&!rl&&r
11302
MC/DC50%1/2
$$wasref == $BLOCK and $options->{"separate_blocks"}
$$wasref == $BLOCK $options->{"separate_blocks"}
Truth table66%2/3
A and B
A= $$wasref == $BLOCKB= $options->{"separate_blocks"}
ABresult
0X0
100
111
411  
412 $text .= $space_indent .
413 pad($key, $key_length - $indent) .
414 (' ' x $options->{key_spacing}) .
415 115 render_wrapped_array(\@a, $options, $key_length + $options->{key_spacing}) .
416 "\n";
417  
418 115 $$wasref = $PARAM;
419  
420 } elsif (is_neat_array($val)) {
421 map {
422 8 $text .= render_key_val($options, $key_length, $indent, $wasref, $array_mode, $sequential_keys, $key, $_);
423 } @$val;
424  
425 } else {
426 102 ▶ $text .= "\n" if $$wasref and $options->{separate_blocks};
Branch: 1 branch
BranchTrueFalseTotal
if $$wasref and $options->{"separate_blocks"} 48 54 102
Condition66%2/3
$$wasref and $options->{"separate_blocks"}
!ll&&!rl&&r
54048
MC/DC50%1/2
$$wasref and $options->{"separate_blocks"}
$$wasref $options->{"separate_blocks"}
Truth table66%2/3
A and B
A= $$wasrefB= $options->{"separate_blocks"}
ABresult
0X0
100
111
427  
428 102 ▶ if (is_hash($val) && exists $val->{''}) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash($val) and exists $val->{''}) { } 5 97 102
Condition100%3/3
is_hash($val) and exists $val->{""}
!ll&&!rl&&r
1965
MC/DC100%2/2
is_hash($val) and exists $val->{""}
is_hash($val) exists $val->{""}
Truth table100%3/3
A and B
A= is_hash($val)B= exists $val->{""}
ABresult
0X0
100
111
429 5 my $default_value = $val->{''};
430 5 ▶ if (!is_scalar($default_value) && !is_homogenous_simple_array($default_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_scalar($default_value) or is_homogenous_simple_array($default_value)) 0 5 5
Condition33%1/3
is_scalar($default_value) or is_homogenous_simple_array($default_value)
l!l&&r!l&&!r
050
MC/DC0%0/2
is_scalar($default_value) or is_homogenous_simple_array($default_value)
is_scalar($default_value) is_homogenous_simple_array($default_value)
Truth table33%1/3
A or B
A= is_scalar($default_value)B= is_homogenous_simple_array($default_value)
ABresult
1X1
011
000
431 0 die "Only scalar or simple array can be rendered as a default node value";
432 }
433 5 $$wasref = $PARAM;
434 5 $text .= render_key_val($options, $key_length, $indent, $wasref, undef, $sequential_keys, $key, $default_value);
435 5 $text .= $space_indent;
436 } else {
437 97 $text .= $space_indent;
438  
439 97 ▶ if (!$array_mode && !($sequential_keys && is_number($key))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($array_mode or $sequential_keys and is_number($key)) 78 19 97
Condition83%5/6
$array_mode or $sequential_keys and is_number($key)
l!l&&r!l&&!r
15478
$sequential_keys and is_number($key)
!ll&&!rl&&r
0784
MC/DC66%2/3
$array_mode or $sequential_keys and is_number($key)
$array_mode $sequential_keys is_number($key)
Truth table75%3/4
A or B and C
A= $array_modeB= $sequential_keysC= is_number($key)
ABCresult
1XX1
0111
00X0
0100
440 78 ▶ $text .= $options->{brace_under} ? "$key\n$space_indent" : "$key ";
Branch: 1 branch
BranchTrueFalseTotal
$options->{'brace_under'} ? : 78 0 78
441 }
442 }
443  
444 $text .= "{\n" .
445 102 render_node_recursively($val, $options, $indent + $options->{indentation}) .
446 $space_indent .
447 "}\n";
448  
449 102 $$wasref = $BLOCK;
450 }
451  
452 236 return $text;
453 }
454  
455 sub render_node_recursively {
456 140 ▶ my ($node, $options, $indent) = @_;
Subroutine render_node_recursively: called CC=12 SCAR=25.4
Pod undocumented
457 140 my $text = '';
458 140 my $key_length = 0;
459 140 my $array_mode;
460 my $sequential_keys;
461  
462 140 ▶ if (is_array($node) || is_neat_array($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array($node) or is_neat_array($node)) 1 139 140
Condition66%2/3
is_array($node) or is_neat_array($node)
l!l&&r!l&&!r
10139
MC/DC50%1/2
is_array($node) or is_neat_array($node)
is_array($node) is_neat_array($node)
Truth table66%2/3
A or B
A= is_array($node)B= is_neat_array($node)
ABresult
1X1
011
000
463 1 ▶ if (is_homogenous_simple_array($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_homogenous_simple_array($node)) { } 0 1 1
464 0 die "Can't render simple arrays as a main block content";
465 } else {
466 1 $array_mode = 1;
467 1 $node = convert_array_to_hash($node);
468 }
469 }
470  
471 140 ▶ if (is_hash($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash($node)) { } 140 0 140
472 140 $array_mode = hash_has_only_sequential_keys($node);
473 140 $sequential_keys = hash_has_sequential_keys($node);
474 140 ▶ $key_length = $options->{align_all} ? $options->{global_key_length} : max_key_length($node, $options, $indent);
Branch: 1 branch
BranchTrueFalseTotal
$options->{'align_all'} ? : 140 0 140
475  
476 } else {
477 0 die "Unsupported data type: '".ref($node)."'";
478 }
479  
480 140 my $was = undef;
481  
482 140 my $sort = $options->{sort};
483 140 my @keys = keys %$node;
484 140 ▶ if (!$array_mode and scalar(@keys) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $array_mode and scalar @keys > 1) 39 101 140
Condition100%3/3
not $array_mode and scalar @keys > 1
!ll&&!rl&&r
89339
MC/DC100%2/2
not $array_mode and scalar @keys > 1
not $array_mode scalar @keys > 1
Truth table100%3/3
not(A) and B
A= not $array_modeB= scalar @keys > 1
ABresult
0X0
100
111
485 39 ▶ if (is_hash($sort)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_hash($sort)) { } 8 31 39
elsif ($sort) { } 0 31 31
486 8 @keys = sort { $sort->{$a} <=> $sort->{$b} } @keys;
487 } elsif ($sort) {
488 0 @keys = sort @keys;
489 }
490 }
491  
492 140 foreach my $key (@keys) {
493 # default node values are rendered separately
494 218 ▶ if ($key ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "") 213 5 218
495 213 $text .= render_key_val($options, $key_length, $indent, \$was, $array_mode, $sequential_keys, $key, $node->{$key});
496 }
497 }
498 140 return $text;
499 }
500  
501 38 ▶ if ($options->{align_all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"align_all"}) 38 0 38
502 # calculate indent recursively
503 38 $options->{global_key_length} = max_key_length($data, $options, 0, 1);
504 }
505  
506 38 return render_node_recursively($data, $options, 0);
507 }
508  
509 2 1;
« blib/lib/Config/Neat/Array.pm ↑ Summary lib/Config/Neat/Inheritable.pm »