Old/Dumper.pm

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