File Coverage

blib/lib/perl5i/1/Meta/Instance.pm
Criterion Covered Total %
statement 15 56 26.7
branch 0 14 0.0
condition 0 7 0.0
subroutine 5 11 45.4
pod 0 6 0.0
total 20 94 21.2


line stmt bran cond sub pod time code
1             package perl5i::1::Meta::Instance;
2              
3 2     2   11 use strict;
  2         4  
  2         68  
4 2     2   10 use warnings;
  2         4  
  2         79  
5 2     2   2214 no if $] > 5.018000, warnings => 'experimental::smartmatch';
  2         22  
  2         17  
6              
7             require Scalar::Util;
8             require overload;
9             require Carp;
10              
11 2     2   1272 use perl5i::1::autobox;
  2         6  
  2         59  
12              
13 2     2   16901 use parent qw(perl5i::1::Meta);
  2         5  
  2         19  
14              
15             sub class {
16 0     0 0   return ref ${$_[0]};
  0            
17             }
18              
19             sub reftype {
20 0     0 0   return Scalar::Util::reftype(${$_[0]});
  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 0     0 0   my $code;
33              
34 0 0         if( $code = $_[0]->$has_string_overload ) {
35 0           require Taint::Util;
36 0           return Taint::Util::tainted( $code->(${$_[0]}) );
  0            
37             }
38             else {
39 0           return 0;
40             }
41              
42 0           die "Never should be reached";
43             }
44              
45              
46             sub taint {
47 0 0   0 0   if( $_[0]->$has_string_overload ) {
48 0 0         Carp::croak "Untainted overloaded objects cannot normally be made tainted" if
49             !$_[0]->is_tainted;
50 0           return 1;
51             }
52             else {
53 0           Carp::croak "Only scalars can normally be made tainted";
54             }
55              
56 0           Carp::confess "Should not be reached";
57             }
58              
59              
60             sub untaint {
61 0 0 0 0 0   if( $_[0]->$has_string_overload && $_[0]->is_tainted ) {
62 0           Carp::croak "Tainted overloaded objects cannot normally be untainted";
63             }
64             else {
65 0           return 1;
66             }
67              
68 0           Carp::confess "Should never be reached";
69             }
70              
71              
72             sub checksum {
73 0     0 0   my( $thing, %args ) = @_;
74              
75 0           my $algorithms = [qw(sha1 md5)];
76 0   0       $args{algorithm} //= 'sha1';
77 0 0         $args{algorithm} ~~ $algorithms or
78 0           Carp::croak("algorithm must be @{[ $algorithms->join(' or ' ) ]}");
79              
80 0           my $algorithm2module = { sha1 => "Digest::SHA", md5 => "Digest::MD5" };
81              
82 0           my $format = [qw(hex base64 binary)];
83 0   0       $args{format} //= 'hex';
84 0 0         $args{format} ~~ $format or
85 0           Carp::croak("format must be @{[ $format->join(' or ') ]}");
86              
87 0           my %prefix = ( hex => 'hex', base64 => 'b64', binary => undef );
88              
89 0           my $module = $algorithm2module->{ $args{algorithm} };
90 0 0         my $digest = defined $prefix{ $args{format} } ? $prefix{ $args{format} } . 'digest' : 'digest';
91              
92 0           Module::Load::load($module);
93 0           my $digestor = $module->new;
94              
95 0           require Data::Dumper;
96              
97 0           my $d = Data::Dumper->new( [ ${$thing} ] );
  0            
98 0           $d->Deparse(1)->Terse(1)->Sortkeys(1)->Indent(0);
99              
100 0           $digestor->add( $d->Dump );
101 0           return $digestor->$digest;
102             }
103              
104             1;