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

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 269 CC 15 SCAR 20.8
CC
15
Cov
0%
is_tainted
20.0
taint
20.0
as_json
20.0
CRAP 8.0
1 package perl5i::2::Meta::Instance;
2  
3 # Methods here are for $thing->mo->method.
4  
5 use 5.010_000;
6 use strict;
7 use warnings;
8 no if $] >= 5.018000, warnings => 'experimental::smartmatch';
9  
10 # Don't import anything that might be misinterpreted as a method
11 require Scalar::Util;
12 require overload;
13 require Carp::Fix::1_25;
14  
15 use perl5i::2::autobox;
16  
17 use parent qw(perl5i::2::Meta);
18  
19 sub id {
20 require Object::ID;
21  
22 # Hash::FieldHash cannot handle non-references
23 return Object::ID::object_id(ref ${${$_[0]}} ? ${${$_[0]}} : ${$_[0]});
24 }
25  
26 sub class {
27 return ref ${${$_[0]}};
28 }
29  
30 sub reftype {
31 return Scalar::Util::reftype(${${$_[0]}});
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 return overload::Method(${${$_[0]}}, q[""]) || overload::Method(${${$_[0]}}, q[0+])
40 };
41  
42 sub is_tainted {
43 my $code;
44  
45 require Taint::Util;
46  
47 if( !ref ${${$_[0]}} ) {
48 # Its a plain scalar
49 return Taint::Util::tainted(${${$_[0]}});
50 }
51 elsif( ref ${${$_[0]}} eq 'SCALAR' ) {
52 # Unblessed scalar
53 return Taint::Util::tainted(${${$_[0]}});
54 }
55 elsif( $code = $_[0]->$has_string_overload ) {
56 return Taint::Util::tainted( $code->(${${$_[0]}}) );
57 }
58 else {
59 return 0;
60 }
61  
62 die "Never should be reached";
63 }
64  
65  
66 sub taint {
67 require Taint::Util;
68  
69 if( !ref ${${$_[0]}} ) {
70 # Its a plain scalar
71 return Taint::Util::taint(${${$_[0]}});
72 }
73 elsif( $_[0]->$has_string_overload ) {
74 Carp::Fix::1_25::croak("Untainted overloaded objects cannot normally be made tainted") if
75 !$_[0]->is_tainted;
76 return 1;
77 }
78 else {
79 Carp::Fix::1_25::croak("Only scalars can normally be made tainted");
80 }
81  
82 Carp::Fix::1_25::confess("Should not be reached");
83 }
84  
85  
86 sub untaint {
87 require Taint::Util;
88  
89 if( !ref ${${$_[0]}} ) {
90 # Its a plain scalar
91 return Taint::Util::untaint(${${$_[0]}});
92 }
93 elsif( $_[0]->$has_string_overload && $_[0]->is_tainted ) {
94 Carp::Fix::1_25::croak("Tainted overloaded objects cannot normally be untainted");
95 }
96 else {
97 return 1;
98 }
99  
100 Carp::Fix::1_25::confess("Should never be reached");
101 }
102  
103  
104 sub checksum {
105 my( $thing, %args ) = @_;
106  
107 state $algorithms = [qw(sha1 md5)];
108 $args{algorithm} //= 'sha1';
109 $args{algorithm} ~~ $algorithms or
110 Carp::Fix::1_25::croak("algorithm must be @{[ $algorithms->join(' or ' ) ]}");
111  
112 state $algorithm2module = { sha1 => "Digest::SHA", md5 => "Digest::MD5" };
113  
114 state $format = [qw(hex base64 binary)];
115 $args{format} //= 'hex';
116 $args{format} ~~ $format or
117 Carp::Fix::1_25::croak("format must be @{[ $format->join(' or ') ]}");
118  
119 state $prefix = { hex => 'hex', base64 => 'b64', binary => undef };
120  
121 my $module = $algorithm2module->{ $args{algorithm} };
122 my $digest = defined $prefix->{ $args{format} } ? $prefix->{ $args{format} } . 'digest' : 'digest';
123  
124 $module->require;
125 my $digestor = $module->new;
126  
127 require Data::Dumper;
128  
129 my $d = Data::Dumper->new( [ ${$thing} ] );
130 $d->Deparse(1)->Terse(1)->Sortkeys(1)->Indent(0);
131  
132 $digestor->add( $d->Dump );
133 return $digestor->$digest;
134 }
135  
136  
137 sub is_equal {
138 my ($self, $other) = @_;
139 require perl5i::2::equal;
140  
141 return perl5i::2::equal::are_equal($$$self, $other);
142 }
143  
144 *perl = \&as_perl;
145 sub as_perl {
146 require Data::Dumper;
147  
148 state $options = [qw(Terse Sortkeys Deparse)];
149  
150 my $self = shift;
151 my $dumper = Data::Dumper->new([$$$self]);
152 for my $option (@$options) {
153 $dumper->$option(1);
154 }
155  
156 $dumper->Indent(1);
157  
158 return $dumper->Dump;
159 }
160  
161  
162 sub dump {
163 my $self = shift;
164 my %args = @_;
165  
166 my $format = $args{format} // "perl";
167 state $dumpers = {
168 json => "as_json",
169 yaml => "as_yaml",
170 perl => "as_perl",
171 };
172  
173 my $dumper = $dumpers->{$format};
174 Carp::Fix::1_25::croak("Unknown format '$format' for dump()") unless $dumper;
175  
176 return $self->$dumper(%args);
177 }
178  
179 sub as_json {
180 require JSON::MaybeXS;
181 my $json = JSON::MaybeXS->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 require B;
193 my $b_obj = B::svref_2object( $_[0] );
194 return $b_obj->isa('B::HV') ? { %{ $_[0] } }
195 : $b_obj->isa('B::AV') ? [ @{ $_[0] } ]
196 : undef
197 ;
198 } unless defined &UNIVERSAL::TO_JSON;
199  
200 return $json->encode(${${$_[0]}});
201 }
202  
203 sub as_yaml {
204 require YAML::Any;
205 return YAML::Any::Dump(${${$_[0]}});
206 }
207  
208 1;