Aspect/Advice/After.pm

filter: Statementstmt 84.2% 400 / 475 Branchbran 68.7% 191 / 278 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 78.8% 604 / 766 SCAR 28.5
CC
15
Cov
78%
_install
9.9
__ANON__
7.6
BEGIN
1.0
CRAP 17.2
« Aspect/Advice/Before.pm ↑ Summary Aspect/Point.pm »
1 package Aspect::Advice::After;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Added by eilara as hack around caller() core dump
7 # NOTE: Now we've switched to Sub::Uplevel can this be removed? --ADAMK
8 21 ▶use Carp::Heavy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Sub::Uplevel ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use Aspect::Hook ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use Aspect::Advice ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use Aspect::Point::After ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 21 our $VERSION = '0.97_06';
16 21 our @ISA = 'Aspect::Advice';
17  
18 # NOTE: To simplify debugging of the generated code, all injected string
19 # fragments will be defined in $UPPERCASE, and all lexical variables to be
20 # accessed via the closure will be in $lowercase.
21 sub _install {
22 58 ▶ my $self = shift;
Subroutine _install: called CC=9 SCAR=22.9
23 58 my $pointcut = $self->pointcut;
24 58 my $code = $self->code;
25 58 my $lexical = $self->lexical;
26  
27 # Get the curried version of the pointcut we will use for the
28 # runtime checks instead of the original.
29 # Because $MATCH_RUN is used in boolean conditionals, if there
30 # is nothing to do the compiler will optimise away the code entirely.
31 58 my $curried = $pointcut->curry_runtime;
32 58 ▶ my $compiled = $curried ? $curried->compiled_runtime : undef;
Branch: 1 branch
BranchTrueFalseTotal
$curried ? : 42 16 58
33 58 ▶ my $MATCH_RUN = $compiled ? '$compiled->()' : 1;
Branch: 1 branch
BranchTrueFalseTotal
$compiled ? : 42 16 58
34  
35 # When an aspect falls out of scope, we don't attempt to remove
36 # the generated hook code, because it might (for reasons potentially
37 # outside our control) have been recursively hooked several times
38 # by both Aspect and other modules.
39 # Instead, we store an "out of scope" flag that is used to shortcut
40 # past the hook as quickely as possible.
41 # This flag is shared between all the generated hooks for each
42 # installed Aspect.
43 # If the advice is going to last lexical then we don't need to
44 # check or use the $out_of_scope variable.
45 58 my $out_of_scope = undef;
46 58 ▶ my $MATCH_DISABLED = $lexical ? '$out_of_scope' : '0';
Branch: 1 branch
BranchTrueFalseTotal
$lexical ? : 40 18 58
47  
48 # Find all pointcuts that are statically matched
49 # wrap the method with advice code and install the wrapper
50 58 foreach my $name ( $pointcut->match_all ) {
51 71 my $NAME = $name; # For completeness
52  
53 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 71 my $original = *$name{CODE};
55 71 ▶ unless ( $original ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($original) 0 71 71
56 0 Carp::croak("Can't wrap non-existent subroutine ", $name);
57 }
58  
59 # Any way to set prototypes other than eval?
60 71 my $PROTOTYPE = prototype($original);
61 71 ▶ $PROTOTYPE = defined($PROTOTYPE) ? "($PROTOTYPE)" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $PROTOTYPE ? : 3 68 71
62  
63 # Generate the new function
64 21 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 71 ▶ eval <<"END_PERL"; die $@ if $@;
Subroutine __ANON__: called CC=7 SCAR=20.3
Branch: 133 branches
BranchTrueFalseTotal
if $@ 0 71 71
if ($wantarray) 6 10 16
unless (&$compiled()) 2 6 8
unless $_->{"exception"} 1 1 2
if $exception 1 1 2
if (defined $wantarray) 1 3 4
unless (&$compiled()) 3 3 6
unless $_->{"exception"} 1 2 3
if $exception 1 1 2
unless (&$compiled()) 2 2 4
unless $_->{"exception"} 1 2 3
if $exception 0 0 0
if ($wantarray) 2 3 5
unless (&$compiled()) 4 5 9
unless $_->{"exception"} 1 4 5
if $exception 1 1 2
if (defined $wantarray) 1 3 4
unless (&$compiled()) 1 1 2
unless $_->{"exception"} 2 2 4
if $exception 1 3 4
unless (&$compiled()) 2 0 2
unless $_->{"exception"} 2 3 5
if $exception 0 2 2
if ($wantarray) 1 3 4
unless (&$compiled()) 1 2 3
unless $_->{"exception"} 3 4 7
if $exception 1 3 4
if (defined $wantarray) 1 3 4
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 1 1 2
if $exception 5 2 7
unless (&$compiled()) 4 3 7
unless $_->{"exception"} 2 2 4
if $exception 0 1 1
if ($wantarray) 1 3 4
unless (&$compiled()) 1 2 3
unless $_->{"exception"} 0 1 1
if $exception 3 3 6
if (defined $wantarray) 1 5 6
unless (&$compiled()) 2 0 2
unless $_->{"exception"} 0 2 2
if $exception 1 0 1
unless (&$compiled()) 9 8 17
unless $_->{"exception"} 2 7 9
if $exception 1 2 3
if ($wantarray) 3 6 9
unless (&$compiled()) 3 1 4
unless $_->{"exception"} 1 3 4
if $exception 0 2 2
if (defined $wantarray) 1 5 6
unless (&$compiled()) 0 2 2
unless $_->{"exception"} 1 0 1
if $exception 1 1 2
unless (&$compiled()) 2 1 3
unless $_->{"exception"} 1 3 4
if $exception 1 2 3
if ($wantarray) 1 3 4
unless (&$compiled()) 2 1 3
unless $_->{"exception"} 1 1 2
if $exception 0 0 0
if (defined $wantarray) 4 5 9
unless (&$compiled()) 2 3 5
unless $_->{"exception"} 1 3 4
if $exception 0 0 0
unless (&$compiled()) 1 2 3
unless $_->{"exception"} 1 1 2
if $exception 1 2 3
if ($wantarray) 1 3 4
unless (&$compiled()) 1 1 2
unless $_->{"exception"} 1 2 3
if $exception 0 0 0
if (defined $wantarray) 1 2 3
unless (&$compiled()) 1 1 2
unless $_->{"exception"} 1 0 1
if $exception 0 0 0
unless (&$compiled()) 0 1 1
unless $_->{"exception"} 1 0 1
if $exception 0 2 2
if ($wantarray) 1 2 3
unless (&$compiled()) 0 2 2
unless $_->{"exception"} 0 0 0
if $exception 0 1 1
if (defined $wantarray) 1 1 2
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 1 0 1
if $exception 0 0 0
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 1 0 1
if $exception 0 0 0
if ($wantarray) 1 2 3
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 0 1 1
if $exception 0 0 0
if (defined $wantarray) 1 1 2
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 0 1 1
if $exception 0 0 0
unless (&$compiled()) 0 1 1
unless $_->{"exception"} 0 0 0
if $exception 1 0 1
if ($wantarray) 1 2 3
unless (&$compiled()) 0 1 1
unless $_->{"exception"} 0 0 0
if $exception 0 1 1
if (defined $wantarray) 1 1 2
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 1 0 1
if $exception 0 0 0
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 1 0 1
if $exception 0 0 0
if ($wantarray) 1 2 3
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 0 1 1
if $exception 0 0 0
if (defined $wantarray) 1 1 2
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 0 1 1
if $exception 0 0 0
unless (&$compiled()) 0 1 1
unless $_->{"exception"} 0 0 0
if $exception 1 0 1
if ($wantarray) 1 2 3
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 1 0 1
if $exception 0 0 0
if (defined $wantarray) 1 1 2
unless (&$compiled()) 0 1 1
unless $_->{"exception"} 0 0 0
if $exception 0 1 1
unless (&$compiled()) 1 0 1
unless $_->{"exception"} 1 0 1
if $exception 0 0 0
66 package Aspect::Hook;
67  
68 *$NAME = sub $PROTOTYPE {
69 # Is this a lexically scoped hook that has finished
70 goto &\$original if $MATCH_DISABLED;
71  
72 my \$wantarray = wantarray;
73 if ( \$wantarray ) {
74 my \$return = eval { [
75 Sub::Uplevel::uplevel(
76 2, \$original, \@_,
77 )
78 ] };
79  
80 local \$_ = bless {
81 sub_name => \$name,
82 wantarray => \$wantarray,
83 args => \\\@_,
84 return_value => \$return,
85 exception => \$\@,
86 pointcut => \$pointcut,
87 original => \$original,
88 }, 'Aspect::Point::After';
89 unless ( $MATCH_RUN ) {
90 return \@\$return unless \$_->{exception};
91 die \$_->{exception};
92 }
93  
94 # Execute the advice code
95 &\$code(\$_);
96  
97 # Throw the same (or modified) exception
98 my \$exception = \$_->{exception};
99 die \$exception if \$exception;
100  
101 # Get the (potentially) modified return value
102 return \@{\$_->{return_value}};
103 }
104  
105 if ( defined \$wantarray ) {
106 my \$return = eval {
107 Sub::Uplevel::uplevel(
108 2, \$original, \@_,
109 )
110 };
111  
112 local \$_ = bless {
113 sub_name => \$name,
114 wantarray => \$wantarray,
115 args => \\\@_,
116 return_value => \$return,
117 exception => \$\@,
118 pointcut => \$pointcut,
119 original => \$original,
120 }, 'Aspect::Point::After';
121  
122 unless ( $MATCH_RUN ) {
123 return \$return unless \$_->{exception};
124 die \$_->{exception};
125 }
126  
127 # Execute the advice code
128 &\$code(\$_);
129  
130 # Throw the same (or modified) exception
131 my \$exception = \$_->{exception};
132 die \$exception if \$exception;
133  
134 # Return the potentially-modified value
135 return \$_->{return_value};
136  
137 }
138  
139 eval {
140 Sub::Uplevel::uplevel(
141 2, \$original, \@_,
142 )
143 };
144  
145 local \$_ = bless {
146 sub_name => \$name,
147 wantarray => \$wantarray,
148 args => \\\@_,
149 return_value => undef,
150 exception => \$\@,
151 pointcut => \$pointcut,
152 original => \$original,
153 }, 'Aspect::Point::After';
154 unless ( $MATCH_RUN ) {
155 return unless \$_->{exception};
156 die \$_->{exception};
157 }
158  
159 # Execute the advice code
160 &\$code(\$_);
161  
162 # Throw the same (or modified) exception
163 my \$exception = \$_->{exception};
164 die \$exception if \$exception;
165  
166 return;
167 };
168 END_PERL
169 71 $self->{installed}++;
170 }
171  
172 # If this will run lexical we don't need a descoping hook
173 58 ▶ return unless $lexical;
Branch: 1 branch
BranchTrueFalseTotal
unless $lexical 18 40 58
174  
175 # Return the lexical descoping hook.
176 # This MUST be stored and run at DESTROY-time by the
177 # parent object calling _install. This is less bullet-proof
178 # than the DESTROY-time self-executing blessed coderef
179 40 ▶ return sub { $out_of_scope = 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
180 }
181  
182 21 1;
183  
184 __END__
« Aspect/Advice/Before.pm ↑ Summary Aspect/Point.pm »