Deadly.pm

filter: Statementstmt 58.0% 87 / 150 Branchbran 28.2% 13 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.8% 22 / 35 Podpod 85.7% 18 / 21 totaltotal 55.5% 140 / 252 CC 25 SCAR 13.0
CC
25
Cov
51%
kill_function
9.5
AUTOLOAD
4.2
DOES
4.2
CRAP 3.7
↑ Summary Deadly/_unsafe.pm »
1 ## no critic (PodSections,UseWarnings,RcsKeywords)
2 package Object::Deadly;
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Devel::Symdump ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util qw(refaddr blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use English '$EVAL_ERROR'; ## no critic Interpolation
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp::Clan 5.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use vars '$VERSION'; ## no critic Interpolation
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 $VERSION = '0.09';
13  
14 7 ▶sub new_with {
Subroutine new_with: called CC=1 SCAR=0.0
Pod documented
15  
16 # Public, overridable class method. Returns an _unsafe
17 # object. Accepts a single reference which will be blessed.
18 7 my ( $class, $self ) = @_;
19 7 my $implementation_class = "$class\::_unsafe";
20  
21 7 return bless $self, $implementation_class;
22 }
23  
24 21 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
25  
26 # Public, overridable class method. Returns an ${class}::_unsafe
27 # object.
28  
29 21 my $class = shift @_;
30 21 my $implementation_class = "$class\::_unsafe";
31  
32 21 my $data;
33 21 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 10 11 21
34 10 $data = shift @_;
35 }
36 else {
37  
38 # No sense in loading this unless we actually use it.
39 11 require Devel::StackTrace;
40  
41 11 $data = Devel::StackTrace->new( ignore_package => $class )->as_string;
42 11 $data =~ s/\AT/Object::Deadly t/xm;
43  
44 }
45  
46 21 my $self = bless \$data, $implementation_class;
47 3 ▶ no strict 'refs'; ## no critic strict
Subroutine BEGIN: called CC=1 SCAR=0.0
48 21 ${"${implementation_class}::SIMPLE_OBJECTS"}{ refaddr $self} = undef;
49  
50 21 return $self;
51 }
52  
53 228 ▶sub kill_function {
Subroutine kill_function: called CC=4 SCAR=22.5
Pod documented
54  
55 # Public, overridable class method. Creates a deadly function in
56 # the ${class}::_unsafe class.
57  
58 228 my ( $class, $func, $death ) = @_;
59 228 my $implementation_class = "$class\::_unsafe";
60 228 my $function_name = "$implementation_class\::$func";
61 3 ▶ no strict 'refs'; ## no critic Strict
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 228 ▶ if ( defined &$function_name ) { ## no critic Sigil
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$function_name) 171 57 228
64 171 return;
65 }
66  
67 # Get a default death if our caller hasn't given us something
68 # special.
69 57 ▶ if ( not defined $death ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $death) 57 0 57
70 57 $death = $class->get_death;
71 }
72  
73 57 my $src = <<"PROXY_FOR_DEATH";
74 57 #line @{[__LINE__+2]} "@{[__FILE__]}"
75 package $implementation_class;
76 \$death = \$death;
77 sub $func {
78 0 ▶ if ( defined Object::Deadly::blessed \$_[0] ) {
Subroutine AUTOLOAD: not called CC=2 SCAR=14.3
Subroutine DOES: called CC=2 SCAR=14.3
Subroutine SUPER: not called CC=2 SCAR=14.3
Subroutine VERSION: called CC=2 SCAR=14.3
Subroutine blessed: not called CC=2 SCAR=14.3
Subroutine can: called CC=2 SCAR=14.3
Subroutine dump: not called CC=2 SCAR=14.3
Subroutine exports: not called CC=2 SCAR=14.3
Subroutine import: called CC=2 SCAR=14.3
Subroutine isa: called CC=2 SCAR=14.3
Subroutine moniker: not called CC=2 SCAR=14.3
Subroutine peek: not called CC=2 SCAR=14.3
Subroutine plural_moniker: not called CC=2 SCAR=14.3
Subroutine refaddr: not called CC=2 SCAR=14.3
Subroutine require: called CC=2 SCAR=14.3
Subroutine super: not called CC=2 SCAR=14.3
Subroutine unimport: not called CC=2 SCAR=14.3
Subroutine use: not called CC=2 SCAR=14.3
Subroutine which: not called CC=2 SCAR=14.3
Pod documented
Pod undocumented
Pod undocumented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod undocumented
Pod documented
Pod documented
79 0 ▶ 
Branch: 19 branches
BranchTrueFalseTotal
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 9 0 9
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 9 0 9
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 9 2 11
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 9 0 9
if (defined builtin::blessed($_[0])) { } 9 0 9
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 9 0 9
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 0 0 0
if (defined builtin::blessed($_[0])) { } 0 0 0
80 # Object method calls are fatal.
81 \$death->( \$_[0], "Function $func" );
82 0 }
83 else {
84 my \$class = shift \@_;
85 0 return \$class->SUPER::$func( \@_ );
86 0 }
87 }
88 PROXY_FOR_DEATH
89 57 eval $src; ## no critic eval
90 57 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Object::Deadly::EVAL_ERROR) 0 57 57
91 0 croak "$src\n$EVAL_ERROR";
92 }
93  
94 57 return 1;
95 }
96  
97 # A dictionary of stuff that can show up in UNIVERSAL.
98 3 our @UNIVERSAL_METHODS = (
99  
100 # core perl
101 qw( isa can VERSION ),
102  
103 # core perl 5.9.4+
104 'DOES',
105  
106 # UNIVERSAL.pm
107 'import',
108  
109 # UNIVERSAL/require.pm
110 qw( require use ),
111  
112 # UNIVERSAL/dump.pm
113 qw( blessed dump peek refaddr ),
114  
115 # UNIVERSAL/exports.pm
116 'exports',
117  
118 # UNIVERSAL/moniker.pm
119 qw( moniker plural_moniker ),
120  
121 # UNIVERSAL/which.pm
122 'which',
123  
124 # SUPER.pm
125 qw( super SUPER ),
126 );
127  
128 12 ▶sub kill_UNIVERSAL {
Subroutine kill_UNIVERSAL: called CC=2 SCAR=6.9
Pod documented
129  
130 # Public, overridable method call. Creates deadly functions in
131 # ${class}::_unsafe to mask all UNIVERSAL methods.
132  
133 12 my $class = shift @_;
134 12 for my $fqf_function (
135 @UNIVERSAL_METHODS,
136  
137 # Anything else we happen to find
138 Devel::Symdump->rnew('UNIVERSAL')->functions
139 )
140 {
141 225 my $function = $fqf_function;
142 225 $function =~ s/\AUNIVERSAL:://mx;
143  
144 225 $class->kill_function($function);
145 }
146  
147 12 return 1;
148 }
149  
150 60 ▶sub get_death {
Subroutine get_death: called CC=1 SCAR=0.0
Pod documented
151  
152 # Public, overridable method call. Returns the _death function
153 60 my $class = shift @_;
154  
155 3 ▶ no strict 'refs'; ## no critic Strict
Subroutine BEGIN: called CC=1 SCAR=0.0
156 60 return \&{"${class}::_unsafe::death"};
157 }
158  
159 # Compile and load our implementing classes.
160 3 ▶use Object::Deadly::_safe ();
Subroutine BEGIN: called CC=1 SCAR=0.0
161 3 ▶use Object::Deadly::_unsafe ();
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 ## no critic EndWithOne
164 3 'For the SAKE... of the FUTURE of ALL... mankind... I WILL have
165 a... SMALL sprite!';
166  
167 __END__
↑ Summary Deadly/_unsafe.pm »