blib/lib/Object/Destroyer.pm

filter: Statementstmt 86.8% 53 / 61 Branchbran 79.4% 27 / 34 Conditioncond 25.8% 8 / 31 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 91.6% 11 / 12 Podpod 75.0% 3 / 4 totaltotal 67.7% 105 / 155 CC 28 SCAR 32.1
CC
28
Cov
70%
__ANON__
90.0
new
9.0
AUTOLOAD
7.0
CRAP 24.8
↑ Summary
1 package Object::Destroyer;
2  
3 # See POD at end for details
4  
5 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 ##use Scalar::Util ();
9  
10 5 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 BEGIN {
12 5 ▶ $VERSION = '2.01';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14  
15 5 ▶if ( eval { require Scalar::Util } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Scalar::Util } }) { } 5 0 5
16 5 Scalar::Util->import('blessed');
17 } else {
18 *blessed = sub {
19 0 ▶ my $ref = ref($_[0]);
Subroutine __ANON__: not called CC=9 SCAR=45.0
20 0 ▶ return $ref
Branch: 1 branch
BranchTrueFalseTotal
if $ref and $ref ne "SCALAR" and $ref ne "ARRAY" and $ref ne "HASH" and $ref ne "CODE" and $ref ne "REF" and $ref ne "GLOB" and $ref ne "LVALUE" 0 0 0
Condition0%0/21
$ref and $ref ne "SCALAR" and $ref ne "ARRAY" and $ref ne "HASH" and $ref ne "CODE" and $ref ne "REF" and $ref ne "GLOB" and $ref ne "LVALUE"
!ll&&!rl&&r
000
$ref and $ref ne "SCALAR" and $ref ne "ARRAY" and $ref ne "HASH" and $ref ne "CODE" and $ref ne "REF" and $ref ne "GLOB"
!ll&&!rl&&r
000
$ref and $ref ne "SCALAR" and $ref ne "ARRAY" and $ref ne "HASH" and $ref ne "CODE" and $ref ne "REF"
!ll&&!rl&&r
000
$ref and $ref ne "SCALAR" and $ref ne "ARRAY" and $ref ne "HASH" and $ref ne "CODE"
!ll&&!rl&&r
000
$ref and $ref ne "SCALAR" and $ref ne "ARRAY" and $ref ne "HASH"
!ll&&!rl&&r
000
$ref and $ref ne "SCALAR" and $ref ne "ARRAY"
!ll&&!rl&&r
000
$ref and $ref ne "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/8
$ref and $ref ne "SCALAR" and $ref ne "ARRAY" and $ref ne "HASH" and $ref ne "CODE" and $ref ne "REF" and $ref ne "GLOB" and $ref ne "LVALUE"
$ref $ref ne "SCALAR" $ref ne "ARRAY" $ref ne "HASH" $ref ne "CODE" $ref ne "REF" $ref ne "GLOB" $ref ne "LVALUE"
Truth table0%0/9
A and B and C and D and E and F and G and H
A= $refB= $ref ne "SCALAR"C= $ref ne "ARRAY"D= $ref ne "HASH"E= $ref ne "CODE"F= $ref ne "REF"G= $ref ne "GLOB"H= $ref ne "LVALUE"
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
21 if $ref && ($ref ne 'SCALAR') && ($ref ne 'ARRAY') &&
22 ($ref ne 'HASH') && ($ref ne 'CODE') &&
23 ($ref ne 'REF') && ($ref ne 'GLOB') &&
24 ($ref ne 'LVALUE');
25 0 return;
26 }
27 0 }
28  
29 sub new {
30 47 ▶ if ( ref $_[0] ) {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 1 46 47
31 # This is a method called on an existing
32 # Destroyer, and should actually be passed through
33 # to the encased object via the AUTOLOAD
34 1 $Object::Destroyer::AUTOLOAD = '::new';
35 1 goto &AUTOLOAD;
36 }
37  
38 # *ahem*... where were we...
39 46 my $destroyer = shift;
40 46 ▶ my $ref = shift || '';
Condition50%1/2
shift() || ''
l!l
460
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
41 46 my $self = {};
42
43 46 ▶ if ( ref($ref) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref eq 'CODE') { } 24 22 46
elsif (my $class = &blessed($ref)) { } 21 1 22
44 ##
45 ## Object::Destroyer->new( sub {...} )
46 ##
47 24 $self->{code} = $ref;
48 } elsif ( my $class = blessed($ref) ) {
49 ##
50 ## Object::Destroyer->new( $object, 'optional_method' )
51 ##
52 21 ▶ my $method = shift || 'DESTROY';
Condition100%2/2
shift() || 'DESTROY'
l!l
156
MC/DC100%1/1
shift() || 'DESTROY'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
53 21 ▶ Carp::croak("Second argument to constructor must be a method name")
Branch: 1 branch
BranchTrueFalseTotal
if ref $method 1 20 21
54 if ref($method);
55 20 ▶ Carp::croak("Object::Destroyer requires that $class has a $method method")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($method) 2 18 20
56 unless $class->can($method);
57 18 $self->{object} = $ref;
58 18 $self->{method} = $method;
59 } else {
60 ##
61 ## And what is this?
62 ##
63 1 Carp::croak("You should pass an object or code reference to constructor");
64 }
65 42 ▶ Carp::croak("Extra arguments to constructor") if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 40 42
66  
67 40 return bless $self, $destroyer;
68 }
69  
70 # Hand off general method calls to the encased object.
71 # Rather than just doing a $self->{object}->$method(@_), which
72 # would leave us in the call stack, find the actual subroutine
73 # that will be executed, and goto that directly.
74 sub AUTOLOAD {
75 16 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=6 SCAR=19.5
76
77 16 my ($method) = $Object::Destroyer::AUTOLOAD =~ /^.*::(.*)$/;
78 16 ▶ if (my $object = $self->{object}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $object = $self->{"object"}) 16 0 16
79 16 ▶ if (my $function = $object->can($method)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $function = $object->can($method)) { } 10 6 16
elsif ($object->can('AUTOLOAD')) { } 6 0 6
80 ##
81 ## Rearrange stack - instead of
82 ## $object_destroy->method(@params)
83 ## make it look like
84 ## $underlying_object->method(@params)
85 ##
86 10 unshift @_, $object;
87 10 goto &$function;
88 } elsif ( $object->can("AUTOLOAD") ) {
89 ##
90 ## We can't just goto to AUTOLOAD method in unknown
91 ## package (it may be in base class of $object).
92 ## We have to preserve the method's name.
93 ##
94 6 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 3 3 6
elsif (defined wantarray) { } 3 0 3
95 ## List context
96 3 return $object->$method(@_);
97 } elsif ( defined wantarray ) {
98 ## Scalar context
99 3 return scalar $object->$method(@_);
100 } else {
101 ## Void context
102 0 $object->$method(@_);
103 }
104 } else {
105 ##
106 ## Probably this is a caller's error
107 ##
108 0 my $package = ref $self->{object};
109 0 Carp::croak(qq[Can't locate object method "$method" via package "$package"]);
110 }
111 }
112
113 ##
114 ## No object at all. Either we have a $coderef instead of object
115 ## or DESTROY has been called already.
116 ##
117 0 Carp::croak("Can't locate object to call method '$method'");
118 }
119  
120 sub dismiss{
121 1 ▶ $_[0]->{dismissed} = 1;
Subroutine dismiss: called CC=1 SCAR=0.0
Pod documented
122 }
123  
124 ##
125 ## Use our automatically triggered DESTROY to call the
126 ## non-automatically triggered clean-up method of the encased object
127 ##
128 sub DESTROY {
129 42 ▶ my $self = shift;
Subroutine DESTROY: called CC=4 SCAR=13.9
130  
131 42 ▶ if ( $self->{dismissed} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'dismissed'}) { } 1 41 42
elsif ($self->{'code'}) { } 23 18 41
elsif (my $object = $self->{'object'}) { } 16 2 18
132 ## do nothing
133 } elsif ( $self->{code} ) {
134 23 $self->{code}->();
135 } elsif ( my $object = $self->{object} ) {
136 16 my $method = $self->{method};
137 16 $object->$method();
138 }
139  
140 42 %$self = ();
141 }
142  
143 ##
144 ## Catch a couple of specific cases that would be handled by UNIVERSAL
145 ## before our AUTOLOAD got a chance to dispatch it.
146 ##
147 ## We are both 'Object::Destroyer' (or it's derived class)
148 ## and underlying object's class
149 ##
150 sub isa {
151 4 ▶ my $self = shift;
Subroutine isa: called CC=3 SCAR=11.0
Pod documented
152 4 my $class = shift;
153  
154 return $class eq __PACKAGE__ ||
155 4 ▶ ($self->{object} && $self->{object}->isa($class));
Condition83%5/6
$class eq __PACKAGE__ || $self->{'object'} && $self->{'object'}->isa($class)
l!l&&r!l&&!r
121
$self->{"object"} and $self->{"object"}->isa($class)
!ll&&!rl&&r
012
MC/DC66%2/3
$class eq __PACKAGE__ || $self->{'object'} && $self->{'object'}->isa($class)
$class eq __PACKAGE__ $self->{"object"} $self->{"object"}->isa($class)
Truth table75%3/4
A || B and C
A= $class eq __PACKAGE__B= $self->{"object"}C= $self->{"object"}->isa($class)
ABCresult
1XX1
0111
00X0
0100
156 }
157  
158 sub can {
159 6 ▶ my $self = shift;
Subroutine can: called CC=2 SCAR=7.2
Pod undocumented
160 6 ▶ return $self->{object}->can(@_) if $self->{object};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"object"} 6 0 6
161 }
162  
163 5 1;
164  
165 __END__
↑ Summary