Aspect/Advice/Before.pm

filter: Statementstmt 91.2% 83 / 91 Branchbran 85.0% 34 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 90.2% 130 / 144 SCAR 29.1
CC
18
Cov
89%
_install
9.1
__ANON__
7.1
_validate
5.0
CRAP 18.4
« Aspect/Pointcut.pm ↑ Summary Aspect/Advice/After.pm »
1 package Aspect::Advice::Before;
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 Aspect::Hook ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use Aspect::Advice ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use Aspect::Point::Before ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 21 our $VERSION = '0.97_06';
15 21 our @ISA = 'Aspect::Advice';
16  
17 sub _install {
18 30 ▶ my $self = shift;
Subroutine _install: called CC=9 SCAR=22.0
19 30 my $pointcut = $self->pointcut;
20 30 my $code = $self->code;
21 30 my $lexical = $self->lexical;
22  
23 # Get the curried version of the pointcut we will use for the
24 # runtime checks instead of the original.
25 # Because $MATCH_RUN is used in boolean conditionals, if there
26 # is nothing to do the compiler will optimise away the code entirely.
27 30 my $curried = $pointcut->curry_runtime;
28 30 ▶ my $compiled = $curried ? $curried->compiled_runtime : undef;
Branch: 1 branch
BranchTrueFalseTotal
$curried ? : 11 19 30
29 30 ▶ my $MATCH_RUN = $compiled ? '$compiled->()' : 1;
Branch: 1 branch
BranchTrueFalseTotal
$compiled ? : 11 19 30
30  
31 # When an aspect falls out of scope, we don't attempt to remove
32 # the generated hook code, because it might (for reasons potentially
33 # outside our control) have been recursively hooked several times
34 # by both Aspect and other modules.
35 # Instead, we store an "out of scope" flag that is used to shortcut
36 # past the hook as quickely as possible.
37 # This flag is shared between all the generated hooks for each
38 # installed Aspect.
39 # If the advice is going to last lexical then we don't need to
40 # check or use the $out_of_scope variable.
41 30 my $out_of_scope = undef;
42 30 ▶ my $MATCH_DISABLED = $lexical ? '$out_of_scope' : '0';
Branch: 1 branch
BranchTrueFalseTotal
$lexical ? : 16 14 30
43  
44 # Find all pointcuts that are statically matched
45 # wrap the method with advice code and install the wrapper
46 30 foreach my $name ( $pointcut->match_all ) {
47 31 my $NAME = $name; # For completeness
48  
49 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 31 my $original = *$name{CODE};
51 31 ▶ unless ( $original ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($original) 0 31 31
52 0 Carp::croak("Can't wrap non-existent subroutine ", $name);
53 }
54  
55 # Any way to set prototypes other than eval?
56 31 my $PROTOTYPE = prototype($original);
57 31 ▶ $PROTOTYPE = defined($PROTOTYPE) ? "($PROTOTYPE)" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $PROTOTYPE ? : 1 30 31
58  
59 # Generate the new function
60 21 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 31 ▶ eval <<"END_PERL"; die $@ if $@;
Subroutine __ANON__: called CC=7 SCAR=19.6
Branch: 11 branches
BranchTrueFalseTotal
if $@ 0 31 31
$wantarray ? : 5 18 23
unless &$compiled() 6 14 20
if ($wantarray) 2 7 9
if ($_->{"proceed"}) 3 6 9
unless $_->{"proceed"} 2 5 7
$wantarray ? : 2 6 8
unless &$compiled() 2 5 7
if ($wantarray) 2 4 6
if ($_->{"proceed"}) 0 2 2
unless $_->{"proceed"} 2 3 5
62 package Aspect::Hook;
63  
64 *$NAME = sub $PROTOTYPE {
65 # Is this a lexically scoped hook that has finished
66 goto &\$original if $MATCH_DISABLED;
67  
68 # Apply any runtime-specific context checks
69 my \$wantarray = wantarray;
70 local \$_ = bless {
71 sub_name => \$name,
72 wantarray => \$wantarray,
73 args => \\\@_,
74 pointcut => \$pointcut,
75 return_value => \$wantarray ? [ ] : undef,
76 original => \$original,
77 proceed => 1,
78 }, 'Aspect::Point::Before';
79  
80 goto &\$original unless $MATCH_RUN;
81  
82 # Array context needs some special return handling
83 if ( \$wantarray ) {
84 # Run the advice code
85 &\$code(\$_);
86  
87 if ( \$_->{proceed} ) {
88 \@_ = \$_->args; ### Superfluous?
89 goto &\$original;
90 }
91  
92 # Don't run the original
93 return \@{\$_->{return_value}};
94 }
95  
96 # Scalar and void have the same return handling.
97 &\$code(\$_);
98  
99 # Do they want to shortcut?
100 return \$_->{return_value} unless \$_->{proceed};
101  
102 # Continue onwards to the original function
103 \@_ = \$_->args; ### Superfluous?
104 goto &\$original;
105 };
106 END_PERL
107 31 $self->{installed}++;
108 }
109  
110 # If this will run lexical we don't need a descoping hook
111 30 ▶ return unless $lexical;
Branch: 1 branch
BranchTrueFalseTotal
unless $lexical 14 16 30
112  
113 # Return the lexical descoping hook.
114 # This MUST be stored and run at DESTROY-time by the
115 # parent object calling _install. This is less bullet-proof
116 # than the DESTROY-time self-executing blessed coderef
117 16 ▶ return sub { $out_of_scope = 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
118 }
119  
120 # Check for pointcut usage not supported by the advice type
121 sub _validate {
122 30 ▶ my $self = shift;
Subroutine _validate: called CC=4 SCAR=16.1
123 30 my $pointcut = $self->pointcut;
124  
125 # The method used by the Highest pointcut is incompatible
126 # with the goto optimisation used by the before() advice.
127 30 ▶ if ( $pointcut->match_contains('Aspect::Pointcut::Highest') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointcut->match_contains("Aspect::Pointcut::Highest")) 0 30 30
128 0 return 'The pointcut highest is not currently supported by before advice';
129 }
130  
131 # Pointcuts using "throwing" are irrelevant in before advice
132 30 ▶ if ( $pointcut->match_contains('Aspect::Pointcut::Throwing') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointcut->match_contains("Aspect::Pointcut::Throwing")) 0 30 30
133 0 return 'The pointcut throwing is illegal when used by before advice';
134 }
135  
136 # Pointcuts using "throwing" are irrelevant in before advice
137 30 ▶ if ( $pointcut->match_contains('Aspect::Pointcut::Returning') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pointcut->match_contains("Aspect::Pointcut::Returning")) 0 30 30
138 0 return 'The pointcut returning is illegal when used by before advice';
139 }
140  
141 30 $self->SUPER::_validate(@_);
142 }
143  
144 21 1;
145  
146 __END__
« Aspect/Pointcut.pm ↑ Summary Aspect/Advice/After.pm »