YAML/Dumper.pm

filter: Statementstmt 98.0% 305 / 311 Branchbran 89.0% 155 / 174 Conditioncond 80.9% 51 / 63 MC/DCmcdc 67.5% 25 / 37 Subroutinesub 100% 30 / 30 Podpod 0.0% 0 / 5 totaltotal 91.2% 566 / 620 CC 115 SCAR 26.6
CC
115
Cov
93%
_emit_mapping
23.1
_prewalk
22.1
_emit_node
20.2
CRAP 14.3
« YAML/Loader.pm ↑ Summary YAML/Dumper/Base.pm »
1 package YAML::Dumper;
2  
3 34 ▶use YAML::Mo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 extends 'YAML::Dumper::Base';
5  
6 34 ▶use YAML::Dumper::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use YAML::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use YAML::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Context constants
14 34 ▶use constant KEY => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 34 ▶use constant BLESSED => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 34 ▶use constant FROMARRAY => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 34 ▶use constant VALUE => "\x07YAML\x07VALUE\x07";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Common YAML character sets
20 34 my $ESCAPE_CHAR = '[\\x00-\\x08\\x0b-\\x0d\\x0e-\\x1f]';
21 34 my $LIT_CHAR = '|';
22  
23 #==============================================================================
24 # OO version of Dump. YAML->new->dump($foo);
25 160 ▶sub dump {
Subroutine dump: called CC=2 SCAR=6.9
Pod undocumented
26 160 my $self = shift;
27 160 $self->stream('');
28 160 $self->document(0);
29 160 for my $document (@_) {
30 166 $self->{document}++;
31 166 $self->transferred({});
32 166 $self->id_refcnt({});
33 166 $self->id_anchor({});
34 166 $self->anchor(1);
35 166 $self->level(0);
36 166 $self->offset->[0] = 0 - $self->indent_width;
37 166 $self->_prewalk($document);
38 166 $self->_emit_header($document);
39 163 $self->_emit_node($document);
40 }
41 157 return $self->stream;
42 }
43  
44 # Every YAML document in the stream must begin with a YAML header, unless
45 # there is only a single document and the user requests "no header".
46 166 ▶sub _emit_header {
Subroutine _emit_header: called CC=9 SCAR=22.1
47 166 my $self = shift;
48 166 my ($node) = @_;
49 166 ▶ if (not $self->use_header and
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->use_header and $self->document == 1) 4 162 166
Condition100%3/3
not $self->use_header and $self->document == 1
!ll&&!rl&&r
16114
MC/DC100%2/2
not $self->use_header and $self->document == 1
not $self->use_header $self->document == 1
Truth table100%3/3
not(A) and B
A= not $self->use_headerB= $self->document == 1
ABresult
0X0
100
111
50 $self->document == 1
51 ) {
52 4 ▶ $self->die('YAML_DUMP_ERR_NO_HEADER')
Branch: 1 branch
BranchTrueFalseTotal
unless ref($node) =~ /^(HASH|ARRAY)$/ 1 3 4
53 unless ref($node) =~ /^(HASH|ARRAY)$/;
54 3 ▶ $self->die('YAML_DUMP_ERR_NO_HEADER')
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "HASH" and keys %$node == 0 1 2 3
Condition66%2/3
ref $node eq "HASH" and keys %$node == 0
!ll&&!rl&&r
201
MC/DC50%1/2
ref $node eq "HASH" and keys %$node == 0
ref $node eq "HASH" keys %$node == 0
Truth table66%2/3
A and B
A= ref $node eq "HASH"B= keys %$node == 0
ABresult
0X0
100
111
55 if ref($node) eq 'HASH' and keys(%$node) == 0;
56 2 ▶ $self->die('YAML_DUMP_ERR_NO_HEADER')
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "ARRAY" and @$node == 0 1 1 2
Condition66%2/3
ref $node eq "ARRAY" and @$node == 0
!ll&&!rl&&r
011
MC/DC50%1/2
ref $node eq "ARRAY" and @$node == 0
ref $node eq "ARRAY" @$node == 0
Truth table66%2/3
A and B
A= ref $node eq "ARRAY"B= @$node == 0
ABresult
0X0
100
111
57 if ref($node) eq 'ARRAY' and @$node == 0;
58 # XXX Also croak if aliased, blessed, or ynode
59 1 $self->headless(1);
60 1 return;
61 }
62 162 $self->{stream} .= '---';
63 # XXX Consider switching to 1.1 style
64 162 ▶ if ($self->use_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_version) 0 162 162
65 # $self->{stream} .= " #YAML:1.0";
66 }
67 }
68  
69 # Walk the tree to be dumped and keep track of its reference counts.
70 # This function is where the Dumper does all its work. All type
71 # transfers happen here.
72 1499 ▶sub _prewalk {
Subroutine _prewalk: called CC=22 SCAR=30.9
73 1499 my $self = shift;
74 1499 my $stringify = $self->stringify;
75 1499 my ($class, $type, $node_id) = $self->node_info(\$_[0], $stringify);
76  
77 # Handle typeglobs
78 1499 ▶ if ($type eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "GLOB") 4 1495 1499
79 4 $self->transferred->{$node_id} =
80 YAML::Type::glob->yaml_dump($_[0]);
81 4 $self->_prewalk($self->transferred->{$node_id});
82 4 return;
83 }
84  
85 # Handle regexps
86 1495 ▶ if (ref($_[0]) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "Regexp") 11 1484 1495
87 11 return;
88 }
89  
90 # Handle Purity for scalars.
91 # XXX can't find a use case yet. Might be YAGNI.
92 1484 ▶ if (not ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 950 534 1484
93 950 ▶ $self->{id_refcnt}{$node_id}++ if $self->purity;
Branch: 1 branch
BranchTrueFalseTotal
if $self->purity 0 950 950
94 950 return;
95 }
96  
97 # Make a copy of original
98 534 my $value = $_[0];
99 534 ($class, $type, $node_id) = $self->node_info($value, $stringify);
100  
101 # Must be a stringified object.
102 534 ▶ return if (ref($value) and not $type);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and not $type 2 532 534
Condition66%2/3
ref $value and not $type
!ll&&!rl&&r
05322
MC/DC50%1/2
ref $value and not $type
ref $value not $type
Truth table66%2/3
A and not(B)
A= ref $valueB= not $type
ABresult
0X0
100
111
103  
104 # Look for things already transferred.
105 532 ▶ if ($self->transferred->{$node_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->transferred->{$node_id}) 11 521 532
106 (undef, undef, $node_id) = (ref $self->transferred->{$node_id})
107 ? $self->node_info($self->transferred->{$node_id}, $stringify)
108 11 ▶ : $self->node_info(\ $self->transferred->{$node_id}, $stringify);
Branch: 1 branch
BranchTrueFalseTotal
ref $self->transferred->{$node_id} ? : 9 2 11
109 11 $self->{id_refcnt}{$node_id}++;
110 11 return;
111 }
112  
113 # Handle code refs
114 521 ▶ if ($type eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "CODE") 9 512 521
115 9 $self->transferred->{$node_id} = 'placeholder';
116 YAML::Type::code->yaml_dump(
117 $self->dump_code,
118 $_[0],
119 9 $self->transferred->{$node_id}
120 );
121 ($class, $type, $node_id) =
122 9 $self->node_info(\ $self->transferred->{$node_id}, $stringify);
123 9 $self->{id_refcnt}{$node_id}++;
124 9 return;
125 }
126  
127 # Handle blessed things
128 512 ▶ if (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 31 481 512
129 31 ▶ if ($value->can('yaml_dump')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value->can('yaml_dump')) { } 5 26 31
elsif ($type eq 'SCALAR') { } 7 19 26
130 5 $value = $value->yaml_dump;
131 }
132 elsif ($type eq 'SCALAR') {
133 7 $self->transferred->{$node_id} = 'placeholder';
134 YAML::Type::blessed->yaml_dump
135 7 ($_[0], $self->transferred->{$node_id});
136 ($class, $type, $node_id) =
137 7 $self->node_info(\ $self->transferred->{$node_id}, $stringify);
138 7 $self->{id_refcnt}{$node_id}++;
139 7 return;
140 }
141 else {
142 19 $value = YAML::Type::blessed->yaml_dump($value);
143 }
144 24 $self->transferred->{$node_id} = $value;
145 24 (undef, $type, $node_id) = $self->node_info($value, $stringify);
146 }
147  
148 # Handle YAML Blessed things
149 505 require YAML;
150 505 ▶ if (defined YAML->global_object()->{blessed_map}{$node_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined "YAML"->global_object->{"blessed_map"}{$node_id}) 4 501 505
151 4 $value = YAML->global_object()->{blessed_map}{$node_id};
152 4 $self->transferred->{$node_id} = $value;
153 4 ($class, $type, $node_id) = $self->node_info($value, $stringify);
154 4 $self->_prewalk($value);
155 4 return;
156 }
157  
158 # Handle hard refs
159 501 ▶ if ($type eq 'REF' or $type eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'REF' or $type eq 'SCALAR') { } 31 470 501
elsif ($type eq 'GLOB') { } 2 468 470
Condition100%3/3
$type eq "REF" or $type eq "SCALAR"
l!l&&r!l&&!r
238470
MC/DC100%2/2
$type eq "REF" or $type eq "SCALAR"
$type eq "REF" $type eq "SCALAR"
Truth table100%3/3
A or B
A= $type eq "REF"B= $type eq "SCALAR"
ABresult
1X1
011
000
160 31 $value = YAML::Type::ref->yaml_dump($value);
161 31 $self->transferred->{$node_id} = $value;
162 31 (undef, $type, $node_id) = $self->node_info($value, $stringify);
163 }
164  
165 # Handle ref-to-glob's
166 elsif ($type eq 'GLOB') {
167 2 my $ref_ynode = $self->transferred->{$node_id} =
168 YAML::Type::ref->yaml_dump($value);
169  
170 2 my $glob_ynode = $ref_ynode->{&VALUE} =
171 YAML::Type::glob->yaml_dump($$value);
172  
173 2 (undef, undef, $node_id) = $self->node_info($glob_ynode, $stringify);
174 2 $self->transferred->{$node_id} = $glob_ynode;
175 2 $self->_prewalk($glob_ynode);
176 2 return;
177 }
178  
179 # Increment ref count for node
180 499 ▶ return if ++($self->{id_refcnt}{$node_id}) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if ++$self->{"id_refcnt"}{$node_id} > 1 3 496 499
181  
182 # Keep on walking
183 496 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 436 60 496
elsif ($type eq 'ARRAY') { } 60 0 60
184 $self->_prewalk($value->{$_})
185 436 for keys %{$value};
186 436 return;
187 }
188 elsif ($type eq 'ARRAY') {
189 $self->_prewalk($_)
190 60 for @{$value};
191 60 return;
192 }
193  
194 # Unknown type. Need to know about it.
195 0 $self->warn(<<"...");
196 YAML::Dumper can't handle dumping this type of data.
197 Please report this to the author.
198  
199 id: $node_id
200 type: $type
201 class: $class
202 value: $value
203  
204 ...
205  
206 0 return;
207 }
208  
209 # Every data element and sub data element is a node.
210 # Everything emitted goes through this function.
211 1492 ▶sub _emit_node {
Subroutine _emit_node: called CC=20 SCAR=30.0
212 1492 my $self = shift;
213 1492 my ($type, $node_id);
214 1492 my $ref = ref($_[0]);
215 1492 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 536 956 1492
216 536 ▶ if ($ref eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Regexp") 11 525 536
217 11 $self->_emit(' !!perl/regexp');
218 11 $self->_emit_str("$_[0]");
219 11 return;
220 }
221 525 (undef, $type, $node_id) = $self->node_info($_[0], $self->stringify);
222 }
223 else {
224 956 ▶ $type = $ref || 'SCALAR';
Condition50%1/2
$ref || 'SCALAR'
l!l
0956
MC/DC0%0/1
$ref || 'SCALAR'
$ref
Truth table50%1/2
A
A= $ref
Aresult
11
00
225 956 (undef, undef, $node_id) = $self->node_info(\$_[0], $self->stringify);
226 }
227  
228 1481 my ($ynode, $tag) = ('') x 2;
229 1481 my ($value, $context) = (@_, 0);
230  
231 1481 ▶ if (defined $self->transferred->{$node_id}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->transferred->{$node_id}) { } 92 1389 1481
elsif ($ynode = ynode($value)) { } 5 1384 1389
232 92 $value = $self->transferred->{$node_id};
233 92 $ynode = ynode($value);
234 92 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 74 18 92
235 74 ▶ $tag = defined $ynode ? $ynode->tag->short : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ynode ? : 74 0 74
236 74 (undef, $type, $node_id) =
237 $self->node_info($value, $self->stringify);
238 }
239 else {
240 18 $ynode = ynode($self->transferred->{$node_id});
241 18 ▶ $tag = defined $ynode ? $ynode->tag->short : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ynode ? : 18 0 18
242 18 $type = 'SCALAR';
243 (undef, undef, $node_id) =
244 $self->node_info(
245 18 \ $self->transferred->{$node_id},
246 $self->stringify
247 );
248 }
249 }
250 elsif ($ynode = ynode($value)) {
251 5 $tag = $ynode->tag->short;
252 }
253  
254 1481 ▶ if ($self->use_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_aliases) 1476 5 1481
255 1476 ▶ $self->{id_refcnt}{$node_id} ||= 0;
Condition100%2/2
$self->{"id_refcnt"}{$node_id} ||= 0
l!l
521955
MC/DC100%1/1
$self->{"id_refcnt"}{$node_id} ||= 0
$self->{"id_refcnt"}{$node_id}
Truth table100%2/2
A
A= $self->{"id_refcnt"}{$node_id}
Aresult
11
00
256 1476 ▶ if ($self->{id_refcnt}{$node_id} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"id_refcnt"}{$node_id} > 1) 20 1456 1476
257 20 ▶ if (defined $self->{id_anchor}{$node_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"id_anchor"}{$node_id}) 11 9 20
258 11 $self->{stream} .= ' *' . $self->{id_anchor}{$node_id} . "\n";
259 11 return;
260 }
261 9 my $anchor = $self->anchor_prefix . $self->{anchor}++;
262 9 $self->{stream} .= ' &' . $anchor;
263 9 $self->{id_anchor}{$node_id} = $anchor;
264 }
265 }
266  
267 1470 ▶ return $self->_emit_str("$value") # Stringified object
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and not $type 2 1468 1470
Condition100%3/3
ref $value and not $type
!ll&&!rl&&r
9685002
MC/DC100%2/2
ref $value and not $type
ref $value not $type
Truth table100%3/3
A and not(B)
A= ref $valueB= not $type
ABresult
0X0
100
111
268 if ref($value) and not $type;
269 1468 ▶ return $self->_emit_scalar($value, $tag)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SCALAR" and $tag 16 1452 1468
Condition100%3/3
$type eq "SCALAR" and $tag
!ll&&!rl&&r
50095216
MC/DC100%2/2
$type eq "SCALAR" and $tag
$type eq "SCALAR" $tag
Truth table100%3/3
A and B
A= $type eq "SCALAR"B= $tag
ABresult
0X0
100
111
270 if $type eq 'SCALAR' and $tag;
271 1452 ▶ return $self->_emit_str($value)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SCALAR" 952 500 1452
272 if $type eq 'SCALAR';
273 500 ▶ return $self->_emit_mapping($value, $tag, $node_id, $context)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" 440 60 500
274 if $type eq 'HASH';
275 60 ▶ return $self->_emit_sequence($value, $tag)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ARRAY" 60 0 60
276 if $type eq 'ARRAY';
277 0 $self->warn('YAML_DUMP_WARN_BAD_NODE_TYPE', $type);
278 0 return $self->_emit_str("$value");
279 }
280  
281 # A YAML mapping is akin to a Perl hash.
282 440 ▶sub _emit_mapping {
Subroutine _emit_mapping: called CC=21 SCAR=31.4
283 440 my $self = shift;
284 440 my ($value, $tag, $node_id, $context) = @_;
285 440 ▶ $self->{stream} .= " !$tag" if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 58 382 440
286  
287 # Sometimes 'keys' fails. Like on a bad tie implementation.
288 440 my $empty_hash = not(eval {keys %$value});
289 440 ▶ $self->warn('YAML_EMIT_WARN_KEYS', $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 440 440
290 440 ▶ return ($self->{stream} .= " {}\n") if $empty_hash;
Branch: 1 branch
BranchTrueFalseTotal
if $empty_hash 8 432 440
291  
292 # If CompressSeries is on (default) and legal is this context, then
293 # use it and make the indent level be 2 for this node.
294 432 ▶ if ($context == FROMARRAY and
Branch: 1 branch
BranchTrueFalseTotal
if ($context == 5 and $self->compress_series and not defined $self->{'id_anchor'}{$node_id} || $tag || $empty_hash) { } 20 412 432
Condition91%11/12
$context == 5 and $self->compress_series and not defined $self->{"id_anchor"}{$node_id} || $tag || $empty_hash
!ll&&!rl&&r
406620
$context == 5 and $self->compress_series
!ll&&!rl&&r
403326
defined $self->{'id_anchor'}{$node_id} || $tag || $empty_hash
l!l&&r!l&&!r
6020
defined $self->{"id_anchor"}{$node_id} or $tag
l!l&&r!l&&!r
4220
MC/DC80%4/5
$context == 5 and $self->compress_series and not defined $self->{"id_anchor"}{$node_id} || $tag || $empty_hash
$context == 5 $self->compress_series defined $self->{"id_anchor"}{$node_id} $tag $empty_hash
Truth table83%5/6
A and B and not(C or D || E)
A= $context == 5B= $self->compress_seriesC= defined $self->{"id_anchor"}{$node_id}D= $tagE= $empty_hash
ABCDEresult
0XXXX0
10XXX0
111XX0
1101X0
110010
110001
295 $self->compress_series and
296 not (defined $self->{id_anchor}{$node_id} or $tag or $empty_hash)
297 ) {
298 20 $self->{stream} .= ' ';
299 20 $self->offset->[$self->level+1] = $self->offset->[$self->level] + 2;
300 }
301 else {
302 412 $context = 0;
303 412 ▶ $self->{stream} .= "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->headless and not $self->headless(0) 412 0 412
Condition33%1/3
$self->headless and not $self->headless(0)
!ll&&!rl&&r
41200
MC/DC0%0/2
$self->headless and not $self->headless(0)
$self->headless not $self->headless(0)
Truth table33%1/3
A and not(B)
A= $self->headlessB= not $self->headless(0)
ABresult
0X0
100
111
304 unless $self->headless && not($self->headless(0));
305 412 $self->offset->[$self->level+1] =
306 $self->offset->[$self->level] + $self->indent_width;
307 }
308  
309 432 $self->{level}++;
310 432 my @keys;
311 432 ▶ if ($self->sort_keys == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->sort_keys == 1) { } 431 1 432
elsif ($self->sort_keys == 2) { } 0 1 1
elsif (ref $self->sort_keys eq 'ARRAY') { } 1 0 1
312 431 ▶ if (ynode($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ynode($value)) { } 62 369 431
313 62 @keys = keys %$value;
314 }
315 else {
316 369 @keys = sort keys %$value;
317 }
318 }
319 elsif ($self->sort_keys == 2) {
320 0 @keys = sort keys %$value;
321 }
322 # XXX This is hackish but sometimes handy. Not sure whether to leave it in.
323 elsif (ref($self->sort_keys) eq 'ARRAY') {
324 1 my $i = 1;
325 1 my %order = map { ($_, $i++) } @{$self->sort_keys};
326 @keys = sort {
327 1 (defined $order{$a} and defined $order{$b})
328 3 ▶ ? ($order{$a} <=> $order{$b})
Branch: 1 branch
BranchTrueFalseTotal
defined $order{$a} && defined $order{$b} ? : 3 0 3
Condition33%1/3
defined $order{$a} and defined $order{$b}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $order{$a} and defined $order{$b}
defined $order{$a} defined $order{$b}
Truth table33%1/3
A and B
A= defined $order{$a}B= defined $order{$b}
ABresult
0X0
100
111
329 : ($a cmp $b);
330 } keys %$value;
331 }
332 else {
333 0 @keys = keys %$value;
334 }
335 # Force the YAML::VALUE ('=') key to sort last.
336 432 ▶ if (exists $value->{&VALUE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$value{&VALUE}) 36 396 432
337 36 ▶ for (my $i = 0; $i < @keys; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @keys) 36 0 36
338 36 ▶ if ($keys[$i] eq &VALUE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keys[$i] eq &VALUE) 36 0 36
339 36 splice(@keys, $i, 1);
340 36 push @keys, &VALUE;
341 36 last;
342 }
343 }
344 }
345  
346 432 for my $key (@keys) {
347 1196 $self->_emit_key($key, $context);
348 1196 $context = 0;
349 1196 $self->{stream} .= ':';
350 1196 $self->_emit_node($value->{$key});
351 }
352 432 $self->{level}--;
353 }
354  
355 # A YAML series is akin to a Perl array.
356 60 ▶sub _emit_sequence {
Subroutine _emit_sequence: called CC=12 SCAR=24.9
357 60 my $self = shift;
358 60 my ($value, $tag) = @_;
359 60 ▶ $self->{stream} .= " !$tag" if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 7 53 60
360  
361 60 ▶ return ($self->{stream} .= " []\n") if @$value == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$value == 0 4 56 60
362  
363 56 ▶ $self->{stream} .= "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->headless and not $self->headless(0) 55 1 56
Condition66%2/3
$self->headless and not $self->headless(0)
!ll&&!rl&&r
5501
MC/DC50%1/2
$self->headless and not $self->headless(0)
$self->headless not $self->headless(0)
Truth table66%2/3
A and not(B)
A= $self->headlessB= not $self->headless(0)
ABresult
0X0
100
111
364 unless $self->headless && not($self->headless(0));
365  
366 # XXX Really crufty feature. Better implemented by ynodes.
367 56 ▶ if ($self->inline_series and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->inline_series and @$value <= $self->inline_series and not scalar grep({ref $_ or /\n/;} @$value)) 2 54 56
Condition100%6/6
$self->inline_series and @$value <= $self->inline_series and not scalar grep({ref $_ or /\n/;} @$value)
!ll&&!rl&&r
5312
$self->inline_series and @$value <= $self->inline_series
!ll&&!rl&&r
5213
MC/DC100%3/3
$self->inline_series and @$value <= $self->inline_series and not scalar grep({ref $_ or /\n/;} @$value)
$self->inline_series @$value <= $self->inline_series not scalar grep({ref $_ or /\n/;} @$value)
Truth table100%4/4
A and B and not(C)
A= $self->inline_seriesB= @$value <= $self->inline_seriesC= not scalar grep({ref $_ or /\n/;} @$value)
ABCresult
0XX0
10X0
1100
1111
368 @$value <= $self->inline_series and
369 8 ▶ not (scalar grep {ref or /\n/} @$value)
Condition66%2/3
ref $_ || /\n/
l!l&&r!l&&!r
305
MC/DC50%1/2
ref $_ || /\n/
ref $_ /\n/
Truth table66%2/3
A || B
A= ref $_B= /\n/
ABresult
1X1
011
000
370 ) {
371 2 $self->{stream} =~ s/\n\Z/ /;
372 2 $self->{stream} .= '[';
373 2 ▶ for (my $i = 0; $i < @$value; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$value) 5 0 5
374 5 $self->_emit_str($value->[$i], KEY);
375 5 ▶ last if $i == $#{$value};
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#{$value;} 2 3 5
376 3 $self->{stream} .= ', ';
377 }
378 2 $self->{stream} .= "]\n";
379 2 return;
380 }
381  
382 54 $self->offset->[$self->level + 1] =
383 $self->offset->[$self->level] + $self->indent_width;
384 54 $self->{level}++;
385 54 for my $val (@$value) {
386 133 $self->{stream} .= ' ' x $self->offset->[$self->level];
387 133 $self->{stream} .= '-';
388 133 $self->_emit_node($val, FROMARRAY);
389 }
390 54 $self->{level}--;
391 }
392  
393 # Emit a mapping key
394 1196 ▶sub _emit_key {
Subroutine _emit_key: called CC=2 SCAR=6.9
395 1196 my $self = shift;
396 1196 my ($value, $context) = @_;
397 1196 ▶ $self->{stream} .= ' ' x $self->offset->[$self->level]
Branch: 1 branch
BranchTrueFalseTotal
unless $context == 5 1176 20 1196
398 unless $context == FROMARRAY;
399 1196 $self->_emit_str($value, KEY);
400 }
401  
402 # Emit a blessed SCALAR
403 16 ▶sub _emit_scalar {
Subroutine _emit_scalar: called CC=1 SCAR=0.0
404 16 my $self = shift;
405 16 my ($value, $tag) = @_;
406 16 $self->{stream} .= " !$tag";
407 16 $self->_emit_str($value, BLESSED);
408 }
409  
410 4338 ▶sub _emit {
Subroutine _emit: called CC=1 SCAR=0.0
411 4338 my $self = shift;
412 4338 $self->{stream} .= join '', @_;
413 }
414  
415 # Emit a string value. YAML has many scalar styles. This routine attempts to
416 # guess the best style for the text.
417 2182 ▶sub _emit_str {
Subroutine _emit_str: called CC=16 SCAR=27.7
418 2182 my $self = shift;
419 2182 ▶ my $type = $_[1] || 0;
Condition100%2/2
$_[1] || 0
l!l
1217965
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
420  
421 # Use heuristics to find the best scalar emission style.
422 2182 $self->offset->[$self->level + 1] =
423 $self->offset->[$self->level] + $self->indent_width;
424 2182 $self->{level}++;
425  
426 2182 ▶ my $sf = $type == KEY ? '' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$type == 3 ? : 1201 981 2182
427 2182 ▶ my $sb = $type == KEY ? '? ' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$type == 3 ? : 1201 981 2182
428 2182 ▶ my $ef = $type == KEY ? '' : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$type == 3 ? : 1201 981 2182
429 2182 my $eb = "\n";
430  
431 2182 while (1) {
432 2182 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 4 2178 2182
433 $self->_emit_plain($_[0]),
434 $self->_emit($ef), last
435 if not defined $_[0];
436 2178 ▶ $self->_emit($sf, '=', $ef), last
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "\aYAML\aVALUE\a" 37 2141 2178
437 if $_[0] eq VALUE;
438 2141 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /$ESCAPE_CHAR/ 1 2140 2141
439 $self->_emit_double($_[0]),
440 $self->_emit($ef), last
441 if $_[0] =~ /$ESCAPE_CHAR/;
442 2140 ▶ if ($_[0] =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\n/) 28 2112 2140
443 28 ▶ $self->_emit($sb),
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_block 1 27 28
444 $self->_emit_block($LIT_CHAR, $_[0]),
445 $self->_emit($eb), last
446 if $self->use_block;
447 27 ▶ Carp::cluck "[YAML] \$UseFold is no longer supported"
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_fold 0 27 27
448 if $self->use_fold;
449 27 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] <= 30 7 20 27
450 $self->_emit_double($_[0]),
451 $self->_emit($ef), last
452 if length $_[0] <= 30;
453 20 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\n\s*\S/ 2 18 20
454 $self->_emit_double($_[0]),
455 $self->_emit($ef), last
456 if $_[0] !~ /\n\s*\S/;
457 18 $self->_emit($sb),
458 $self->_emit_block($LIT_CHAR, $_[0]),
459 $self->_emit($eb), last;
460 }
461 2112 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_literal_number($_[0]) 64 2048 2112
462 $self->_emit_number($_[0]),
463 $self->_emit($ef), last
464 if $self->is_literal_number($_[0]);
465 2048 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_valid_plain($_[0]) 1999 49 2048
466 $self->_emit_plain($_[0]),
467 $self->_emit($ef), last
468 if $self->is_valid_plain($_[0]);
469 49 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /'/ 1 48 49
470 $self->_emit_double($_[0]),
471 $self->_emit($ef), last
472 if $_[0] =~ /'/;
473 48 $self->_emit($sf),
474 $self->_emit_single($_[0]),
475 $self->_emit($ef);
476 48 last;
477 }
478  
479 2182 $self->{level}--;
480  
481 2182 return;
482 }
483  
484 2115 ▶sub is_literal_number {
Subroutine is_literal_number: called CC=2 SCAR=7.1
Pod undocumented
485 2115 my $self = shift;
486 # Stolen from JSON::Tiny
487 2115 ▶ return B::svref_2object(\$_[0])->FLAGS & (B::SVp_IOK | B::SVp_NOK)
Condition66%2/3
B::svref_2object(\$_[0])->FLAGS & 12288 && 0 + $_[0] eq $_[0]
!ll&&!rl&&r
2049066
MC/DC50%1/2
B::svref_2object(\$_[0])->FLAGS & 12288 && 0 + $_[0] eq $_[0]
B::svref_2object(\$_[0])->FLAGS & 12288 0 + $_[0] eq $_[0]
Truth table66%2/3
A && B
A= B::svref_2object(\$_[0])->FLAGS & 12288B= 0 + $_[0] eq $_[0]
ABresult
0X0
100
111
488 && 0 + $_[0] eq $_[0];
489 }
490  
491 64 ▶sub _emit_number {
Subroutine _emit_number: called CC=1 SCAR=0.0
492 64 my $self = shift;
493 64 return $self->_emit_plain($_[0]);
494 }
495  
496 # Check whether or not a scalar should be emitted as an plain scalar.
497 2048 ▶sub is_valid_plain {
Subroutine is_valid_plain: called CC=12 SCAR=24.9
Pod undocumented
498 2048 my $self = shift;
499 2048 ▶ return 0 unless length $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0] 6 2042 2048
500 2042 ▶ return 0 if $self->quote_numeric_strings and Scalar::Util::looks_like_number($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $self->quote_numeric_strings and &Scalar::Util::looks_like_number($_[0]) 6 2036 2042
Condition100%3/3
$self->quote_numeric_strings and &Scalar::Util::looks_like_number($_[0])
!ll&&!rl&&r
203246
MC/DC100%2/2
$self->quote_numeric_strings and &Scalar::Util::looks_like_number($_[0])
$self->quote_numeric_strings &Scalar::Util::looks_like_number($_[0])
Truth table100%3/3
A and B
A= $self->quote_numeric_stringsB= &Scalar::Util::looks_like_number($_[0])
ABresult
0X0
100
111
501 # refer to YAML::Loader::parse_inline_simple()
502 2036 ▶ return 0 if $_[0] =~ /^[\s\{\[\~\`\'\"\!\@\#\>\|\%\&\?\*\^]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^[\s\{\[\~\`\'\"\!\@\#\>\|\%\&\?\*\^]/ 17 2019 2036
503 2019 ▶ return 0 if $_[0] =~ /[\{\[\]\},]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[\{\[\]\},]/ 9 2010 2019
504 2010 ▶ return 0 if $_[0] =~ /[:\-\?]\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[:\-\?]\s/ 4 2006 2010
505 2006 ▶ return 0 if $_[0] =~ /\s#/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\s#/ 1 2005 2006
506 2005 ▶ return 0 if $_[0] =~ /\:(\s|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\:(\s|$)/ 0 2005 2005
507 2005 ▶ return 0 if $_[0] =~ /[\s\|\>]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[\s\|\>]$/ 4 2001 2005
508 2001 ▶ return 0 if $_[0] eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "-" 1 2000 2001
509 2000 ▶ return 0 if $_[0] eq '=';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "=" 1 1999 2000
510 1999 return 1;
511 }
512  
513 19 ▶sub _emit_block {
Subroutine _emit_block: called CC=5 SCAR=16.2
514 19 my $self = shift;
515 19 my ($indicator, $value) = @_;
516 19 $self->{stream} .= $indicator;
517 19 $value =~ /(\n*)\Z/;
518 19 ▶ my $chomp = length $1 ? (length $1 > 1) ? '+' : '' : '-';
Branch: 2 branches
BranchTrueFalseTotal
length $1 ? : 11 8 19
length $1 > 1 ? : 1 10 11
519 19 ▶ $value = '~' if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 19 19
520 19 $self->{stream} .= $chomp;
521 19 ▶ $self->{stream} .= $self->indent_width if $value =~ /^\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\s/ 0 19 19
522 19 $self->{stream} .= $self->indent($value);
523 }
524  
525 # Plain means that the scalar is unquoted.
526 2067 ▶sub _emit_plain {
Subroutine _emit_plain: called CC=2 SCAR=6.9
527 2067 my $self = shift;
528 2067 ▶ $self->{stream} .= defined $_[0] ? $_[0] : '~';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2063 4 2067
529 }
530  
531 # Double quoting is for single lined escaped strings.
532 11 ▶sub _emit_double {
Subroutine _emit_double: called CC=1 SCAR=0.0
533 11 my $self = shift;
534 11 (my $escaped = $self->escape($_[0])) =~ s/"/\\"/g;
535 11 $self->{stream} .= qq{"$escaped"};
536 }
537  
538 # Single quoting is for single lined unescaped strings.
539 48 ▶sub _emit_single {
Subroutine _emit_single: called CC=1 SCAR=0.0
540 48 my $self = shift;
541 48 my $item = shift;
542 48 $item =~ s{'}{''}g;
543 48 $self->{stream} .= "'$item'";
544 }
545  
546 #==============================================================================
547 # Utility subroutines.
548 #==============================================================================
549  
550 # Indent a scalar to the current indentation level.
551 19 ▶sub indent {
Subroutine indent: called CC=2 SCAR=7.0
Pod undocumented
552 19 my $self = shift;
553 19 my ($text) = @_;
554 19 ▶ return $text unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 19 19
555 19 $text =~ s/\n\Z//;
556 19 my $indent = ' ' x $self->offset->[$self->level];
557 19 $text =~ s/^/$indent/gm;
558 19 $text = "\n$text";
559 19 return $text;
560 }
561  
562 # Escapes for unprintable characters
563 34 my @escapes = qw(\0 \x01 \x02 \x03 \x04 \x05 \x06 \a
564 \x08 \t \n \v \f \r \x0e \x0f
565 \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17
566 \x18 \x19 \x1a \e \x1c \x1d \x1e \x1f
567 );
568  
569 # Escape the unprintable characters
570 11 ▶sub escape {
Subroutine escape: called CC=1 SCAR=0.0
Pod undocumented
571 11 my $self = shift;
572 11 my ($text) = @_;
573 11 $text =~ s/\\/\\\\/g;
574 11 $text =~ s/([\x00-\x1f])/$escapes[ord($1)]/ge;
575 11 return $text;
576 }
577  
578 34 1;
« YAML/Loader.pm ↑ Summary YAML/Dumper/Base.pm »