blib/lib/YAML/PP.pm

filter: Statementstmt 96.5% 221 / 229 Branchbran 83.3% 30 / 36 Conditioncond 88.8% 24 / 27 MC/DCmcdc 82.3% 14 / 17 Subroutinesub 98.3% 60 / 61 Podpod 94.4% 17 / 18 totaltotal 94.3% 366 / 388 CC 32 SCAR 11.9
CC
32
Cov
94%
new
9.2
STORE
8.0
_preserve
7.0
CRAP 3.3
« blib/lib/YAML/PP/Reader.pm ↑ Summary blib/lib/YAML/PP/Schema/Failsafe.pm »
1 # ABSTRACT: YAML 1.2 Processor
2 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package YAML::PP;
5  
6 48 our $VERSION = 'v0.41.1'; # TRIAL VERSION
7  
8 48 ▶use YAML::PP::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 48 ▶use YAML::PP::Schema::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 48 ▶use YAML::PP::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 48 ▶use YAML::PP::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 48 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 48 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 48 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 48 our @EXPORT_OK = qw/ Load LoadFile Dump DumpFile /;
17  
18 48 my %YAML_VERSIONS = ('1.1' => 1, '1.2' => 1);
19  
20  
21 765 ▶sub new {
Subroutine new: called CC=9 SCAR=22.2
Pod documented
22 765 my ($class, %args) = @_;
23  
24 765 my $bool = delete $args{boolean};
25 765 ▶ $bool = 'perl' unless defined $bool;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bool 162 603 765
26 765 ▶ my $schemas = delete $args{schema} || ['+'];
Condition100%2/2
delete $args{'schema'} || ['+']
l!l
632133
MC/DC100%1/1
delete $args{'schema'} || ['+']
delete $args{'schema'}
Truth table100%2/2
A
A= delete $args{'schema'}
Aresult
11
00
27 765 ▶ my $cyclic_refs = delete $args{cyclic_refs} || 'fatal';
Condition100%2/2
delete $args{'cyclic_refs'} || 'fatal'
l!l
16749
MC/DC100%1/1
delete $args{'cyclic_refs'} || 'fatal'
delete $args{'cyclic_refs'}
Truth table100%2/2
A
A= delete $args{'cyclic_refs'}
Aresult
11
00
28 765 my $indent = delete $args{indent};
29 765 my $width = delete $args{width};
30 765 my $writer = delete $args{writer};
31 765 my $header = delete $args{header};
32 765 my $footer = delete $args{footer};
33 765 my $require_footer = delete $args{require_footer};
34 765 my $duplicate_keys = delete $args{duplicate_keys};
35 765 my $max_depth = delete $args{max_depth};
36 765 my $yaml_version = $class->_arg_yaml_version(delete $args{yaml_version});
37 765 my $default_yaml_version = $yaml_version->[0];
38 765 my $version_directive = delete $args{version_directive};
39 765 my $preserve = delete $args{preserve};
40 765 my $parser = delete $args{parser};
41 my $emitter = delete $args{emitter} || {
42 765 ▶ indent => $indent,
Condition50%1/2
delete $args{'emitter'} || {'indent', $indent, 'width', $width, 'writer', $writer}
l!l
0765
MC/DC0%0/1
delete $args{'emitter'} || {'indent', $indent, 'width', $width, 'writer', $writer}
delete $args{'emitter'}
Truth table50%1/2
A
A= delete $args{'emitter'}
Aresult
11
00
43 width => $width,
44 writer => $writer,
45 };
46 765 ▶ if (keys %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args) 0 765 765
47 0 die "Unexpected arguments: " . join ', ', sort keys %args;
48 }
49  
50 765 my %schemas;
51 765 for my $v (@$yaml_version) {
52 767 my $schema;
53 767 ▶ if (blessed($schemas) and $schemas->isa('YAML::PP::Schema')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($schemas) and $schemas->isa('YAML::PP::Schema')) { } 0 767 767
Condition33%1/3
builtin::blessed($schemas) and $schemas->isa("YAML::PP::Schema")
!ll&&!rl&&r
76700
MC/DC0%0/2
builtin::blessed($schemas) and $schemas->isa("YAML::PP::Schema")
builtin::blessed($schemas) $schemas->isa("YAML::PP::Schema")
Truth table33%1/3
A and B
A= builtin::blessed($schemas)B= $schemas->isa("YAML::PP::Schema")
ABresult
0X0
100
111
54 0 $schema = $schemas;
55 }
56 else {
57 767 $schema = YAML::PP::Schema->new(
58 boolean => $bool,
59 yaml_version => $v,
60 );
61 767 $schema->load_subschemas(@$schemas);
62 }
63 766 $schemas{ $v } = $schema;
64 }
65 764 my $default_schema = $schemas{ $default_yaml_version };
66  
67 764 my $loader = YAML::PP::Loader->new(
68 schemas => \%schemas,
69 cyclic_refs => $cyclic_refs,
70 parser => $parser,
71 default_yaml_version => $default_yaml_version,
72 preserve => $preserve,
73 duplicate_keys => $duplicate_keys,
74 require_footer => $require_footer,
75 max_depth => $max_depth,
76 );
77 763 my $dumper = YAML::PP::Dumper->new(
78 schema => $default_schema,
79 emitter => $emitter,
80 header => $header,
81 footer => $footer,
82 version_directive => $version_directive,
83 preserve => $preserve,
84 );
85  
86 763 my $self = bless {
87 schema => \%schemas,
88 loader => $loader,
89 dumper => $dumper,
90 }, $class;
91 763 return $self;
92 }
93  
94 9 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
95 9 my ($self) = @_;
96 9 my $clone = {
97 schema => $self->schema,
98 loader => $self->loader->clone,
99 dumper => $self->dumper->clone,
100 };
101 9 return bless $clone, ref $self;
102 }
103  
104 765 ▶sub _arg_yaml_version {
Subroutine _arg_yaml_version: called CC=5 SCAR=16.2
105 765 my ($class, $version) = @_;
106 765 my @versions = ('1.2');
107 765 ▶ if (defined $version) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version) 5 760 765
108 5 @versions = ();
109 5 ▶ if (not ref $version) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $version) 1 4 5
110 1 $version = [$version];
111 }
112 5 for my $v (@$version) {
113 7 ▶ unless ($YAML_VERSIONS{ $v }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($YAML_VERSIONS{$v}) 0 7 7
114 0 croak "YAML Version '$v' not supported";
115 }
116 7 push @versions, $v;
117 }
118 }
119 765 return \@versions;
120 }
121  
122  
123 2221 ▶sub loader {
Subroutine loader: called CC=2 SCAR=8.1
Pod documented
124 2221 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 2221 2221
125 0 $_[0]->{loader} = $_[1]
126 }
127 2221 return $_[0]->{loader};
128 }
129  
130 1494 ▶sub dumper {
Subroutine dumper: called CC=2 SCAR=8.1
Pod documented
131 1494 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 1494 1494
132 0 $_[0]->{dumper} = $_[1]
133 }
134 1494 return $_[0]->{dumper};
135 }
136  
137 16 ▶sub schema {
Subroutine schema: called CC=2 SCAR=8.1
Pod documented
138 16 ▶ if (@_ > 1) { $_[0]->{schema}->{'1.2'} = $_[1] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 16 16
139 16 return $_[0]->{schema}->{'1.2'};
140 }
141  
142 3 ▶sub default_schema {
Subroutine default_schema: called CC=1 SCAR=0.0
Pod documented
143 3 my ($self, %args) = @_;
144 my $schema = YAML::PP::Schema->new(
145 boolean => $args{boolean},
146 3 );
147 3 $schema->load_subschemas(qw/ Core /);
148 3 return $schema;
149 }
150  
151 2181 ▶sub load_string {
Subroutine load_string: called CC=1 SCAR=0.0
Pod documented
152 2181 my ($self, $yaml) = @_;
153 2181 return $self->loader->load_string($yaml);
154 }
155  
156 28 ▶sub load_file {
Subroutine load_file: called CC=1 SCAR=0.0
Pod documented
157 28 my ($self, $file) = @_;
158 28 return $self->loader->load_file($file);
159 }
160  
161 1 ▶sub dump {
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
162 1 my ($self, @data) = @_;
163 1 return $self->dumper->dump(@data);
164 }
165  
166 1478 ▶sub dump_string {
Subroutine dump_string: called CC=1 SCAR=0.0
Pod documented
167 1478 my ($self, @data) = @_;
168 1478 return $self->dumper->dump_string(@data);
169 }
170  
171 6 ▶sub dump_file {
Subroutine dump_file: called CC=1 SCAR=0.0
Pod documented
172 6 my ($self, $file, @data) = @_;
173 6 return $self->dumper->dump_file($file, @data);
174 }
175  
176 # legagy interface
177 2 ▶sub Load {
Subroutine Load: called CC=1 SCAR=0.0
Pod documented
178 2 my ($yaml) = @_;
179 2 YAML::PP->new->load_string($yaml);
180 }
181  
182 12 ▶sub LoadFile {
Subroutine LoadFile: called CC=1 SCAR=0.0
Pod documented
183 12 my ($file) = @_;
184 12 YAML::PP->new->load_file($file);
185 }
186  
187 1 ▶sub Dump {
Subroutine Dump: called CC=1 SCAR=0.0
Pod documented
188 1 my (@data) = @_;
189 1 YAML::PP->new->dump_string(@data);
190 }
191  
192 4 ▶sub DumpFile {
Subroutine DumpFile: called CC=1 SCAR=0.0
Pod documented
193 4 my ($file, @data) = @_;
194 4 YAML::PP->new->dump_file($file, @data);
195 }
196  
197 4 ▶sub preserved_scalar {
Subroutine preserved_scalar: called CC=1 SCAR=0.0
Pod documented
198 4 my ($self, $value, %args) = @_;
199 4 my $scalar = YAML::PP::Preserve::Scalar->new(
200 value => $value,
201 %args,
202 );
203 4 return $scalar;
204 }
205  
206 4 ▶sub preserved_mapping {
Subroutine preserved_mapping: called CC=1 SCAR=0.0
Pod documented
207 4 my ($self, $hash, %args) = @_;
208 4 my $data = {};
209 4 tie %$data, 'YAML::PP::Preserve::Hash';
210 4 %$data = %$hash;
211 4 my $t = tied %$data;
212 4 $t->{style} = $args{style};
213 4 $t->{alias} = $args{alias};
214 4 return $data;
215 }
216  
217 4 ▶sub preserved_sequence {
Subroutine preserved_sequence: called CC=1 SCAR=0.0
Pod documented
218 4 my ($self, $array, %args) = @_;
219 4 my $data = [];
220 4 tie @$data, 'YAML::PP::Preserve::Array';
221 4 push @$data, @$array;
222 4 my $t = tied @$data;
223 4 $t->{style} = $args{style};
224 4 $t->{alias} = $args{alias};
225 4 return $data;
226 }
227  
228 package YAML::PP::Preserve::Hash;
229 # experimental
230 48 ▶use Tie::Hash; # provides Tie::StdHash, our parent below
Subroutine BEGIN: called CC=1 SCAR=0.0
231 48 our @ISA = qw/ Tie::StdHash /;
232 48 ▶use Scalar::Util qw/ reftype blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 34 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
235 34 my ($class, %args) = @_;
236 34 my $self = bless {
237 keys => [keys %args],
238 data => { %args },
239 }, $class;
240 }
241  
242 127 ▶sub STORE {
Subroutine STORE: called CC=8 SCAR=20.8
243 127 my ($self, $key, $val) = @_;
244 127 my $keys = $self->{keys};
245 127 ▶ unless (exists $self->{data}->{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"data"}{$key}) 120 7 127
246 120 push @$keys, $key;
247 }
248 127 ▶ if (ref $val and not blessed($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and not builtin::blessed($val)) 39 88 127
Condition100%3/3
ref $val and not builtin::blessed($val)
!ll&&!rl&&r
592939
MC/DC100%2/2
ref $val and not builtin::blessed($val)
ref $val not builtin::blessed($val)
Truth table100%3/3
A and not(B)
A= ref $valB= not builtin::blessed($val)
ABresult
0X0
100
111
249 39 ▶ if (reftype($val) eq 'HASH' and not tied %$val) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($val) eq 'HASH' and not tied %$val) { } 1 38 39
elsif (builtin::reftype($val) eq 'ARRAY' and not tied @$val) { } 2 54 56
Condition100%3/3
builtin::reftype($val) eq "HASH" and not tied %$val
!ll&&!rl&&r
20181
MC/DC100%2/2
builtin::reftype($val) eq "HASH" and not tied %$val
builtin::reftype($val) eq "HASH" not tied %$val
Truth table100%3/3
A and not(B)
A= builtin::reftype($val) eq "HASH"B= not tied %$val
ABresult
0X0
100
111
250 1 tie %$val, 'YAML::PP::Preserve::Hash', %$val;
251 }
252 ▶ elsif (reftype($val) eq 'ARRAY' and not tied @$val) {
Condition100%3/3
builtin::reftype($val) eq "ARRAY" and not tied @$val
!ll&&!rl&&r
18182
MC/DC100%2/2
builtin::reftype($val) eq "ARRAY" and not tied @$val
builtin::reftype($val) eq "ARRAY" not tied @$val
Truth table100%3/3
A and not(B)
A= builtin::reftype($val) eq "ARRAY"B= not tied @$val
ABresult
0X0
100
111
253 2 tie @$val, 'YAML::PP::Preserve::Array', @$val;
254 }
255 }
256 127 $self->{data}->{ $key } = $val;
257 }
258  
259 91 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
260 91 my ($self) = @_;
261 91 return $self->{keys}->[0];
262 }
263  
264 320 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=3 SCAR=11.0
265 320 my ($self, $last) = @_;
266 320 my $keys = $self->{keys};
267 320 for my $i (0 .. $#$keys) {
268 1167 ▶ if ("$keys->[ $i ]" eq "$last") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$keys->[$i]" eq "$last") 320 847 1167
269 320 return $keys->[ $i + 1 ];
270 }
271 }
272 0 return;
273 }
274  
275 305 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
276 305 my ($self, $key) = @_;
277 305 my $val = $self->{data}->{ $key };
278 }
279  
280 2 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
281 2 my ($self, $key) = @_;
282 2 @{ $self->{keys} } = grep { "$_" ne "$key" } @{ $self->{keys} };
283 2 delete $self->{data}->{ $key };
284 }
285  
286 34 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
287 34 my ($self, $key) = @_;
288 34 return exists $self->{data}->{ $key };
289 }
290  
291 9 ▶sub CLEAR {
Subroutine CLEAR: called CC=1 SCAR=0.0
292 9 my ($self) = @_;
293 9 $self->{keys} = [];
294 9 $self->{data} = {};
295 }
296  
297 3 ▶sub SCALAR {
Subroutine SCALAR: called CC=1 SCAR=0.0
298 3 my ($self) = @_;
299 3 return scalar %{ $self->{data} };
300 }
301  
302 package YAML::PP::Preserve::Array;
303 # experimental
304 48 ▶use Tie::Array; # provides Tie::StdArray, our parent below
Subroutine BEGIN: called CC=1 SCAR=0.0
305 48 our @ISA = qw/ Tie::StdArray /;
306 48 ▶use Scalar::Util qw/ reftype blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
307  
308 16 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=1 SCAR=0.0
309 16 my ($class, @items) = @_;
310 16 my $self = bless {
311 data => [@items],
312 }, $class;
313 16 return $self;
314 }
315  
316 99 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
317 99 my ($self, $i) = @_;
318 99 return $self->{data}->[ $i ];
319 }
320 101 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
321 101 my ($self) = @_;
322 101 return $#{ $self->{data} } + 1;
323 }
324  
325 42 ▶sub _preserve {
Subroutine _preserve: called CC=7 SCAR=19.5
326 42 my ($val) = @_;
327 42 ▶ if (ref $val and not blessed($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val and not builtin::blessed($val)) 13 29 42
Condition100%3/3
ref $val and not builtin::blessed($val)
!ll&&!rl&&r
191013
MC/DC100%2/2
ref $val and not builtin::blessed($val)
ref $val not builtin::blessed($val)
Truth table100%3/3
A and not(B)
A= ref $valB= not builtin::blessed($val)
ABresult
0X0
100
111
328 13 ▶ if (reftype($val) eq 'HASH' and not tied %$val) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($val) eq 'HASH' and not tied %$val) { } 5 8 13
elsif (builtin::reftype($val) eq 'ARRAY' and not tied @$val) { } 1 12 13
Condition100%3/3
builtin::reftype($val) eq "HASH" and not tied %$val
!ll&&!rl&&r
355
MC/DC100%2/2
builtin::reftype($val) eq "HASH" and not tied %$val
builtin::reftype($val) eq "HASH" not tied %$val
Truth table100%3/3
A and not(B)
A= builtin::reftype($val) eq "HASH"B= not tied %$val
ABresult
0X0
100
111
329 5 tie %$val, 'YAML::PP::Preserve::Hash', %$val;
330 }
331 ▶ elsif (reftype($val) eq 'ARRAY' and not tied @$val) {
Condition100%3/3
builtin::reftype($val) eq "ARRAY" and not tied @$val
!ll&&!rl&&r
521
MC/DC100%2/2
builtin::reftype($val) eq "ARRAY" and not tied @$val
builtin::reftype($val) eq "ARRAY" not tied @$val
Truth table100%3/3
A and not(B)
A= builtin::reftype($val) eq "ARRAY"B= not tied @$val
ABresult
0X0
100
111
332 1 tie @$val, 'YAML::PP::Preserve::Array', @$val;
333 }
334 }
335 42 return $val;
336 }
337  
338 6 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
339 6 my ($self, $i, $val) = @_;
340 6 _preserve($val);
341 6 $self->{data}->[ $i ] = $val;
342 }
343 14 ▶sub PUSH {
Subroutine PUSH: called CC=1 SCAR=0.0
344 14 my ($self, @args) = @_;
345 14 push @{ $self->{data} }, map { _preserve $_ } @args;
346 }
347 1 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=1 SCAR=0.0
348 1 my ($self, $i) = @_;
349 1 $#{ $self->{data} } = $i - 1;
350 }
351 1 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
352 1 my ($self, $i) = @_;
353 1 delete $self->{data}->[ $i ];
354 }
355 2 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
356 2 my ($self, $i) = @_;
357 2 return exists $self->{data}->[ $i ];
358 }
359 1 ▶sub CLEAR {
Subroutine CLEAR: called CC=1 SCAR=0.0
360 1 my ($self) = @_;
361 1 $self->{data} = [];
362 }
363 1 ▶sub SHIFT {
Subroutine SHIFT: called CC=1 SCAR=0.0
364 1 my ($self) = @_;
365 1 shift @{ $self->{data} };
366 }
367 2 ▶sub UNSHIFT {
Subroutine UNSHIFT: called CC=1 SCAR=0.0
368 2 my ($self, @args) = @_;
369 2 unshift @{ $self->{data} }, map { _preserve $_ } @args;
370 }
371 2 ▶sub SPLICE {
Subroutine SPLICE: called CC=1 SCAR=0.0
372 2 my ($self, $offset, $length, @args) = @_;
373 2 splice @{ $self->{data} }, $offset, $length, map { _preserve $_ } @args;
374 }
375 1 ▶sub EXTEND {}
Subroutine EXTEND: called CC=1 SCAR=0.0
376  
377  
378 package YAML::PP::Preserve::Scalar;
379  
380 use overload
381 48 ▶ fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
382 '+' => \&value,
383 '""' => \&value,
384 'bool' => \&value,
385 48 ;
386 87 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
387 87 my ($class, %args) = @_;
388 87 my $self = {
389 %args,
390 };
391 87 bless $self, $class;
392 }
393 2218 ▶sub value { $_[0]->{value} }
Subroutine value: called CC=1 SCAR=0.0
394 0 ▶sub tag { $_[0]->{tag} }
Subroutine tag: not called CC=1 SCAR=6.9
395 21 ▶sub style { $_[0]->{style} || 0 }
Subroutine style: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"style"} or 0 20 1 21
396 17 ▶sub alias { $_[0]->{alias} }
Subroutine alias: called CC=1 SCAR=0.0
397  
398 48 1;
399  
400 __END__
« blib/lib/YAML/PP/Reader.pm ↑ Summary blib/lib/YAML/PP/Schema/Failsafe.pm »