blib/lib/perl5i/2/Meta/Instance.pm

filter: Statementstmt 97.0% 130 / 134 Branchbran 84.3% 27 / 32 Conditioncond 91.6% 11 / 12 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 12 totaltotal 89.4% 194 / 217 SCAR 31.4
CC
23
Cov
94%
checksum
6.0
taint
4.0
untaint
4.0
CRAP 23.1
« blib/lib/perl5i/0/Meta/Instance.pm ↑ Summary blib/lib/perl5i/2/HASH.pm »
1 package perl5i::2::Meta::Instance;
2  
3 # Methods here are for $thing->mo->method.
4  
5 109 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 109 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 109 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 109 ▶no if $] >= 5.018000, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Don't import anything that might be misinterpreted as a method
11 109 require Scalar::Util;
12 109 require overload;
13 109 require Carp::Fix::1_25;
14  
15 109 ▶use perl5i::2::autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 109 ▶use parent qw(perl5i::2::Meta);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub id {
20 32 ▶ require Object::ID;
Subroutine id: called CC=2 SCAR=6.9
Pod undocumented
21  
22 # Hash::FieldHash cannot handle non-references
23 32 ▶ return Object::ID::object_id(ref ${${$_[0]}} ? ${${$_[0]}} : ${$_[0]});
Branch: 1 branch
BranchTrueFalseTotal
ref ${${$_[0];};} ? : 22 10 32
24 }
25  
26 sub class {
27 21 ▶ return ref ${${$_[0]}};
Subroutine class: called CC=1 SCAR=0.0
Pod undocumented
28 }
29  
30 sub reftype {
31 6 ▶ return Scalar::Util::reftype(${${$_[0]}});
Subroutine reftype: called CC=1 SCAR=0.0
Pod undocumented
32 }
33  
34  
35 # Only instances can be tainted
36  
37 # Returns the code which will run when the object is used as a string
38 my $has_string_overload = sub {
39 28 ▶ return overload::Method(${${$_[0]}}, q[""]) || overload::Method(${${$_[0]}}, q[0+])
Subroutine __ANON__: called CC=2 SCAR=7.0
Condition66%2/3
overload::Method(${${$_[0];};}, '""') || overload::Method(${${$_[0];};}, '0+')
l!l&&r!l&&!r
11017
MC/DC50%1/2
overload::Method(${${$_[0];};}, '""') || overload::Method(${${$_[0];};}, '0+')
overload::Method(${${$_[0];};}, '""') overload::Method(${${$_[0];};}, '0+')
Truth table66%2/3
A || B
A= overload::Method(${${$_[0];};}, '""')B= overload::Method(${${$_[0];};}, '0+')
ABresult
1X1
011
000
40 109 };
41  
42 sub is_tainted {
43 28 ▶ my $code;
Subroutine is_tainted: called CC=4 SCAR=13.9
Pod undocumented
44  
45 28 require Taint::Util;
46  
47 28 ▶ if( !ref ${${$_[0]}} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref ${${$_[0];};}) { } 9 19 28
elsif (ref ${${$_[0];};} eq 'SCALAR') { } 3 16 19
elsif ($code = $_[0]->$has_string_overload) { } 7 9 16
48 # Its a plain scalar
49 9 return Taint::Util::tainted(${${$_[0]}});
50 }
51 19 elsif( ref ${${$_[0]}} eq 'SCALAR' ) {
52 # Unblessed scalar
53 3 return Taint::Util::tainted(${${$_[0]}});
54 }
55 elsif( $code = $_[0]->$has_string_overload ) {
56 7 return Taint::Util::tainted( $code->(${${$_[0]}}) );
57 }
58 else {
59 9 return 0;
60 }
61  
62 0 die "Never should be reached";
63 }
64  
65  
66 sub taint {
67 9 ▶ require Taint::Util;
Subroutine taint: called CC=4 SCAR=13.9
Pod undocumented
68  
69 9 ▶ if( !ref ${${$_[0]}} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref ${${$_[0];};}) { } 3 6 9
elsif ($_[0]->$has_string_overload) { } 2 4 6
70 # Its a plain scalar
71 3 return Taint::Util::taint(${${$_[0]}});
72 }
73 elsif( $_[0]->$has_string_overload ) {
74 2 ▶ Carp::Fix::1_25::croak("Untainted overloaded objects cannot normally be made tainted") if
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->is_tainted 1 1 2
75 !$_[0]->is_tainted;
76 1 return 1;
77 }
78 else {
79 4 Carp::Fix::1_25::croak("Only scalars can normally be made tainted");
80 }
81  
82 0 Carp::Fix::1_25::confess("Should not be reached");
83 }
84  
85  
86 sub untaint {
87 9 ▶ require Taint::Util;
Subroutine untaint: called CC=4 SCAR=13.9
Pod undocumented
88  
89 9 ▶ if( !ref ${${$_[0]}} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref ${${$_[0];};}) { } 3 6 9
elsif ($_[0]->$has_string_overload and $_[0]->is_tainted) { } 1 5 6
90 # Its a plain scalar
91 3 return Taint::Util::untaint(${${$_[0]}});
92 }
93 ▶ elsif( $_[0]->$has_string_overload && $_[0]->is_tainted ) {
Condition100%3/3
$_[0]->$has_string_overload and $_[0]->is_tainted
!ll&&!rl&&r
411
MC/DC100%2/2
$_[0]->$has_string_overload and $_[0]->is_tainted
$_[0]->$has_string_overload $_[0]->is_tainted
Truth table100%3/3
A and B
A= $_[0]->$has_string_overloadB= $_[0]->is_tainted
ABresult
0X0
100
111
94 1 Carp::Fix::1_25::croak("Tainted overloaded objects cannot normally be untainted");
95 }
96 else {
97 5 return 1;
98 }
99  
100 0 Carp::Fix::1_25::confess("Should never be reached");
101 }
102  
103  
104 sub checksum {
105 33 ▶ my( $thing, %args ) = @_;
Subroutine checksum: called CC=6 SCAR=17.9
Pod undocumented
106  
107 33 state $algorithms = [qw(sha1 md5)];
108 33 ▶ $args{algorithm} //= 'sha1';
Condition100%2/2
$args{"algorithm"} //= "sha1"
l!l
1914
MC/DC100%1/1
$args{"algorithm"} //= "sha1"
$args{"algorithm"}
Truth table100%2/2
A
A= $args{"algorithm"}
Aresult
11
00
109 33 ▶ $args{algorithm} ~~ $algorithms or
Branch: 1 branch
BranchTrueFalseTotal
$args{"algorithm"} ~~ $algorithms or Carp::Fix::1_25::croak(qq[algorithm must be @{[$algorithms->join(" or ")];}]) 1 32 33
110 1 Carp::Fix::1_25::croak("algorithm must be @{[ $algorithms->join(' or ' ) ]}");
111  
112 32 state $algorithm2module = { sha1 => "Digest::SHA", md5 => "Digest::MD5" };
113  
114 32 state $format = [qw(hex base64 binary)];
115 32 ▶ $args{format} //= 'hex';
Condition100%2/2
$args{"format"} //= "hex"
l!l
1913
MC/DC100%1/1
$args{"format"} //= "hex"
$args{"format"}
Truth table100%2/2
A
A= $args{"format"}
Aresult
11
00
116 32 ▶ $args{format} ~~ $format or
Branch: 1 branch
BranchTrueFalseTotal
$args{"format"} ~~ $format or Carp::Fix::1_25::croak(qq[format must be @{[$format->join(" or ")];}]) 1 31 32
117 1 Carp::Fix::1_25::croak("format must be @{[ $format->join(' or ') ]}");
118  
119 31 state $prefix = { hex => 'hex', base64 => 'b64', binary => undef };
120  
121 31 my $module = $algorithm2module->{ $args{algorithm} };
122 31 ▶ my $digest = defined $prefix->{ $args{format} } ? $prefix->{ $args{format} } . 'digest' : 'digest';
Branch: 1 branch
BranchTrueFalseTotal
defined $$prefix{$args{'format'}} ? : 25 6 31
123  
124 31 $module->require;
125 31 my $digestor = $module->new;
126  
127 31 require Data::Dumper;
128  
129 31 my $d = Data::Dumper->new( [ ${$thing} ] );
130 31 $d->Deparse(1)->Terse(1)->Sortkeys(1)->Indent(0);
131  
132 31 $digestor->add( $d->Dump );
133 31 return $digestor->$digest;
134 }
135  
136  
137 sub is_equal {
138 43 ▶ my ($self, $other) = @_;
Subroutine is_equal: called CC=1 SCAR=0.0
Pod undocumented
139 43 require perl5i::2::equal;
140  
141 43 return perl5i::2::equal::are_equal($$$self, $other);
142 }
143  
144 109 *perl = \&as_perl;
145 sub as_perl {
146 33 ▶ require Data::Dumper;
Subroutine as_perl: called CC=2 SCAR=6.9
Pod undocumented
147  
148 33 state $options = [qw(Terse Sortkeys Deparse)];
149  
150 33 my $self = shift;
151 33 my $dumper = Data::Dumper->new([$$$self]);
152 33 for my $option (@$options) {
153 99 $dumper->$option(1);
154 }
155  
156 33 $dumper->Indent(1);
157  
158 33 return $dumper->Dump;
159 }
160  
161  
162 sub dump {
163 8 ▶ my $self = shift;
Subroutine dump: called CC=3 SCAR=11.0
Pod undocumented
164 8 my %args = @_;
165  
166 8 ▶ my $format = $args{format} // "perl";
Condition100%2/2
$args{"format"} // "perl"
l!l
71
MC/DC100%1/1
$args{"format"} // "perl"
$args{"format"}
Truth table100%2/2
A
A= $args{"format"}
Aresult
11
00
167 8 state $dumpers = {
168 json => "as_json",
169 yaml => "as_yaml",
170 perl => "as_perl",
171 };
172  
173 8 my $dumper = $dumpers->{$format};
174 8 ▶ Carp::Fix::1_25::croak("Unknown format '$format' for dump()") unless $dumper;
Branch: 1 branch
BranchTrueFalseTotal
unless $dumper 0 8 8
175  
176 8 return $self->$dumper(%args);
177 }
178  
179 sub as_json {
180 4 ▶ require JSON;
Subroutine as_json: called CC=2 SCAR=7.4
Pod undocumented
181 4 my $json = JSON->new
182 ->utf8
183 ->pretty
184 ->allow_unknown
185 ->allow_blessed
186 ->convert_blessed;
187  
188 # JSON doesn't seem to have an easy way to say
189 # "just dump objects as references please". This is their
190 # recommended way to do it (yarf).
191 local *UNIVERSAL::TO_JSON = sub {
192 1 ▶ require B;
Subroutine __ANON__: called CC=3 SCAR=13.3
193 1 my $b_obj = B::svref_2object( $_[0] );
194 1 ▶ return $b_obj->isa('B::HV') ? { %{ $_[0] } }
Branch: 1 branch
BranchTrueFalseTotal
$b_obj->isa('B::AV') ? : 0 0 0
195 1 ▶ : $b_obj->isa('B::AV') ? [ @{ $_[0] } ]
Branch: 1 branch
BranchTrueFalseTotal
$b_obj->isa('B::HV') ? : 1 0 1
196 : undef
197 ;
198 4 ▶ } unless defined &UNIVERSAL::TO_JSON;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &UNIVERSAL::TO_JSON 4 0 4
199  
200 4 return $json->encode(${${$_[0]}});
201 }
202  
203 sub as_yaml {
204 4 ▶ require YAML::Any;
Subroutine as_yaml: called CC=1 SCAR=0.0
Pod undocumented
205 4 return YAML::Any::Dump(${${$_[0]}});
206 }
207  
208 109 1;
« blib/lib/perl5i/0/Meta/Instance.pm ↑ Summary blib/lib/perl5i/2/HASH.pm »