blib/lib/CGI/Application/Plugin/RunmodeParseKeyword.pm

filter: Statementstmt 92.6% 215 / 232 Branchbran 55.9% 47 / 84 Conditioncond 64.0% 16 / 25 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 97.2% 35 / 36 Podpod 100% 9 / 9 totaltotal 81.5% 326 / 400 CC 55 SCAR 21.2
CC
55
Cov
82%
parse_signature
21.7
parse_attribute
16.0
parse_body
12.0
CRAP 8.4
↑ Summary
1 package CGI::Application::Plugin::RunmodeParseKeyword;
2 9 $CGI::Application::Plugin::RunmodeParseKeyword::VERSION = '0.15';
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 CGI::Application::Plugin::RunmodeParseKeyword - Declare runmodes using Parse::Keyword
9  
10 =cut
11  
12 9 our @EXPORT = qw(runmode errormode startmode);
13 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Sub::Name 'subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Parse::Keyword {};
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 $Carp::Internal{ (__PACKAGE__) }++;
19  
20 14 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
21 14 my $caller = caller;
22 14 my $class = shift;
23 14 my %args = @_;
24 14 ▶ my $into = delete $args{into} || $caller;
Condition66%2/3
delete $args{'into'} || $caller
l!l&&r!l&&!r
4100
MC/DC0%0/2
delete $args{'into'} || $caller
delete $args{'into'} $caller
Truth table66%2/3
A || B
A= delete $args{'into'}B= $caller
ABresult
1X1
011
000
25 14 ▶ my $inv = delete $args{invocant} || '$self';
Condition100%2/2
delete $args{'invocant'} || '$self'
l!l
410
MC/DC100%1/1
delete $args{'invocant'} || '$self'
delete $args{'invocant'}
Truth table100%2/2
A
A= delete $args{'invocant'}
Aresult
11
00
26  
27 14 ▶ Parse::Keyword->import( { runmode => sub { my ($kw) = @_; parse_mode($kw, $inv); } } );
Subroutine __ANON__: called CC=1 SCAR=0.0
28 14 ▶ Parse::Keyword->import( { errormode => sub { my ($kw) = @_; parse_mode($kw, $inv); } } );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 14 ▶ Parse::Keyword->import( { startmode => sub { my ($kw) = @_; parse_mode($kw, $inv); } } );
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 14 for my $e(@EXPORT) {
32 42 my $fn = $into . '::' . $e;
33 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 42 *$fn = \&$e;
35 42 *$fn if 0;
36 }
37 }
38  
39 11 ▶sub runmode { @_ ? $_[0] : () }
Subroutine runmode: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 11 11
40 6 ▶sub errormode { @_ ? $_[0] : () }
Subroutine errormode: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 6 6
41 8 ▶sub startmode { @_ ? $_[0] : () }
Subroutine startmode: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 8 8
42  
43 9 my %REGISTRY;
44 11 ▶sub _setup_runmode {
Subroutine _setup_runmode: called CC=1 SCAR=0.0
45 11 my ($pkg, $name, $code) = @_;
46 11 ▶ $pkg->add_callback( init => sub { $_[0]->run_modes([ $name ]) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
47 }
48 8 ▶sub _setup_startmode {
Subroutine _setup_startmode: called CC=2 SCAR=6.9
49 8 my ($pkg, $name, $code) = @_;
50 9 ▶ no strict 'refs'; no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
51 # compile time check
52 8 ▶ croak "start mode redefined (from $REGISTRY{$pkg}{start_mode_installed})" if $REGISTRY{$pkg}{start_mode_installed};
Branch: 1 branch
BranchTrueFalseTotal
if $REGISTRY{$pkg}{"start_mode_installed"} 0 8 8
53 $pkg->add_callback(
54 26 ▶ init => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
55 # run time check
56 26 ▶ return if exists $_[0]->{__START_MODE_SET_BY_RUNMODEDECLARE};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"__START_MODE_SET_BY_RUNMODEDECLARE"} 10 16 26
57 16 $_[0]->run_modes( [$name] );
58 16 $_[0]->start_mode($name);
59 16 $_[0]->{__START_MODE_SET_BY_RUNMODEDECLARE} = 1;
60 }
61 8 );
62 8 $REGISTRY{$pkg}{start_mode_installed} = join '::', $pkg, $name;
63 }
64 6 ▶sub _setup_errormode {
Subroutine _setup_errormode: called CC=2 SCAR=6.9
65 6 my ($pkg, $name, $code) = @_;
66 9 ▶ no strict 'refs'; no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
67 6 ▶ croak "error mode redefined (from $REGISTRY{$pkg}{error_mode_installed})" if $REGISTRY{$pkg}{error_mode_installed};
Branch: 1 branch
BranchTrueFalseTotal
if $REGISTRY{$pkg}{"error_mode_installed"} 0 6 6
68 $pkg->add_callback(
69 19 ▶ init => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
70 19 ▶ return if exists $_[0]->{__ERROR_MODE_SET_BY_RUNMODEDECLARE};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"__ERROR_MODE_SET_BY_RUNMODEDECLARE"} 4 15 19
71 15 $_[0]->error_mode($name);
72 15 $_[0]->{__ERROR_MODE_SET_BY_RUNMODEDECLARE} = 1;
73 }
74 6 );
75 6 $REGISTRY{$pkg}{error_mode_installed} = join '::', $pkg, $name;
76 }
77  
78 =begin pod-coverage
79  
80 =over 4
81  
82 =item parse_mode - we hook into this to install cgiapp callbacks
83  
84 =item parse_name - identifier name parser
85  
86 =item parse_signature - runmode signature parser
87  
88 =item parse_attribute - parse one sub attr
89  
90 =item parse_attributes - parse sub attrs
91  
92 =item parse_body - parse code and inject defaults
93  
94 =back
95  
96 =end pod-coverage
97  
98 =cut
99  
100 28 ▶sub parse_mode {
Subroutine parse_mode: called CC=6 SCAR=18.1
Pod documented
101 28 my ($keyword, $invocant) = @_;
102  
103 28 my $name = parse_name();
104 28 my $sig = parse_signature($invocant);
105 27 my $attr = parse_attributes();
106 27 ▶ my $body = parse_body($sig) or ($@ and die);
Branch: 1 branch
BranchTrueFalseTotal
my $body = parse_body($sig) or $@ and die 25 1 26
Condition60%3/5
(my $body = parse_body($sig)) || $@ && die()
l!l&&r!l&&!r
2510
$@ and die
!ll
01
MC/DC0%0/2
(my $body = parse_body($sig)) || $@ && die()
(my $body = parse_body($sig)) $@
Truth table66%2/3
A || B
A= (my $body = parse_body($sig))B= $@
ABresult
1X1
011
000
107  
108 25 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 25 0 25
109 25 my $full_name = join('::', compiling_package, $name);
110 {
111 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 25 *$full_name = subname $full_name, $body;
113 25 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 1 24 25
114 9 ▶ use attributes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 attributes->import(compiling_package, $body, $_) for @$attr;
116 }
117 25 my $setup = '_setup_' . $keyword;
118 25 $setup->(compiling_package, $name, $body);
119  
120 }
121 25 ▶ return (sub {}, 1);
Subroutine __ANON__: called CC=1 SCAR=0.0
122 }
123 else {
124 0 ▶ return (sub { $body }, 0);
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 }
126 }
127  
128 9 my $start_rx = qr/^[\p{ID_Start}_]$/;
129 9 my $cont_rx = qr/^\p{ID_Continue}$/;
130  
131 59 ▶sub parse_name {
Subroutine parse_name: called CC=4 SCAR=13.9
Pod documented
132 59 my $name = '';
133  
134 59 lex_read_space;
135  
136 59 my $char_rx = $start_rx;
137  
138 59 while (1) {
139 304 my $char = lex_peek;
140 304 ▶ last unless length $char;
Branch: 1 branch
BranchTrueFalseTotal
unless length $char 0 304 304
141 304 ▶ if ($char =~ $char_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /$char_rx/) { } 245 59 304
142 245 $name .= $char;
143 245 lex_read;
144 245 $char_rx = $cont_rx;
145 }
146 else {
147 59 last;
148 }
149 }
150  
151 59 ▶ return length($name) ? $name : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 58 1 59
152 }
153  
154 28 ▶sub parse_signature {
Subroutine parse_signature: called CC=15 SCAR=30.8
Pod documented
155 28 my ($invocant_name) = @_;
156 28 lex_read_space;
157  
158 28 my @vars = ({ index => 0, name => $invocant_name });
159 28 ▶ return \@vars unless lex_peek eq '(';
Branch: 1 branch
BranchTrueFalseTotal
unless lex_peek() eq "(" 0 0 0
160  
161 19 my @attr = ();
162  
163 19 lex_read;
164 19 lex_read_space;
165  
166 19 ▶ if (lex_peek eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq ")") 0 0 0
167 1 lex_read;
168 1 return \@vars;
169 }
170  
171 18 my $seen_slurpy;
172 18 ▶ while ((my $sigil = lex_peek) ne ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $sigil = lex_peek()) ne ")") 0 0 0
173 29 my $var = {};
174 29 ▶ die "syntax error: expected ')' but found '$sigil'"
Branch: 1 branch
BranchTrueFalseTotal
unless $sigil eq "\$" or $sigil eq "\@" or $sigil eq "%" 0 29 29
Condition50%3/6
$sigil eq "\$" or $sigil eq "\@" or $sigil eq "%"
l!l&&r!l&&!r
2900
$sigil eq "\$" or $sigil eq "\@"
l!l&&r!l&&!r
2360
MC/DC0%0/3
$sigil eq "\$" or $sigil eq "\@" or $sigil eq "%"
$sigil eq "\$" $sigil eq "\@" $sigil eq "%"
Truth table50%2/4
A or B or C
A= $sigil eq "\$"B= $sigil eq "\@"C= $sigil eq "%"
ABCresult
1XX1
01X1
0011
0000
175 unless $sigil eq '$' || $sigil eq '@' || $sigil eq '%';
176 29 ▶ die "Can't declare parameters after a slurpy parameter"
Branch: 1 branch
BranchTrueFalseTotal
if $seen_slurpy 0 29 29
177 if $seen_slurpy;
178  
179 29 ▶ $seen_slurpy = 1 if $sigil eq '@' || $sigil eq '%';
Branch: 1 branch
BranchTrueFalseTotal
if $sigil eq "\@" or $sigil eq "%" 6 23 29
Condition66%2/3
$sigil eq "\@" or $sigil eq "%"
l!l&&r!l&&!r
6023
MC/DC50%1/2
$sigil eq "\@" or $sigil eq "%"
$sigil eq "\@" $sigil eq "%"
Truth table66%2/3
A or B
A= $sigil eq "\@"B= $sigil eq "%"
ABresult
1X1
011
000
180  
181 29 lex_read;
182 29 lex_read_space;
183 29 my $name = parse_name(0);
184 29 lex_read_space;
185  
186 29 $var->{name} = "$sigil$name";
187  
188 29 ▶ if (lex_peek eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq "=") 0 0 0
189 3 lex_read;
190 3 lex_read_space;
191 3 $var->{default} = parse_arithexpr;
192 }
193  
194 29 $var->{index} = @vars - 1;
195  
196 29 ▶ if (lex_peek eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq ":") 0 0 0
197 0 $vars[0] = $var;
198 0 lex_read;
199 0 lex_read_space;
200 0 next;
201 }
202  
203 29 push @vars, $var;
204  
205 29 my $c = lex_peek;
206 29 ▶ die "syntax error: expected ')' or ',' but found '$c'"
Branch: 1 branch
BranchTrueFalseTotal
unless $c eq ")" or $c eq "," 1 28 29
Condition100%3/3
$c eq ")" or $c eq ","
l!l&&r!l&&!r
17111
MC/DC100%2/2
$c eq ")" or $c eq ","
$c eq ")" $c eq ","
Truth table100%3/3
A or B
A= $c eq ")"B= $c eq ","
ABresult
1X1
011
000
207 unless $c eq ')' || $c eq ',';
208  
209 28 ▶ if (lex_peek eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq ",") 0 0 0
210 11 lex_read;
211 11 lex_read_space;
212 }
213 }
214  
215 17 lex_read;
216  
217 17 return \@vars;
218 }
219  
220 # grabbed these two functions from
221 # https://metacpan.org/release/PEVANS/XS-Parse-Keyword-0.22/source/hax/lexer-additions.c.inc#L74
222 2 ▶sub parse_attribute {
Subroutine parse_attribute: called CC=9 SCAR=27.7
Pod documented
223 2 my $name = parse_name;
224 2 ▶ if (lex_peek ne '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() ne "(") 0 0 0
225 1 return $name;
226 }
227 1 $name .= lex_peek;
228 1 lex_read;
229 1 my $count = 1;
230 1 my $c = lex_peek;
231 1 ▶ while($count && length $c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count and length $c) 1 0 1
Condition33%1/3
$count and length $c
!ll&&!rl&&r
001
MC/DC0%0/2
$count and length $c
$count length $c
Truth table33%1/3
A and B
A= $countB= length $c
ABresult
0X0
100
111
232 8 ▶ if($c eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(") 0 8 8
233 0 $count++;
234 }
235 8 ▶ if($c eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")") 1 7 8
236 1 $count--;
237 }
238 8 ▶ if($c eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 0 8 8
239 # The next char does not bump count even if it is ( or );
240 # the \\ is still captured
241 #
242 0 $name .= $c;
243 0 lex_read;
244 0 $c = lex_peek;
245 0 ▶ if(! length $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $c) 0 0 0
246 0 goto unterminated;
247 }
248 }
249  
250 # Don't append final closing ')' on split name/val
251 8 $name .= $c;
252 8 lex_read;
253  
254 8 $c = lex_peek;
255 }
256  
257 1 ▶ if(!length $c) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $c) 0 1 1
258 0 return;
259 }
260  
261 1 return $name;
262  
263 0 unterminated:
264 croak("Unterminated attribute parameter in attribute list");
265 0 return;
266 }
267  
268 27 ▶sub parse_attributes {
Subroutine parse_attributes: called CC=4 SCAR=15.2
Pod documented
269 27 lex_read_space;
270 27 ▶ return unless lex_peek eq ':';
Branch: 1 branch
BranchTrueFalseTotal
unless lex_peek() eq ":" 0 0 0
271 1 lex_read;
272 1 lex_read_space;
273 1 my @attrs;
274 1 ▶ while (my $attr = parse_attribute) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = parse_attribute()) 1 1 2
275 1 push @attrs, $attr;
276 1 lex_read_space;
277 1 ▶ if (lex_peek eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (lex_peek() eq ":") 0 0 0
278 0 lex_read;
279 0 lex_read_space;
280 }
281 }
282  
283 1 return \@attrs;
284 }
285  
286 27 ▶sub parse_body {
Subroutine parse_body: called CC=12 SCAR=24.9
Pod documented
287 27 my $sigs = shift;
288 27 my $body;
289  
290 27 lex_read_space;
291  
292 27 my $c = lex_peek;
293 27 ▶ croak "syntax error: expected start of block '{' but found '$c'" unless $c eq '{';
Branch: 1 branch
BranchTrueFalseTotal
unless $c eq "{" 1 26 27
294 {
295 26 local $CAPRPK::{'DEFAULTS::'};
296 26 ▶ if ($sigs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigs) 26 0 26
297 26 lex_read;
298  
299 26 my $preamble = '{';
300  
301 # invocant
302 26 my $inv = shift @$sigs;
303 26 $preamble .= "my $inv->{name} = shift;";
304  
305 # arguments / query params
306 26 my @names = map { $_->{name} } @$sigs;
307 26 $preamble .= 'my (' . join(', ', @names) . ') = @_;';
308  
309 26 for my $name (@names) {
310 27 my $p;
311 27 my $s = substr($name,0,1);
312 27 my $n = substr($name,1);
313 27 ▶ if ($s eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq "\$") 21 6 27
314 21 $p = $inv->{name} . '->param("' . $n . '")';
315 21 ▶ $preamble .= $name . ' = ' . $p . ' unless ' . ( $s eq '$' ? 'defined ' : 'scalar ') . $name . ';';
Branch: 1 branch
BranchTrueFalseTotal
$s eq '$' ? : 21 0 21
316 }
317 27 ▶ $p = $inv->{name} . '->query->' . ($s eq '@' ? 'multi_param' : 'param') . '("' . $n . '")';
Branch: 1 branch
BranchTrueFalseTotal
$s eq '@' ? : 6 21 27
318 27 ▶ $preamble .= $name . ' = ' . $p . ' unless ' . ( $s eq '$' ? 'defined ' : 'scalar ') . $name . ';';
Branch: 1 branch
BranchTrueFalseTotal
$s eq '$' ? : 21 6 27
319 27 ▶ if ($s eq '@') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s eq "\@") 6 21 27
320 6 ▶ $p = $inv->{name} . '->query->' . ($s eq '@' ? 'multi_param' : 'param') . '("' . $n . '[]")';
Branch: 1 branch
BranchTrueFalseTotal
$s eq '@' ? : 6 0 6
321 6 ▶ $preamble .= $name . ' = ' . $p . ' unless ' . ( $s eq '$' ? 'defined ' : 'scalar ') . $name . ';';
Branch: 1 branch
BranchTrueFalseTotal
$s eq '$' ? : 0 6 6
322 }
323 }
324  
325 26 my $index = 0;
326 26 for my $var (grep { defined $_->{default} } @$sigs) {
327 {
328 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
329 3 ▶ *{ 'CAPRPK::DEFAULTS::default_' . $index } = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
330 $var->{default}
331 3 };
332 }
333 3 $preamble .= $var->{name} . ' = CAPRPK::DEFAULTS::default_' . $index . '->()' . ' unless ' . $var->{name} . ';';
334  
335 3 $index++;
336 }
337  
338 # warn $preamble . $/;
339 26 lex_stuff($preamble);
340 }
341 26 $body = parse_block;
342 }
343 26 return $body;
344 }
345  
346 9 1;
347  
348 __END__
↑ Summary