Kavorka.pm

filter: Statementstmt 99.2% 132 / 133 Branchbran 82.3% 28 / 34 Conditioncond 61.1% 11 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 33 / 33 Podpod 0.0% 0 / 3 totaltotal 89.6% 208 / 232 CC 62 SCAR 17.6
CC
62
Cov
92%
_exporter_fail
9.1
__ANON__
8.0
after
8.0
CRAP 5.8
« Parse/KeywordX.pm ↑ Summary Kavorka/MethodModifier.pm »
1 34 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
3 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4 34 ▶no warnings 'void';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
5  
6 34 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
7 34 ▶use Exporter::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 34 ▶use PadWalker ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Parse::Keyword ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Sub::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 package Kavorka;
15  
16 38 our $AUTHORITY = 'cpan:TOBYINK';
17 38 our $VERSION = '0.039';
18  
19 38 our @ISA = qw( Exporter::Tiny );
20 38 our @EXPORT = qw( fun method );
21 38 our @EXPORT_OK = qw( fun method after around before override augment classmethod objectmethod multi );
22 38 our %EXPORT_TAGS = (
23 modifiers => [qw( after around before )],
24 allmodifiers => [qw( after around before override augment )],
25 );
26  
27 38 our %IMPLEMENTATION = (
28 after => 'Kavorka::Sub::After',
29 around => 'Kavorka::Sub::Around',
30 augment => 'Kavorka::Sub::Augment',
31 before => 'Kavorka::Sub::Before',
32 classmethod => 'Kavorka::Sub::ClassMethod',
33 f => 'Kavorka::Sub::Fun',
34 fun => 'Kavorka::Sub::Fun',
35 func => 'Kavorka::Sub::Fun',
36 function => 'Kavorka::Sub::Fun',
37 method => 'Kavorka::Sub::Method',
38 multi => 'Kavorka::Multi',
39 objectmethod => 'Kavorka::Sub::ObjectMethod',
40 override => 'Kavorka::Sub::Override',
41 );
42  
43 38 our %INFO;
44  
45 sub info
46 16 ▶{
Subroutine info: called CC=1 SCAR=0.0
Pod undocumented
47 16 my $me = shift;
48 16 my $code = $_[0];
49 16 $INFO{$code};
50 }
51  
52 sub guess_implementation
53 297 ▶{
Subroutine guess_implementation: called CC=1 SCAR=0.0
Pod undocumented
54 297 my $me = shift;
55 297 $IMPLEMENTATION{$_[0]};
56 }
57  
58 sub compose_implementation
59 1 ▶{
Subroutine compose_implementation: called CC=1 SCAR=0.0
Pod undocumented
60 1 shift;
61 1 require Moo::Role;
62 1 Moo::Role->create_class_with_roles(@_);
63 }
64  
65 sub _exporter_validate_opts
66 81 ▶{
Subroutine _exporter_validate_opts: called CC=2 SCAR=7.1
67 81 my $class = shift;
68 81 $^H{'Kavorka/package'} = $_[0]{into};
69 81 ▶ $_[0]{replace} = 1 unless exists $_[0]{replace};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{"replace"} 81 0 81
70 }
71  
72 sub _fqname ($;$)
73 164 ▶{
Subroutine _fqname: called CC=4 SCAR=14.0
74 164 my $name = shift;
75 164 my ($package, $subname);
76
77 164 $name =~ s{'}{::}g;
78
79 164 ▶ if ($name =~ /::/)
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /::/u) { } 5 159 164
80 {
81 5 ($package, $subname) = $name =~ m{^(.+)::(\w+)$};
82 }
83 else
84 {
85 159 ▶ my $caller = @_ ? shift : $^H{'Kavorka/package'};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 159 159
86 159 ($package, $subname) = ($caller, $name);
87 }
88
89 164 ▶ return wantarray ? ($package, $subname) : "$package\::$subname";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 164 164
90 }
91  
92  
93 sub _exporter_fail
94 298 ▶{
Subroutine _exporter_fail: called CC=9 SCAR=22.0
95 298 my $me = shift;
96 298 my ($name, $args, $globals) = @_;
97
98 my $implementation =
99 298 ▶ $args->{'implementation'}
Condition50%3/6
($args->{"implementation"} // $me->guess_implementation($name)) // $me
l!l&&r!l&&!r
29800
$args->{"implementation"} // $me->guess_implementation($name)
l!l&&r!l&&!r
12970
MC/DC0%0/3
($args->{"implementation"} // $me->guess_implementation($name)) // $me
$args->{"implementation"} $me->guess_implementation($name) $me
Truth table50%2/4
A // B // C
A= $args->{"implementation"}B= $me->guess_implementation($name)C= $me
ABCresult
1XX1
01X1
0011
0000
100 // $me->guess_implementation($name)
101 // $me;
102
103 298 my $into = $globals->{into};
104
105 298 Module::Runtime::use_package_optimistically($implementation);
106
107 {
108 298 ▶ my $traits = $globals->{traits} // $args->{traits};
Condition66%2/3
$globals->{"traits"} // $args->{"traits"}
l!l&&r!l&&!r
01297
MC/DC50%1/2
$globals->{"traits"} // $args->{"traits"}
$globals->{"traits"} $args->{"traits"}
Truth table66%2/3
A // B
A= $globals->{"traits"}B= $args->{"traits"}
ABresult
1X1
011
000
109 298 ▶ $implementation = $me->compose_implementation($implementation, @$traits)
Branch: 1 branch
BranchTrueFalseTotal
if $traits 1 297 298
110 if $traits;
111 }
112
113 298 ▶ $implementation->can('parse')
Branch: 1 branch
BranchTrueFalseTotal
$implementation->can("parse") or Carp::croak("No suitable implementation for keyword '${name}'") 298 0 298
114 or Carp::croak("No suitable implementation for keyword '$name'");
115
116 # Workaround for RT#95786 which might be caused by a bug in the Perl
117 # interpreter.
118 # Also RT#98666 is why we can't just call undefer_all.
119 298 require Sub::Defer;
120 298 for (keys %Sub::Defer::DEFERRED) {
121 38 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 Sub::Defer::undefer_sub($_)
123 11101 ▶ if $Sub::Defer::DEFERRED{$_} && $Sub::Defer::DEFERRED{$_}[0] =~ /\AKavorkaX?\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $Sub::Defer::DEFERRED{$_} and $Sub::Defer::DEFERRED{$_}[0] =~ /\AKavorkaX?\b/u 2118 8983 11101
Condition100%3/3
$Sub::Defer::DEFERRED{$_} and $Sub::Defer::DEFERRED{$_}[0] =~ /\AKavorkaX?\b/u
!ll&&!rl&&r
253964442118
MC/DC100%2/2
$Sub::Defer::DEFERRED{$_} and $Sub::Defer::DEFERRED{$_}[0] =~ /\AKavorkaX?\b/u
$Sub::Defer::DEFERRED{$_} $Sub::Defer::DEFERRED{$_}[0] =~ /\AKavorkaX?\b/u
Truth table100%3/3
A and B
A= $Sub::Defer::DEFERRED{$_}B= $Sub::Defer::DEFERRED{$_}[0] =~ /\AKavorkaX?\b/u
ABresult
0X0
100
111
124 }
125
126 # Kavorka::Multi (for example) needs to know what Kavorka keywords are
127 # currently in scope.
128 298 $^H{'Kavorka'} .= "$name=$implementation ";
129
130 # This is the code that gets called at run-time.
131 #
132 my $code = Sub::Util::set_subname(
133 "$me\::$name",
134 42 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
Subroutine method: called CC=8 SCAR=20.8
Subroutine fun: called CC=8 SCAR=20.8
Subroutine before: called CC=8 SCAR=20.8
Subroutine after: called CC=8 SCAR=20.8
Subroutine multi: called CC=8 SCAR=20.8
135 42 ▶ unless (Scalar::Util::blessed($_[0]) and $_[0]->DOES('Kavorka::Sub'))
Branch: 2 branches
BranchTrueFalseTotal
unless (builtin::blessed($_[0]) and $_[0]->DOES("Kavorka::Sub")) 0 42 42
unless (builtin::blessed($_[0]) and $_[0]->DOES("Kavorka::Sub")) 3 154 157
Condition50%3/6
builtin::blessed($_[0]) and $_[0]->DOES("Kavorka::Sub")
!ll&&!rl&&r
0042
builtin::blessed($_[0]) and $_[0]->DOES("Kavorka::Sub")
!ll&&!rl&&r
30154
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->DOES("Kavorka::Sub")
builtin::blessed($_[0]) $_[0]->DOES("Kavorka::Sub")
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->DOES("Kavorka::Sub")
builtin::blessed($_[0]) $_[0]->DOES("Kavorka::Sub")
Truth table33%1/3
A and B
A= builtin::blessed($_[0])B= $_[0]->DOES("Kavorka::Sub")
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->DOES("Kavorka::Sub")
ABresult
0X0
100
111
136 {
137 0 return $implementation->bypass_custom_parsing($name, $into, \@_);
138 }
139  
140 42 my $subroutine = shift;
141
142 # Post-parse clean-up
143 42 $subroutine->_post_parse();
144
145 # Store $subroutine for introspection
146 42 $INFO{ $subroutine->body } = $subroutine;
147
148 # Install sub
149 42 ▶ my @r = wantarray
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 1 41 42
wantarray ? : 15 139 154
150 ? $subroutine->install_sub
151 : scalar($subroutine->install_sub);
152
153 # Workarounds for closure issues in Parse::Keyword
154 42 ▶ if ($subroutine->is_anonymous)
Branch: 2 branches
BranchTrueFalseTotal
if ($subroutine->is_anonymous) { } 5 37 42
if ($subroutine->is_anonymous) { } 24 126 150
155 {
156 5 my $orig = $r[0];
157 5 my $caller_vars = PadWalker::peek_my(1);
158 50 ▶ @r = Sub::Util::set_subname($subroutine->package."::__ANON__", sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 50 $subroutine->_poke_pads($caller_vars);
160 50 goto $orig;
161 5 });
162 5 &Scalar::Util::set_prototype($r[0], $_) for grep defined, prototype($orig);
163 5 $INFO{ $r[0] } = $subroutine;
164 5 Scalar::Util::weaken($INFO{ $r[0] });
165 }
166 else
167 {
168 37 $subroutine->_poke_pads( PadWalker::peek_my(1) );
169 }
170
171 # Prevents a cycle between %INFO and $subroutine.
172 Scalar::Util::weaken($subroutine->{body})
173 42 ▶ unless Scalar::Util::isweak($subroutine->{body});
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::is_weak($subroutine->{"body"}) 42 0 42
unless builtin::is_weak($subroutine->{"body"}) 144 6 150
174
175 42 ▶ wantarray ? @r : $r[0];
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 1 41 42
wantarray ? : 15 135 150
176 },
177 298 );
178
179 # This joins up the code above with our custom parsing via
180 # Parse::Keyword
181 #
182 Parse::Keyword::install_keyword_handler(
183 $code => Sub::Util::set_subname(
184 "$me\::parse_$name",
185 188 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
186 188 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
187 188 my $subroutine = $implementation->parse(keyword => $name);
188 return (
189 196 ▶ sub { ($subroutine, $args) },
Subroutine __ANON__: called CC=1 SCAR=0.0
190 188 !! $subroutine->declared_name,
191 );
192 },
193 298 ),
194 );
195
196 # Symbol for Exporter::Tiny to export
197 298 return ($name => $code);
198 }
199  
200 38 1;
201  
202 __END__
« Parse/KeywordX.pm ↑ Summary Kavorka/MethodModifier.pm »