Old/Dumper.pm

filter: Statementstmt 98.0% 304 / 310 Branchbran 88.9% 153 / 172 Conditioncond 80.9% 51 / 63 MC/DCmcdc 67.5% 25 / 37 Subroutinesub 100% 30 / 30 Podpod 0.0% 0 / 5 totaltotal 91.2% 563 / 617 CC 114 SCAR 26.6
CC
114
Cov
93%
_emit_mapping
23.1
_prewalk
22.1
_emit_node
20.2
CRAP 14.3
« Old/Loader.pm ↑ Summary Old/Dumper/Base.pm »
1 package YAML::Old::Dumper;
2  
3 32 ▶use YAML::Old::Mo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 extends 'YAML::Old::Dumper::Base';
5  
6 32 ▶use YAML::Old::Dumper::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use YAML::Old::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use YAML::Old::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Context constants
14 32 ▶use constant KEY => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use constant BLESSED => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use constant FROMARRAY => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 32 ▶use constant VALUE => "\x07YAML\x07VALUE\x07";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Common YAML character sets
20 32 my $ESCAPE_CHAR = '[\\x00-\\x08\\x0b-\\x0d\\x0e-\\x1f]';
21 32 my $LIT_CHAR = '|';
22  
23 #==============================================================================
24 # OO version of Dump. YAML->new->dump($foo);
25 151 ▶sub dump {
Subroutine dump: called CC=2 SCAR=6.9
Pod undocumented
26 151 my $self = shift;
27 151 $self->stream('');
28 151 $self->document(0);
29 151 for my $document (@_) {
30 157 $self->{document}++;
31 157 $self->transferred({});
32 157 $self->id_refcnt({});
33 157 $self->id_anchor({});
34 157 $self->anchor(1);
35 157 $self->level(0);
36 157 $self->offset->[0] = 0 - $self->indent_width;
37 157 $self->_prewalk($document);
38 157 $self->_emit_header($document);
39 154 $self->_emit_node($document);
40 }
41 148 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 157 ▶sub _emit_header {
Subroutine _emit_header: called CC=9 SCAR=22.1
47 157 my $self = shift;
48 157 my ($node) = @_;
49 157 ▶ if (not $self->use_header and
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->use_header and $self->document == 1) 4 153 157
Condition100%3/3
not $self->use_header and $self->document == 1
!ll&&!rl&&r
15214
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 153 $self->{stream} .= '---';
63 # XXX Consider switching to 1.1 style
64 153 ▶ if ($self->use_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_version) 0 153 153
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 1483 ▶sub _prewalk {
Subroutine _prewalk: called CC=22 SCAR=30.9
73 1483 my $self = shift;
74 1483 my $stringify = $self->stringify;
75 1483 my ($class, $type, $node_id) = $self->node_info(\$_[0], $stringify);
76  
77 # Handle typeglobs
78 1483 ▶ if ($type eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "GLOB") 4 1479 1483
79 4 $self->transferred->{$node_id} =
80 YAML::Old::Type::glob->yaml_dump($_[0]);
81 4 $self->_prewalk($self->transferred->{$node_id});
82 4 return;
83 }
84  
85 # Handle regexps
86 1479 ▶ if (ref($_[0]) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "Regexp") 5 1474 1479
87 5 return;
88 }
89  
90 # Handle Purity for scalars.
91 # XXX can't find a use case yet. Might be YAGNI.
92 1474 ▶ if (not ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 949 525 1474
93 949 ▶ $self->{id_refcnt}{$node_id}++ if $self->purity;
Branch: 1 branch
BranchTrueFalseTotal
if $self->purity 0 949 949
94 949 return;
95 }
96  
97 # Make a copy of original
98 525 my $value = $_[0];
99 525 ($class, $type, $node_id) = $self->node_info($value, $stringify);
100  
101 # Must be a stringified object.
102 525 ▶ return if (ref($value) and not $type);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and not $type 2 523 525
Condition66%2/3
ref $value and not $type
!ll&&!rl&&r
05232
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 523 ▶ if ($self->transferred->{$node_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->transferred->{$node_id}) 11 512 523
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 512 ▶ if ($type eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "CODE") 7 505 512
115 7 $self->transferred->{$node_id} = 'placeholder';
116 YAML::Old::Type::code->yaml_dump(
117 $self->dump_code,
118 $_[0],
119 7 $self->transferred->{$node_id}
120 );
121 ($class, $type, $node_id) =
122 7 $self->node_info(\ $self->transferred->{$node_id}, $stringify);
123 7 $self->{id_refcnt}{$node_id}++;
124 7 return;
125 }
126  
127 # Handle blessed things
128 505 ▶ if (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class) 34 471 505
129 34 ▶ if ($value->can('yaml_dump')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value->can('yaml_dump')) { } 5 29 34
elsif ($type eq 'SCALAR') { } 7 22 29
130 5 $value = $value->yaml_dump;
131 }
132 elsif ($type eq 'SCALAR') {
133 7 $self->transferred->{$node_id} = 'placeholder';
134 YAML::Old::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 22 $value = YAML::Old::Type::blessed->yaml_dump($value);
143 }
144 27 $self->transferred->{$node_id} = $value;
145 27 (undef, $type, $node_id) = $self->node_info($value, $stringify);
146 }
147  
148 # Handle YAML Blessed things
149 498 require YAML::Old;
150 498 ▶ if (defined YAML::Old->global_object()->{blessed_map}{$node_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined "YAML::Old"->global_object->{"blessed_map"}{$node_id}) 4 494 498
151 4 $value = YAML::Old->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 494 ▶ if ($type eq 'REF' or $type eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'REF' or $type eq 'SCALAR') { } 31 463 494
elsif ($type eq 'GLOB') { } 2 461 463
Condition100%3/3
$type eq "REF" or $type eq "SCALAR"
l!l&&r!l&&!r
238463
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::Old::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::Old::Type::ref->yaml_dump($value);
169  
170 2 my $glob_ynode = $ref_ynode->{&VALUE} =
171 YAML::Old::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 492 ▶ return if ++($self->{id_refcnt}{$node_id}) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if ++$self->{"id_refcnt"}{$node_id} > 1 3 489 492
181  
182 # Keep on walking
183 489 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 429 60 489
elsif ($type eq 'ARRAY') { } 60 0 60
184 $self->_prewalk($value->{$_})
185 429 for keys %{$value};
186 429 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::Old::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 1476 ▶sub _emit_node {
Subroutine _emit_node: called CC=20 SCAR=30.0
212 1476 my $self = shift;
213 1476 my ($type, $node_id);
214 1476 my $ref = ref($_[0]);
215 1476 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 521 955 1476
216 521 ▶ if ($ref eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Regexp") 5 516 521
217 5 $self->_emit(' !!perl/regexp');
218 5 $self->_emit_str("$_[0]");
219 5 return;
220 }
221 516 (undef, $type, $node_id) = $self->node_info($_[0], $self->stringify);
222 }
223 else {
224 955 ▶ $type = $ref || 'SCALAR';
Condition50%1/2
$ref || 'SCALAR'
l!l
0955
MC/DC0%0/1
$ref || 'SCALAR'
$ref
Truth table50%1/2
A
A= $ref
Aresult
11
00
225 955 (undef, undef, $node_id) = $self->node_info(\$_[0], $self->stringify);
226 }
227  
228 1471 my ($ynode, $tag) = ('') x 2;
229 1471 my ($value, $context) = (@_, 0);
230  
231 1471 ▶ if (defined $self->transferred->{$node_id}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->transferred->{$node_id}) { } 93 1378 1471
elsif ($ynode = ynode($value)) { } 5 1373 1378
232 93 $value = $self->transferred->{$node_id};
233 93 $ynode = ynode($value);
234 93 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 77 16 93
235 77 ▶ $tag = defined $ynode ? $ynode->tag->short : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ynode ? : 77 0 77
236 77 (undef, $type, $node_id) =
237 $self->node_info($value, $self->stringify);
238 }
239 else {
240 16 $ynode = ynode($self->transferred->{$node_id});
241 16 ▶ $tag = defined $ynode ? $ynode->tag->short : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ynode ? : 16 0 16
242 16 $type = 'SCALAR';
243 (undef, undef, $node_id) =
244 $self->node_info(
245 16 \ $self->transferred->{$node_id},
246 $self->stringify
247 );
248 }
249 }
250 elsif ($ynode = ynode($value)) {
251 5 $tag = $ynode->tag->short;
252 }
253  
254 1471 ▶ if ($self->use_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_aliases) 1466 5 1471
255 1466 ▶ $self->{id_refcnt}{$node_id} ||= 0;
Condition100%2/2
$self->{"id_refcnt"}{$node_id} ||= 0
l!l
512954
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 1466 ▶ if ($self->{id_refcnt}{$node_id} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"id_refcnt"}{$node_id} > 1) 20 1446 1466
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 1460 ▶ return $self->_emit_str("$value") # Stringified object
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and not $type 2 1458 1460
Condition100%3/3
ref $value and not $type
!ll&&!rl&&r
9654932
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 1458 ▶ return $self->_emit_scalar($value, $tag)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SCALAR" and $tag 14 1444 1458
Condition100%3/3
$type eq "SCALAR" and $tag
!ll&&!rl&&r
49395114
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 1444 ▶ return $self->_emit_str($value)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SCALAR" 951 493 1444
272 if $type eq 'SCALAR';
273 493 ▶ return $self->_emit_mapping($value, $tag, $node_id, $context)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" 433 60 493
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 433 ▶sub _emit_mapping {
Subroutine _emit_mapping: called CC=21 SCAR=31.4
283 433 my $self = shift;
284 433 my ($value, $tag, $node_id, $context) = @_;
285 433 ▶ $self->{stream} .= " !$tag" if $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $tag 61 372 433
286  
287 # Sometimes 'keys' fails. Like on a bad tie implementation.
288 433 my $empty_hash = not(eval {keys %$value});
289 433 ▶ $self->warn('YAML_EMIT_WARN_KEYS', $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 433 433
290 433 ▶ return ($self->{stream} .= " {}\n") if $empty_hash;
Branch: 1 branch
BranchTrueFalseTotal
if $empty_hash 8 425 433
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 425 ▶ 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 405 425
Condition91%11/12
$context == 5 and $self->compress_series and not defined $self->{"id_anchor"}{$node_id} || $tag || $empty_hash
!ll&&!rl&&r
399620
$context == 5 and $self->compress_series
!ll&&!rl&&r
396326
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 405 $context = 0;
303 405 ▶ $self->{stream} .= "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->headless and not $self->headless(0) 405 0 405
Condition33%1/3
$self->headless and not $self->headless(0)
!ll&&!rl&&r
40500
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 405 $self->offset->[$self->level+1] =
306 $self->offset->[$self->level] + $self->indent_width;
307 }
308  
309 425 $self->{level}++;
310 425 my @keys;
311 425 ▶ if ($self->sort_keys == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->sort_keys == 1) { } 424 1 425
elsif ($self->sort_keys == 2) { } 0 1 1
elsif (ref $self->sort_keys eq 'ARRAY') { } 1 0 1
312 424 ▶ if (ynode($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ynode($value)) { } 65 359 424
313 65 @keys = keys %$value;
314 }
315 else {
316 359 @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 425 ▶ if (exists $value->{&VALUE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$value{&VALUE}) 36 389 425
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 425 for my $key (@keys) {
347 1189 $self->_emit_key($key, $context);
348 1189 $context = 0;
349 1189 $self->{stream} .= ':';
350 1189 $self->_emit_node($value->{$key});
351 }
352 425 $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 1189 ▶sub _emit_key {
Subroutine _emit_key: called CC=2 SCAR=6.9
395 1189 my $self = shift;
396 1189 my ($value, $context) = @_;
397 1189 ▶ $self->{stream} .= ' ' x $self->offset->[$self->level]
Branch: 1 branch
BranchTrueFalseTotal
unless $context == 5 1169 20 1189
398 unless $context == FROMARRAY;
399 1189 $self->_emit_str($value, KEY);
400 }
401  
402 # Emit a blessed SCALAR
403 14 ▶sub _emit_scalar {
Subroutine _emit_scalar: called CC=1 SCAR=0.0
404 14 my $self = shift;
405 14 my ($value, $tag) = @_;
406 14 $self->{stream} .= " !$tag";
407 14 $self->_emit_str($value, BLESSED);
408 }
409  
410 4300 ▶sub _emit {
Subroutine _emit: called CC=1 SCAR=0.0
411 4300 my $self = shift;
412 4300 $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 2166 ▶sub _emit_str {
Subroutine _emit_str: called CC=16 SCAR=27.7
418 2166 my $self = shift;
419 2166 ▶ my $type = $_[1] || 0;
Condition100%2/2
$_[1] || 0
l!l
1208958
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 2166 $self->offset->[$self->level + 1] =
423 $self->offset->[$self->level] + $self->indent_width;
424 2166 $self->{level}++;
425  
426 2166 ▶ my $sf = $type == KEY ? '' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$type == 3 ? : 1194 972 2166
427 2166 ▶ my $sb = $type == KEY ? '? ' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$type == 3 ? : 1194 972 2166
428 2166 ▶ my $ef = $type == KEY ? '' : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$type == 3 ? : 1194 972 2166
429 2166 my $eb = "\n";
430  
431 2166 while (1) {
432 2166 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 4 2162 2166
433 $self->_emit_plain($_[0]),
434 $self->_emit($ef), last
435 if not defined $_[0];
436 2162 ▶ $self->_emit($sf, '=', $ef), last
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "\aYAML\aVALUE\a" 37 2125 2162
437 if $_[0] eq VALUE;
438 2125 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /$ESCAPE_CHAR/ 1 2124 2125
439 $self->_emit_double($_[0]),
440 $self->_emit($ef), last
441 if $_[0] =~ /$ESCAPE_CHAR/;
442 2124 ▶ if ($_[0] =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\n/) 26 2098 2124
443 26 ▶ $self->_emit($sb),
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_block 1 25 26
444 $self->_emit_block($LIT_CHAR, $_[0]),
445 $self->_emit($eb), last
446 if $self->use_block;
447 25 ▶ Carp::cluck "[YAML::Old] \$UseFold is no longer supported"
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_fold 0 25 25
448 if $self->use_fold;
449 25 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] <= 30 6 19 25
450 $self->_emit_double($_[0]),
451 $self->_emit($ef), last
452 if length $_[0] <= 30;
453 19 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /\n\s*\S/ 2 17 19
454 $self->_emit_double($_[0]),
455 $self->_emit($ef), last
456 if $_[0] !~ /\n\s*\S/;
457 17 $self->_emit($sb),
458 $self->_emit_block($LIT_CHAR, $_[0]),
459 $self->_emit($eb), last;
460 }
461 2098 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_literal_number($_[0]) 61 2037 2098
462 $self->_emit_number($_[0]),
463 $self->_emit($ef), last
464 if $self->is_literal_number($_[0]);
465 2037 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_valid_plain($_[0]) 1989 48 2037
466 $self->_emit_plain($_[0]),
467 $self->_emit($ef), last
468 if $self->is_valid_plain($_[0]);
469 48 ▶ $self->_emit($sf),
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /'/ 1 47 48
470 $self->_emit_double($_[0]),
471 $self->_emit($ef), last
472 if $_[0] =~ /'/;
473 47 $self->_emit($sf),
474 $self->_emit_single($_[0]),
475 $self->_emit($ef);
476 47 last;
477 }
478  
479 2166 $self->{level}--;
480  
481 2166 return;
482 }
483  
484 2101 ▶sub is_literal_number {
Subroutine is_literal_number: called CC=2 SCAR=7.1
Pod undocumented
485 2101 my $self = shift;
486 # Stolen from JSON::Tiny
487 2101 ▶ 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
2038063
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 61 ▶sub _emit_number {
Subroutine _emit_number: called CC=1 SCAR=0.0
492 61 my $self = shift;
493 61 return $self->_emit_plain($_[0]);
494 }
495  
496 # Check whether or not a scalar should be emitted as an plain scalar.
497 2037 ▶sub is_valid_plain {
Subroutine is_valid_plain: called CC=11 SCAR=24.0
Pod undocumented
498 2037 my $self = shift;
499 2037 ▶ return 0 unless length $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0] 6 2031 2037
500 2031 ▶ 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 2025 2031
Condition100%3/3
$self->quote_numeric_strings and &Scalar::Util::looks_like_number($_[0])
!ll&&!rl&&r
202146
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::Old::Loader::parse_inline_simple()
502 2025 ▶ return 0 if $_[0] =~ /^[\s\{\[\~\`\'\"\!\@\#\>\|\%\&\?\*\^]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^[\s\{\[\~\`\'\"\!\@\#\>\|\%\&\?\*\^]/ 17 2008 2025
503 2008 ▶ return 0 if $_[0] =~ /[\{\[\]\},]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[\{\[\]\},]/ 9 1999 2008
504 1999 ▶ return 0 if $_[0] =~ /[:\-\?]\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[:\-\?]\s/ 4 1995 1999
505 1995 ▶ return 0 if $_[0] =~ /\s#/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\s#/ 1 1994 1995
506 1994 ▶ return 0 if $_[0] =~ /\:(\s|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\:(\s|$)/ 0 1994 1994
507 1994 ▶ return 0 if $_[0] =~ /[\s\|\>]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[\s\|\>]$/ 4 1990 1994
508 1990 ▶ return 0 if $_[0] eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "-" 1 1989 1990
509 1989 return 1;
510 }
511  
512 18 ▶sub _emit_block {
Subroutine _emit_block: called CC=5 SCAR=16.2
513 18 my $self = shift;
514 18 my ($indicator, $value) = @_;
515 18 $self->{stream} .= $indicator;
516 18 $value =~ /(\n*)\Z/;
517 18 ▶ my $chomp = length $1 ? (length $1 > 1) ? '+' : '' : '-';
Branch: 2 branches
BranchTrueFalseTotal
length $1 ? : 10 8 18
length $1 > 1 ? : 1 9 10
518 18 ▶ $value = '~' if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 18 18
519 18 $self->{stream} .= $chomp;
520 18 ▶ $self->{stream} .= $self->indent_width if $value =~ /^\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\s/ 0 18 18
521 18 $self->{stream} .= $self->indent($value);
522 }
523  
524 # Plain means that the scalar is unquoted.
525 2054 ▶sub _emit_plain {
Subroutine _emit_plain: called CC=2 SCAR=6.9
526 2054 my $self = shift;
527 2054 ▶ $self->{stream} .= defined $_[0] ? $_[0] : '~';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2050 4 2054
528 }
529  
530 # Double quoting is for single lined escaped strings.
531 10 ▶sub _emit_double {
Subroutine _emit_double: called CC=1 SCAR=0.0
532 10 my $self = shift;
533 10 (my $escaped = $self->escape($_[0])) =~ s/"/\\"/g;
534 10 $self->{stream} .= qq{"$escaped"};
535 }
536  
537 # Single quoting is for single lined unescaped strings.
538 47 ▶sub _emit_single {
Subroutine _emit_single: called CC=1 SCAR=0.0
539 47 my $self = shift;
540 47 my $item = shift;
541 47 $item =~ s{'}{''}g;
542 47 $self->{stream} .= "'$item'";
543 }
544  
545 #==============================================================================
546 # Utility subroutines.
547 #==============================================================================
548  
549 # Indent a scalar to the current indentation level.
550 18 ▶sub indent {
Subroutine indent: called CC=2 SCAR=7.0
Pod undocumented
551 18 my $self = shift;
552 18 my ($text) = @_;
553 18 ▶ return $text unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 18 18
554 18 $text =~ s/\n\Z//;
555 18 my $indent = ' ' x $self->offset->[$self->level];
556 18 $text =~ s/^/$indent/gm;
557 18 $text = "\n$text";
558 18 return $text;
559 }
560  
561 # Escapes for unprintable characters
562 32 my @escapes = qw(\0 \x01 \x02 \x03 \x04 \x05 \x06 \a
563 \x08 \t \n \v \f \r \x0e \x0f
564 \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17
565 \x18 \x19 \x1a \e \x1c \x1d \x1e \x1f
566 );
567  
568 # Escape the unprintable characters
569 10 ▶sub escape {
Subroutine escape: called CC=1 SCAR=0.0
Pod undocumented
570 10 my $self = shift;
571 10 my ($text) = @_;
572 10 $text =~ s/\\/\\\\/g;
573 10 $text =~ s/([\x00-\x1f])/$escapes[ord($1)]/ge;
574 10 return $text;
575 }
576  
577 32 1;
« Old/Loader.pm ↑ Summary Old/Dumper/Base.pm »