| 1 | ## no critic (Version,PodSections,Warnings,Rcs) | ||||||||||
| 2 | package Object::Deadly::_unsafe; | ||||||||||
| 3 | |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use overload (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | my $death = Object::Deadly->get_death; | |||||||||
| 8 | overload->import( | ||||||||||
| 9 | map { | ||||||||||
| 10 | 225 | my $bad_operation = $_; | |||||||||
| 11 | |||||||||||
| 12 | # returns a pair. | ||||||||||
| 13 | 72 | ▶ | $bad_operation => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | ## no critic Local | ||||||||||
| 16 | 72 | local *__ANON__ = __PACKAGE__ . "::$bad_operation"; | |||||||||
| 17 | 72 | $death->( $_[0], "Overloaded $bad_operation" ); | |||||||||
| 18 | } | ||||||||||
| 19 | 225 | } | |||||||||
| 20 | 3 | map { split ' ' } ## no critic EmptyQuotes | |||||||||
| 21 | values %overload::ops ## no critic PackageVars | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | # Kill off all UNIVERSAL things and try it at several points during | ||||||||||
| 25 | # execution just in case someone added something along the way. | ||||||||||
| 26 | 3 | ▶ | use Object::Deadly (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 3 | Object::Deadly->kill_UNIVERSAL; | |||||||||
| 28 | |||||||||||
| 29 | # Eval CHECK and INIT blocks into existance but only if we haven't | ||||||||||
| 30 | # reached the main program yet. This is just to avoid the warning. | ||||||||||
| 31 | 3 | ▶ | use B (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 3 | ▶ | use English '$EVAL_ERROR'; ## no critic | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | 3 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.1
| |||||||||||
| 35 | 3 | ▶ | if ( not ${ B::main_start() } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 3 | eval <<"CODE"; ## no critic | |||||||||
| 37 | 3 | ▶ | #line @{[__LINE__]} "@{[__FILE__]}" | ||||||||
|
Subroutine
CHECK: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 3 | ▶ | CHECK { Object::Deadly->kill_UNIVERSAL; } | ||||||||
|
Subroutine
INIT: called CC=1 SCAR=0.0
| |||||||||||
| 39 | INIT { Object::Deadly->kill_UNIVERSAL; } | ||||||||||
| 40 | CODE | ||||||||||
| 41 | 3 | ▶ | croak $EVAL_ERROR if $EVAL_ERROR; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | } | ||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 3 | ▶ | END { Object::Deadly->kill_UNIVERSAL; } | ||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | 3 | Object::Deadly->kill_function('AUTOLOAD'); | |||||||||
| 48 | |||||||||||
| 49 | 3 | ▶ | use vars '%SIMPLE_OBJECTS'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | |||||||||||
| 51 | # DESTROY is the only legal method for these objects. It has to be. | ||||||||||
| 52 | 27 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 27 | delete $Object::Deadly::SIMPLE_OBJECTS{ Object::Deadly::refaddr $_[0] }; | |||||||||
| 54 | 27 | return; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 126 | ▶ | sub death { ## no critic RequireFinalReturn | ||||||||
|
Subroutine
death: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 58 | # The common death | ||||||||||
| 59 | 126 | my ( $self, $bad_operation ) = @_; | |||||||||
| 60 | |||||||||||
| 61 | 126 | my $unsafe_implementation_class = Object::Deadly::blessed $self; | |||||||||
| 62 | 126 | my $addr = Object::Deadly::refaddr $self; | |||||||||
| 63 | 126 | my $name = sprintf '%s=(0x%07x)', $unsafe_implementation_class, $addr; | |||||||||
| 64 | 126 | my $message; | |||||||||
| 65 | 126 | ▶ | if ( exists $SIMPLE_OBJECTS{$addr} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | # Fetch the message in the object by switching the object into | ||||||||||
| 68 | # something that's safe. | ||||||||||
| 69 | 126 | my $safe_implementation_class = $unsafe_implementation_class; | |||||||||
| 70 | 126 | $safe_implementation_class =~ s/\::_unsafe\z/::_safe/mx; | |||||||||
| 71 | |||||||||||
| 72 | 126 | bless $self, $safe_implementation_class; | |||||||||
| 73 | 126 | $message = $$self; ## no critic DoubleSigils | |||||||||
| 74 | 126 | bless $self, $unsafe_implementation_class; | |||||||||
| 75 | |||||||||||
| 76 | 126 | Object::Deadly::confess | |||||||||
| 77 | "Attempt to call $bad_operation on $name: $message"; | ||||||||||
| 78 | } | ||||||||||
| 79 | else { | ||||||||||
| 80 | 0 | Object::Deadly::confess "Attempt to call $bad_operation on $name"; | |||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 3 | 1; | |||||||||
| 85 | |||||||||||
| 86 | __END__ | ||||||||||