YAML/Types.pm

filter: Statementstmt 80.2% 134 / 167 Branchbran 78.3% 47 / 60 Conditioncond 60.0% 12 / 20 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 43.5% 17 / 39 Podpod n/a totaltotal 71.2% 213 / 299 CC 41 SCAR 17.7
CC
41
Cov
78%
yaml_load
14.0
yaml_dump
8.3
yaml_dump
8.0
CRAP 5.9
« YAML/Dumper/Base.pm ↑ Summary YAML/Mo.pm »
1 package YAML::Types;
2  
3 48 ▶use YAML::Mo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 48 ▶use YAML::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # XXX These classes and their APIs could still use some refactoring,
7 # but at least they work for now.
8 #-------------------------------------------------------------------------------
9 package YAML::Type::blessed;
10  
11 48 ▶use YAML::Mo; # XXX
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 26 ▶sub yaml_dump {
Subroutine yaml_dump: called CC=5 SCAR=16.1
14 26 my $self = shift;
15 26 my ($value) = @_;
16 26 my ($class, $type) = YAML::Mo::Object->node_info($value);
17 48 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 26 my $kind = lc($type) . ':';
19 26 ▶ my $tag = ${$class . '::ClassTag'} ||
Condition100%2/2
${$class . '::ClassTag';} || "!perl/$kind$class"
l!l
125
MC/DC100%1/1
${$class . '::ClassTag';} || "!perl/$kind$class"
${$class . '::ClassTag';}
Truth table100%2/2
A
A= ${$class . '::ClassTag';}
Aresult
11
00
20 "!perl/$kind$class";
21 26 ▶ if ($type eq 'REF') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'REF') { } 1 25 26
elsif ($type eq 'SCALAR') { } 7 18 25
elsif ($type eq 'GLOB') { } 2 16 18
22 YAML::Node->new(
23 1 {(&YAML::VALUE, ${$_[0]})}, $tag
24 );
25 }
26 elsif ($type eq 'SCALAR') {
27 7 $_[1] = $$value;
28 7 YAML::Node->new($_[1], $tag);
29 }
30 elsif ($type eq 'GLOB') {
31 # blessed glob support is minimal, and will not round-trip
32 # initial aim: to not cause an error
33 2 return YAML::Type::glob->yaml_dump($value, $tag);
34 } else {
35 16 YAML::Node->new($value, $tag);
36 }
37 }
38  
39 #-------------------------------------------------------------------------------
40 package YAML::Type::undef;
41  
42 0 ▶sub yaml_dump {
Subroutine yaml_dump: not called CC=1 SCAR=6.9
43 0 my $self = shift;
44 }
45  
46 0 ▶sub yaml_load {
Subroutine yaml_load: not called CC=1 SCAR=6.9
47 0 my $self = shift;
48 }
49  
50 #-------------------------------------------------------------------------------
51 package YAML::Type::glob;
52  
53 8 ▶sub yaml_dump {
Subroutine yaml_dump: called CC=8 SCAR=20.8
54 8 my $self = shift;
55 # $_[0] remains as the glob
56 8 ▶ my $tag = pop @_ if 2==@_;
Branch: 1 branch
BranchTrueFalseTotal
if 2 == @_ 2 6 8
57  
58 8 ▶ $tag = '!perl/glob:' unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 3 5 8
59 8 my $ynode = YAML::Node->new({}, $tag);
60 8 for my $type (qw(PACKAGE NAME SCALAR ARRAY HASH CODE IO)) {
61 56 my $value = *{$_[0]}{$type};
62 56 ▶ $value = $$value if $type eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "SCALAR" 8 48 56
63 56 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 28 28 56
64 28 ▶ if ($type eq 'IO') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "IO") 1 27 28
65 1 my @stats = qw(device inode mode links uid gid rdev size
66 atime mtime ctime blksize blocks);
67 1 undef $value;
68 1 $value->{stat} = YAML::Node->new({});
69 1 ▶ if ($value->{fileno} = fileno(*{$_[0]})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{"fileno"} = fileno do { $_[0] }) 1 0 1
70 1 local $^W;
71 1 map {$value->{stat}{shift @stats} = $_} stat(*{$_[0]});
72 1 $value->{tell} = tell(*{$_[0]});
73 }
74 }
75 28 $ynode->{$type} = $value;
76 }
77 }
78 8 return $ynode;
79 }
80  
81 6 ▶sub yaml_load {
Subroutine yaml_load: called CC=12 SCAR=26.4
82 6 my $self = shift;
83 6 my ($node, $class, $loader) = @_;
84 6 my ($name, $package);
85 6 ▶ if (defined $node->{NAME}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{'NAME'}) { } 5 1 6
86 5 $name = $node->{NAME};
87 5 delete $node->{NAME};
88 }
89 else {
90 1 $loader->warn('YAML_LOAD_WARN_GLOB_NAME');
91 1 return undef;
92 }
93 5 ▶ if (defined $node->{PACKAGE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{'PACKAGE'}) { } 4 1 5
94 4 $package = $node->{PACKAGE};
95 4 delete $node->{PACKAGE};
96 }
97 else {
98 1 $package = 'main';
99 }
100 48 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 5 ▶ if (exists $node->{SCALAR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"SCALAR"}) 4 1 5
102 4 ▶ if ($YAML::LoadBlessed and $loader->load_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($YAML::LoadBlessed and $loader->load_code) 0 4 4
Condition66%2/3
$YAML::LoadBlessed and $loader->load_code
!ll&&!rl&&r
220
MC/DC0%0/2
$YAML::LoadBlessed and $loader->load_code
$YAML::LoadBlessed $loader->load_code
Truth table66%2/3
A and B
A= $YAML::LoadBlessedB= $loader->load_code
ABresult
0X0
100
111
103 0 *{"${package}::$name"} = \$node->{SCALAR};
104 }
105 4 delete $node->{SCALAR};
106 }
107 5 for my $elem (qw(ARRAY HASH CODE IO)) {
108 20 ▶ if (exists $node->{$elem}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{$elem}) 2 18 20
109 2 ▶ if ($elem eq 'IO') {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem eq "IO") 0 2 2
110 0 $loader->warn('YAML_LOAD_WARN_GLOB_IO');
111 0 delete $node->{IO};
112 0 next;
113 }
114 2 ▶ if ($YAML::LoadBlessed and $loader->load_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($YAML::LoadBlessed and $loader->load_code) 0 2 2
Condition33%1/3
$YAML::LoadBlessed and $loader->load_code
!ll&&!rl&&r
020
MC/DC0%0/2
$YAML::LoadBlessed and $loader->load_code
$YAML::LoadBlessed $loader->load_code
Truth table33%1/3
A and B
A= $YAML::LoadBlessedB= $loader->load_code
ABresult
0X0
100
111
115 0 *{"${package}::$name"} = $node->{$elem};
116 }
117 2 delete $node->{$elem};
118 }
119 }
120 5 for my $elem (sort keys %$node) {
121 1 $loader->warn('YAML_LOAD_WARN_BAD_GLOB_ELEM', $elem);
122 }
123 5 return *{"${package}::$name"};
124 }
125  
126 #-------------------------------------------------------------------------------
127 package YAML::Type::code;
128  
129 48 my $dummy_warned = 0;
130 48 my $default = '{ "DUMMY" }';
131  
132 9 ▶sub yaml_dump {
Subroutine yaml_dump: called CC=8 SCAR=21.1
133 9 my $self = shift;
134 9 my $code;
135 9 my ($dumpflag, $value) = @_;
136 9 my ($class, $type) = YAML::Mo::Object->node_info($value);
137 9 my $tag = "!perl/code";
138 9 ▶ $tag .= ":$class" if defined $class;
Branch: 1 branch
BranchTrueFalseTotal
if defined $class 1 8 9
139 9 ▶ if (not $dumpflag) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dumpflag) { } 2 7 9
140 2 $code = $default;
141 }
142 else {
143 7 ▶ bless $value, "CODE" if $class;
Branch: 1 branch
BranchTrueFalseTotal
if $class 1 6 7
144 7 eval { require B::Deparse };
145 7 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
146 7 my $deparse = B::Deparse->new();
147 7 eval {
148 7 local $^W = 0;
149 7 $code = $deparse->coderef2text($value);
150 };
151 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
152 0 ▶ warn YAML::YAML_DUMP_WARN_DEPARSE_FAILED() if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
153 0 $code = $default;
154 }
155 7 ▶ bless $value, $class if $class;
Branch: 1 branch
BranchTrueFalseTotal
if $class 1 6 7
156 7 chomp $code;
157 7 $code .= "\n";
158 }
159 9 $_[2] = $code;
160 9 YAML::Node->new($_[2], $tag);
161 }
162  
163 6 ▶sub yaml_load {
Subroutine yaml_load: called CC=7 SCAR=20.6
164 6 my $self = shift;
165 6 my ($node, $class, $loader) = @_;
166 6 ▶ if ($loader->load_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loader->load_code) { } 3 3 6
167 3 ▶ my $code = eval "package main; sub $node";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
168 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 3 3
169 0 $loader->warn('YAML_LOAD_WARN_PARSE_CODE', $@);
170 0 ▶ return sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 }
172 else {
173 3 ▶ CORE::bless $code, $class if ($class and $YAML::LoadBlessed);
Branch: 1 branch
BranchTrueFalseTotal
if $class and $YAML::LoadBlessed 0 3 3
Condition66%2/3
$class and $YAML::LoadBlessed
!ll&&!rl&&r
210
MC/DC0%0/2
$class and $YAML::LoadBlessed
$class $YAML::LoadBlessed
Truth table66%2/3
A and B
A= $classB= $YAML::LoadBlessed
ABresult
0X0
100
111
174 3 return $code;
175 }
176 }
177 else {
178 3 ▶ return CORE::bless sub {}, $class if ($class and $YAML::LoadBlessed);
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $class and $YAML::LoadBlessed 0 3 3
Condition33%1/3
$class and $YAML::LoadBlessed
!ll&&!rl&&r
300
MC/DC0%0/2
$class and $YAML::LoadBlessed
$class $YAML::LoadBlessed
Truth table33%1/3
A and B
A= $classB= $YAML::LoadBlessed
ABresult
0X0
100
111
179 3 ▶ return sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
180 }
181 }
182  
183 #-------------------------------------------------------------------------------
184 package YAML::Type::ref;
185  
186 33 ▶sub yaml_dump {
Subroutine yaml_dump: called CC=1 SCAR=0.0
187 33 my $self = shift;
188 33 YAML::Node->new({(&YAML::VALUE, ${$_[0]})}, '!perl/ref')
189 }
190  
191 0 ▶sub yaml_load {
Subroutine yaml_load: not called CC=2 SCAR=17.9
192 0 my $self = shift;
193 0 my ($node, $class, $loader) = @_;
194 $loader->die('YAML_LOAD_ERR_NO_DEFAULT_VALUE', 'ptr')
195 0 ▶ unless exists $node->{&YAML::VALUE};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$node{&YAML::VALUE} 0 0 0
196 0 return \$node->{&YAML::VALUE};
197 }
198  
199 #-------------------------------------------------------------------------------
200 package YAML::Type::regexp;
201  
202 # XXX Be sure to handle blessed regexps (if possible)
203 0 ▶sub yaml_dump {
Subroutine yaml_dump: not called CC=1 SCAR=6.9
204 0 die "YAML::Type::regexp::yaml_dump not currently implemented";
205 }
206  
207 48 ▶use constant _QR_TYPES => {
Subroutine BEGIN: called CC=1 SCAR=3.4
208 7 ▶ '' => sub { qr{$_[0]} },
Subroutine __ANON__: called CC=1 SCAR=0.0
209 0 ▶ x => sub { qr{$_[0]}x },
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 ▶ i => sub { qr{$_[0]}i },
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 0 ▶ s => sub { qr{$_[0]}s },
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 2 ▶ m => sub { qr{$_[0]}m },
Subroutine __ANON__: called CC=1 SCAR=0.0
213 0 ▶ ix => sub { qr{$_[0]}ix },
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 0 ▶ sx => sub { qr{$_[0]}sx },
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 0 ▶ mx => sub { qr{$_[0]}mx },
Subroutine __ANON__: not called CC=1 SCAR=6.9
216 0 ▶ si => sub { qr{$_[0]}si },
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 ▶ mi => sub { qr{$_[0]}mi },
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 ▶ ms => sub { qr{$_[0]}sm },
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 ▶ six => sub { qr{$_[0]}six },
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 ▶ mix => sub { qr{$_[0]}mix },
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 0 ▶ msx => sub { qr{$_[0]}msx },
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 0 ▶ msi => sub { qr{$_[0]}msi },
Subroutine __ANON__: not called CC=1 SCAR=6.9
223 0 ▶ msix => sub { qr{$_[0]}msix },
Subroutine __ANON__: not called CC=1 SCAR=6.9
224 48 };
225  
226 10 ▶sub yaml_load {
Subroutine yaml_load: called CC=5 SCAR=16.2
227 10 my $self = shift;
228 10 my ($node, $class) = @_;
229 10 ▶ return qr{$node} unless $node =~ /^\(\?([\^\-uxism]*):(.*)\)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $node =~ /^\(\?([\^\-uxism]*):(.*)\)\z/s 1 9 10
230 9 my ($flags, $re) = ($1, $2);
231 9 $flags =~ s/-.*//;
232 9 $flags =~ s/^\^//;
233 9 $flags =~ tr/u//d;
234 0 ▶ my $sub = _QR_TYPES->{$flags} || sub { qr{$_[0]} };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
{ => sub { qr/$_[0]/; } , i => sub { qr/$_[0]/i; } , ix => sub { qr/$_[0]/xi; } , m => sub { qr/$_[0]/m; } , mi => sub { qr/$_[0]/im; } , mix => sub { qr/$_[0]/mix; } , ms => sub { qr/$_[0]/ms; } , msi => sub { qr/$_[0]/ism; } , msix => sub { qr/$_[0]/imsx; } , msx => sub { qr/$_[0]/msx; } , mx => sub { qr/$_[0]/mx; } , s => sub { qr/$_[0]/s; } , si => sub { qr/$_[0]/is; } , six => sub { qr/$_[0]/six; } , sx => sub { qr/$_[0]/sx; } , x => sub { qr/$_[0]/x; } }->{$flags} || sub { qr/$_[0]/; }
l!l&&r!l&&!r
900
MC/DC0%0/2
{ => sub { qr/$_[0]/; } , i => sub { qr/$_[0]/i; } , ix => sub { qr/$_[0]/xi; } , m => sub { qr/$_[0]/m; } , mi => sub { qr/$_[0]/im; } , mix => sub { qr/$_[0]/mix; } , ms => sub { qr/$_[0]/ms; } , msi => sub { qr/$_[0]/ism; } , msix => sub { qr/$_[0]/imsx; } , msx => sub { qr/$_[0]/msx; } , mx => sub { qr/$_[0]/mx; } , s => sub { qr/$_[0]/s; } , si => sub { qr/$_[0]/is; } , six => sub { qr/$_[0]/six; } , sx => sub { qr/$_[0]/sx; } , x => sub { qr/$_[0]/x; } }->{$flags} || sub { qr/$_[0]/; }
{ => sub { qr/$_[0]/; } , i => sub { qr/$_[0]/i; } , ix => sub { qr/$_[0]/xi; } , m => sub { qr/$_[0]/m; } , mi => sub { qr/$_[0]/im; } , mix => sub { qr/$_[0]/mix; } , ms => sub { qr/$_[0]/ms; } , msi => sub { qr/$_[0]/ism; } , msix => sub { qr/$_[0]/imsx; } , msx => sub { qr/$_[0]/msx; } , mx => sub { qr/$_[0]/mx; } , s => sub { qr/$_[0]/s; } , si => sub { qr/$_[0]/is; } , six => sub { qr/$_[0]/six; } , sx => sub { qr/$_[0]/sx; } , x => sub { qr/$_[0]/x; } }->{$flags} sub { qr/$_[0]/; }
Truth table33%1/3
A || B
A= { => sub { qr/$_[0]/; } , i => sub { qr/$_[0]/i; } , ix => sub { qr/$_[0]/xi; } , m => sub { qr/$_[0]/m; } , mi => sub { qr/$_[0]/im; } , mix => sub { qr/$_[0]/mix; } , ms => sub { qr/$_[0]/ms; } , msi => sub { qr/$_[0]/ism; } , msix => sub { qr/$_[0]/imsx; } , msx => sub { qr/$_[0]/msx; } , mx => sub { qr/$_[0]/mx; } , s => sub { qr/$_[0]/s; } , si => sub { qr/$_[0]/is; } , six => sub { qr/$_[0]/six; } , sx => sub { qr/$_[0]/sx; } , x => sub { qr/$_[0]/x; } }->{$flags}B= sub { qr/$_[0]/; }
ABresult
1X1
011
000
235 9 my $qr = &$sub($re);
236 9 ▶ bless $qr, $class if (length $class and $YAML::LoadBlessed);
Branch: 1 branch
BranchTrueFalseTotal
if length $class and $YAML::LoadBlessed 1 8 9
Condition100%3/3
length $class and $YAML::LoadBlessed
!ll&&!rl&&r
711
MC/DC100%2/2
length $class and $YAML::LoadBlessed
length $class $YAML::LoadBlessed
Truth table100%3/3
A and B
A= length $classB= $YAML::LoadBlessed
ABresult
0X0
100
111
237 9 return $qr;
238 }
239  
240 48 1;
« YAML/Dumper/Base.pm ↑ Summary YAML/Mo.pm »