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

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 135 CC 6 SCAR 15.2
CC
6
Cov
0%
taint
12.0
is_tainted
6.0
untaint
6.0
CRAP 4.6
1 package perl5i::1::Meta::Instance;
2  
3 use strict;
4 use warnings;
5 no if $] > 5.018000, warnings => 'experimental::smartmatch';
6  
7 require Scalar::Util;
8 require overload;
9 require Carp;
10  
11 use perl5i::1::autobox;
12  
13 use parent qw(perl5i::1::Meta);
14  
15 sub class {
16 return ref ${$_[0]};
17 }
18  
19 sub reftype {
20 return Scalar::Util::reftype(${$_[0]});
21 }
22  
23  
24 # Only instances can be tainted
25  
26 # Returns the code which will run when the object is used as a string
27 my $has_string_overload = sub {
28 return overload::Method(${$_[0]}, q[""]) || overload::Method(${$_[0]}, q[0+])
29 };
30  
31 sub is_tainted {
32 my $code;
33  
34 if( $code = $_[0]->$has_string_overload ) {
35 require Taint::Util;
36 return Taint::Util::tainted( $code->(${$_[0]}) );
37 }
38 else {
39 return 0;
40 }
41  
42 die "Never should be reached";
43 }
44  
45  
46 sub taint {
47 if( $_[0]->$has_string_overload ) {
48 Carp::croak "Untainted overloaded objects cannot normally be made tainted" if
49 !$_[0]->is_tainted;
50 return 1;
51 }
52 else {
53 Carp::croak "Only scalars can normally be made tainted";
54 }
55  
56 Carp::confess "Should not be reached";
57 }
58  
59  
60 sub untaint {
61 if( $_[0]->$has_string_overload && $_[0]->is_tainted ) {
62 Carp::croak "Tainted overloaded objects cannot normally be untainted";
63 }
64 else {
65 return 1;
66 }
67  
68 Carp::confess "Should never be reached";
69 }
70  
71  
72 sub checksum {
73 my( $thing, %args ) = @_;
74  
75 my $algorithms = [qw(sha1 md5)];
76 $args{algorithm} //= 'sha1';
77 $args{algorithm} ~~ $algorithms or
78 Carp::croak("algorithm must be @{[ $algorithms->join(' or ' ) ]}");
79  
80 my $algorithm2module = { sha1 => "Digest::SHA", md5 => "Digest::MD5" };
81  
82 my $format = [qw(hex base64 binary)];
83 $args{format} //= 'hex';
84 $args{format} ~~ $format or
85 Carp::croak("format must be @{[ $format->join(' or ') ]}");
86  
87 my %prefix = ( hex => 'hex', base64 => 'b64', binary => undef );
88  
89 my $module = $algorithm2module->{ $args{algorithm} };
90 my $digest = defined $prefix{ $args{format} } ? $prefix{ $args{format} } . 'digest' : 'digest';
91  
92 Module::Load::load($module);
93 my $digestor = $module->new;
94  
95 require Data::Dumper;
96  
97 my $d = Data::Dumper->new( [ ${$thing} ] );
98 $d->Deparse(1)->Terse(1)->Sortkeys(1)->Indent(0);
99  
100 $digestor->add( $d->Dump );
101 return $digestor->$digest;
102 }
103  
104 1;