blib/lib/Sub/Attributes.pm

filter: Statementstmt 79.0% 64 / 81 Branchbran 83.3% 10 / 12 Conditioncond 50.0% 8 / 16 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 82.6% 19 / 23 Podpod 100% 1 / 1 totaltotal 73.4% 105 / 143 CC 13 SCAR 8.5
CC
13
Cov
75%
MODIFY_CODE_ATTRIBUTES
5.1
__ANON__
3.0
__ANON__
3.0
CRAP 2.3
↑ Summary
1 package Sub::Attributes;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # voodoo
6 1 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no warnings qw(reserved redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use B 'svref_2object';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶BEGIN { our $VERSION = 0.04 }
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # these data structures are key to this module. They're created in a BEGIN block
15 # as package variables so they're available when MODIFY_CODE_ATTRIBUTES is
16 # called:
17 #
18 # %attributes is a hash of subroutine names and their attributes
19 # %allowed is a hash of recognized subroutine attributes with a coderef for
20 # the behavior
21 #
22 # You can relace/extend %allowed in an inherited class to provide your own behavior!
23 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.4
24 our %allowed = (
25 # runtime check that caller is the package
26 1 ▶ Private => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 my ($package) = @_;
28 2 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
29 2 my ($coderef, @args) = @_;
30 2 my ($package_caller, $filename, $line, $sub) = caller(2);
31 2 ▶ croak 'Only the object may call this sub' unless $sub && $sub =~ /^$package\:\:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub and $sub =~ /^$package\:\:/ 1 1 2
Condition66%2/3
$sub and $sub =~ /^$package\:\:/
!ll&&!rl&&r
011
MC/DC50%1/2
$sub and $sub =~ /^$package\:\:/
$sub $sub =~ /^$package\:\:/
Truth table66%2/3
A and B
A= $subB= $sub =~ /^$package\:\:/
ABresult
0X0
100
111
32 1 $coderef->(@args);
33 1 };
34 },
35 # runtime check that the first arg is the package
36 1 ▶ ClassMethod => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 11 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
38 11 my ($coderef, @args) = @_;
39 croak 'Class method called as function / object method'
40 11 ▶ unless $args[0] && exists $Sub::Attributes::attributes{ $args[0] };
Branch: 1 branch
BranchTrueFalseTotal
unless $args[0] and exists $Sub::Attributes::attributes{$args[0]} 1 10 11
Condition66%2/3
$args[0] and exists $Sub::Attributes::attributes{$args[0]}
!ll&&!rl&&r
0110
MC/DC50%1/2
$args[0] and exists $Sub::Attributes::attributes{$args[0]}
$args[0] exists $Sub::Attributes::attributes{$args[0]}
Truth table66%2/3
A and B
A= $args[0]B= exists $Sub::Attributes::attributes{$args[0]}
ABresult
0X0
100
111
41 10 $coderef->(@args);
42 1 };
43 },
44 # runtime check that the first arg is the object
45 3 ▶ Method => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 7 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
47 7 my ($coderef, @args) = @_;
48 croak 'Method called as function'
49 7 ▶ unless $args[0] && exists $Sub::Attributes::attributes{ ref $args[0] };
Branch: 1 branch
BranchTrueFalseTotal
unless $args[0] and exists $Sub::Attributes::attributes{ref $args[0]} 1 6 7
Condition66%2/3
$args[0] and exists $Sub::Attributes::attributes{ref $args[0]}
!ll&&!rl&&r
016
MC/DC50%1/2
$args[0] and exists $Sub::Attributes::attributes{ref $args[0]}
$args[0] exists $Sub::Attributes::attributes{ref $args[0]}
Truth table66%2/3
A and B
A= $args[0]B= exists $Sub::Attributes::attributes{ref $args[0]}
ABresult
0X0
100
111
50 6 $coderef->(@args);
51 3 };
52 },
53 # compile time override, run a coderef before running the subroutine
54 0 ▶ Before => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 my ($package, $value, $coderef) = @_;
56  
57 # full name of the sub to override
58 0 my $fq_sub = "$package:\:$value";
59  
60 0 my $target_coderef = \&{$fq_sub};
61 0 ▶ *{$fq_sub} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 $coderef->(@_);
63 0 $target_coderef->(@_);
64 0 };
65  
66 # we didn't change the method with the attribute
67 # so we return undef as we have no runtime changes
68 0 return undef;
69 },
70 # compile time override, run a coderef after running the subroutine
71 1 ▶ After => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 my ($package, $value, $coderef) = @_;
73  
74 # full name of the sub to override
75 1 my $fq_sub = "$package:\:$value";
76  
77 1 my $target_coderef = \&{$fq_sub};
78 1 ▶ *{$fq_sub} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
79 6 my @rv = $target_coderef->(@_);
80 5 $coderef->(@_);
81 5 ▶ return wantarray ? @rv : $rv[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 5 5
82 1 };
83  
84 # we didn't change the method with the attribute
85 # so we return undef as we have no runtime changes
86 1 return undef;
87 },
88 # compile time override, run a coderef around running the subroutine
89 0 ▶ Around => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 my ($package, $value, $coderef) = @_;
91  
92 # full name of the sub to override
93 0 my $fq_sub = "$package:\:\$value";
94  
95 0 my $target_coderef = \&{$fq_sub};
96 0 ▶ *{$fq_sub} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 $coderef->($target_coderef, @_);
98 0 };
99  
100 # we didn't change the method with the attribute
101 # so we return undef as we have no runtime changes
102 0 return undef;
103 },
104 1 );
105 }
106  
107 # this is the registrar for subroutine attributes called at compile time
108 5 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=5 SCAR=16.3
109 5 my ($package, $coderef, @attributes, @disallowed) = @_;
110  
111 5 my $obj = svref_2object($coderef);
112 5 my $subroutine = $obj->GV->NAME;
113  
114 5 for my $attribute (@attributes) {
115 # parse the attribute into name and value
116 6 my ($name, $value) = $attribute =~ qr/^ (\w+) (?:\((\S+?)\))? $/x;
117 6 my $overrider = $Sub::Attributes::allowed{$name};
118  
119 # attribute not known, compile error
120 6 ▶ push(@disallowed, $name) && next unless $overrider;
Branch: 1 branch
BranchTrueFalseTotal
unless $overrider 0 6 6
Condition25%1/4
$overrider || push(@disallowed, $name) && (next)
l!l
60
push @disallowed, $name and next
!ll
00
MC/DC0%0/2
$overrider || push(@disallowed, $name) && (next)
$overrider push @disallowed, $name
Truth table33%1/3
A || B
A= $overriderB= push @disallowed, $name
ABresult
1X1
011
000
121  
122 # make compile time changes, skip ahead if no runtime changes
123 6 my $override_coderef = $overrider->($package, $value, $coderef);
124 6 ▶ next unless $override_coderef;
Branch: 1 branch
BranchTrueFalseTotal
unless $override_coderef 1 5 6
125  
126 # override subroutine with attribute coderef
127 5 my $old_coderef = $coderef;
128 5 ▶ $coderef = sub { $override_coderef->($old_coderef, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
129 5 *{"$package:\:$subroutine"} = $coderef;
130 }
131  
132 5 $Sub::Attributes::attributes{$package}{ $subroutine } = \@attributes;
133 5 return @disallowed;
134 };
135  
136 1 ▶sub sub_attributes {
Subroutine sub_attributes: called CC=2 SCAR=7.6
Pod documented
137 1 my ($package) = @_;
138 1 ▶ my $package_name = ref $package || $package;
Condition33%1/3
ref $package || $package
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $package || $package
ref $package $package
Truth table33%1/3
A || B
A= ref $packageB= $package
ABresult
1X1
011
000
139 1 return $Sub::Attributes::attributes{ $package_name };
140 }
141 1 1;
142 __END__
↑ Summary