Perinci/Sub/Wrapper.pm

filter: Statementstmt 88.0% 591 / 671 Branchbran 69.2% 198 / 286 Conditioncond 60.2% 97 / 161 MC/DCmcdc 27.6% 26 / 94 Subroutinesub 81.9% 59 / 72 Podpod 2.6% 1 / 38 totaltotal 73.5% 972 / 1322 CC 230 SCAR 38.6
CC
230
Cov
79%
wrap
124.5
handle_result
68.4
_check_module
56.4
CRAP 47.6
↑ Summary Test/Perinci/Sub/Wrapper.pm »
1 package Perinci::Sub::Wrapper;
2  
3 19 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use Data::Dmp qw(dmp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Function::Fallback::CoreOrPP qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Perinci::Sub::Normalize qw(normalize_function_metadata);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Perinci::Sub::Util qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
16 15 our $DATE = '2023-07-09'; # DATE
17 15 our $DIST = 'Perinci-Sub-Wrapper'; # DIST
18 15 our $VERSION = '0.853'; # VERSION
19  
20 15 our @EXPORT_OK = qw(wrap_sub);
21  
22 15 ▶our $Log_Wrapper_Code = $ENV{LOG_PERINCI_WRAPPER_CODE} // 0;
Condition50%1/2
$ENV{"LOG_PERINCI_WRAPPER_CODE"} // 0
l!l
015
MC/DC0%0/1
$ENV{"LOG_PERINCI_WRAPPER_CODE"} // 0
$ENV{"LOG_PERINCI_WRAPPER_CODE"}
Truth table50%1/2
A
A= $ENV{"LOG_PERINCI_WRAPPER_CODE"}
Aresult
11
00
23  
24 15 our %SPEC;
25  
26 15 $SPEC{':package'} = {
27 v => 1.1,
28 summary => 'A multi-purpose subroutine wrapping framework',
29 };
30  
31 # "protocol version" (v). whenever there's a significant change in the basic
32 # structure of the wrapper, which potentially cause some/a lot of property
33 # handlers to stop working, we increase this. property handler must always state
34 # which version it follows in its meta. if unspecified, it's assumed to be 1.
35 15 our $protocol_version = 2;
36  
37 112 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
38 112 my ($class) = @_;
39 112 bless {}, $class;
40 }
41  
42 110 ▶sub _check_module {
Subroutine _check_module: called CC=10 SCAR=40.3
43 110 my ($self, $mod) = @_;
44  
45 110 ▶ if ($self->{_args}{core}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_args"}{"core"}) 0 110 110
46 0 ▶ if ($mod =~ /\A(experimental|Scalar::Numeric::Util|Scalar::Util::Numeric::PP)\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mod =~ /\A(experimental|Scalar::Numeric::Util|Scalar::Util::Numeric::PP)\z/) { } 0 0 0
elsif ($mod =~ /\A(warnings|List::Util)\z/) { } 0 0 0
47 0 die "BUG: Requested non-core module '$mod' while wrap arg core=1";
48 } elsif ($mod =~ /\A(warnings|List::Util)\z/) {
49 # core modules
50 } else {
51 0 die "BUG: Haven't noted whether module '$mod' is core/non-core";
52 }
53 }
54  
55 110 ▶ if ($self->{_args}{pp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_args"}{"pp"}) 0 110 110
56 0 ▶ if ($mod =~ /\A(List::Util|Scalar::Numeric::Util)\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mod =~ /\A(List::Util|Scalar::Numeric::Util)\z/) { } 0 0 0
elsif ($mod =~ /\A(experimental|warnings|Scalar::Util::Numeric::PP)\z/) { } 0 0 0
57 0 die "BUG: Requested XS module '$mod' while wrap arg pp=1";
58 } elsif ($mod =~ /\A(experimental|warnings|Scalar::Util::Numeric::PP)\z/) {
59 # pp modules
60 } else {
61 0 die "BUG: Haven't noted whether module '$mod' is pure-perl/XS";
62 }
63 }
64  
65 110 ▶ if ($self->{_args}{core_or_pp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_args"}{"core_or_pp"}) 0 110 110
66 0 ▶ if ($mod =~ /\A(Scalar::Numeric::Util)\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mod =~ /\A(Scalar::Numeric::Util)\z/) { } 0 0 0
elsif ($mod =~ /\A(experimental|warnings|List::Util|Scalar::Util::Numeric::PP)\z/) { } 0 0 0
67 0 die "BUG: Requested non-core XS module '$mod' while wrap arg core_or_pp=1";
68 } elsif ($mod =~ /\A(experimental|warnings|List::Util|Scalar::Util::Numeric::PP)\z/) {
69 # core or pp modules
70 } else {
71 0 die "BUG: Haven't noted whether module '$mod' is non-core xs or not";
72 }
73 }
74 }
75  
76 154 ▶sub _add_module {
Subroutine _add_module: called CC=4 SCAR=13.9
77 154 my ($self, $mod) = @_;
78 154 ▶ unless (grep { $_ eq $mod } @{ $self->{_modules} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $mod;} @{$$self{"_modules"};}) 110 44 154
79 110 local $self->{_cur_section};
80 110 $self->select_section('before_sub_require_modules');
81 110 ▶ if ($mod =~ /\A(use|no) (\S+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mod =~ /\A(use|no) (\S+)/) { } 52 58 110
elsif ($mod =~ /\A\w+(::\w+)*\z/) { } 58 0 58
82 52 $self->_check_module($2);
83 52 $self->push_lines("$mod;");
84 } elsif ($mod =~ /\A\w+(::\w+)*\z/) {
85 58 $self->_check_module($mod);
86 58 $self->push_lines("require $mod;");
87 } else {
88 0 die "BUG: Invalid module name/statement: $mod";
89 }
90 110 push @{ $self->{_modules} }, $mod;
91 }
92 }
93  
94 100 ▶sub _add_var {
Subroutine _add_var: called CC=2 SCAR=7.0
95 100 my ($self, $var, $value) = @_;
96 100 ▶ unless (exists $self->{_vars}{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_vars"}{$var}) 100 0 100
97 100 local $self->{_cur_section};
98 100 $self->select_section('declare_vars');
99 100 $self->push_lines("my \$$var = ".dmp($value).";");
100 100 $self->{_vars}{$var} = $value;
101 }
102 }
103  
104 2364 ▶sub _known_sections {
Subroutine _known_sections: called CC=1 SCAR=0.0
105  
106 # order=>N regulates the order of code. embed=>1 means the code is for embed
107 # mode only and should not be included in dynamic wrapper code.
108  
109 2364 state $val = {
110 before_sub_require_modules => {order=>1},
111  
112 # reserved by wrapper for setting Perl package and declaring 'sub {'
113 OPEN_SUB => {order=>4},
114  
115 # reserved to say 'my %args = @_;' or 'my @args = @_;' etc
116 ACCEPT_ARGS => {order=>5},
117  
118 # reserved to get args values if converted from array/arrayref
119 ACCEPT_ARGS2 => {order=>6},
120  
121 declare_vars => {order=>7},
122  
123 # for handlers to put stuffs right before eval. for example, 'timeout'
124 # uses this to set ALRM signal handler.
125 before_eval => {order=>10},
126  
127 # reserved by wrapper for generating 'eval {'
128 OPEN_EVAL => {order=>20},
129  
130 # used e.g. to load modules used by validation
131 before_call_before_arg_validation => {order=>31},
132  
133 before_call_arg_validation => {order=>32},
134  
135 # used e.g. by dependency checking
136 before_call_after_arg_validation => {order=>33},
137  
138 # feed arguments to sub
139 before_call_feed_args => {order=>48},
140  
141 # for handlers that *must* do stuffs right before call
142 before_call_right_before_call => {order=>49},
143  
144 # reserved by the wrapper for calling the sub
145 CALL => {order=>50},
146  
147 # for handlers that *must* do stuffs right after call
148 after_call_right_after_call => {order=>51},
149  
150 # reserved by the wrapper for adding/stripping result envelope, this
151 # happens before result validation
152 AFTER_CALL_ADD_OR_STRIP_RESULT_ENVELOPE => {order=>52},
153  
154 # used e.g. to load modules used by validation
155 after_call_before_res_validation => {order=>61},
156  
157 after_call_res_validation => {order=>62},
158  
159 after_call_after_res_validation => {order=>63},
160  
161 # reserved by wrapper to put eval end '}' and capturing result in
162 # $_w_res and $@ in $eval_err
163 CLOSE_EVAL => {order=>70},
164  
165 # for handlers to put checks against $eval_err
166 after_eval => {order=>80},
167  
168 # reserved for returning final result '$_w_res'
169 BEFORE_CLOSE_SUB => {order=>99},
170  
171 # reserved for sub closing '}' line
172 CLOSE_SUB => {order=>100},
173 };
174 2364 $val;
175 }
176  
177 4820 ▶sub section_empty {
Subroutine section_empty: called CC=1 SCAR=0.0
Pod undocumented
178 4820 my ($self, $section) = @_;
179 4820 !$self->{_codes}{$section};
180 }
181  
182 110 ▶sub _needs_eval {
Subroutine _needs_eval: called CC=2 SCAR=8.1
183 110 my ($self) = @_;
184 110 ▶ !($self->section_empty('before_eval') &&
Condition33%1/3
$self->section_empty('before_eval') && $self->section_empty('after_eval')
!ll&&!rl&&r
00110
MC/DC0%0/2
$self->section_empty('before_eval') && $self->section_empty('after_eval')
$self->section_empty('before_eval') $self->section_empty('after_eval')
Truth table33%1/3
A && B
A= $self->section_empty('before_eval')B= $self->section_empty('after_eval')
ABresult
0X0
100
111
185 $self->section_empty('after_eval'));
186 }
187  
188 # whether we need to store call result to a variable ($_w_res)
189 104 ▶sub _needs_store_res {
Subroutine _needs_store_res: called CC=5 SCAR=16.2
190 104 my ($self) = @_;
191 104 ▶ return 1 if $self->{_args}{validate_result};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_args"}{"validate_result"} 98 6 104
192 6 ▶ return 1 if $self->_needs_eval;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_needs_eval 0 6 6
193 6 my $ks = $self->_known_sections;
194 6 for (grep {/^after_call/} keys %$ks) {
195 24 ▶ return 1 unless $self->section_empty($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->section_empty($_) 0 24 24
196 }
197 6 0;
198 }
199  
200 1266 ▶sub _check_known_section {
Subroutine _check_known_section: called CC=2 SCAR=7.1
201 1266 my ($self, $section) = @_;
202 1266 my $ks = $self->_known_sections;
203 1266 ▶ $ks->{$section} or die "BUG: Unknown code section '$section'";
Branch: 1 branch
BranchTrueFalseTotal
$ks->{$section} or die "BUG: Unknown code section '${section}'" 1266 0 1266
204 }
205  
206 366 ▶sub _err {
Subroutine _err: called CC=2 SCAR=6.9
207 366 my ($self, $c_status, $c_msg) = @_;
208 366 ▶ if ($self->{_meta}{result_naked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_meta'}{'result_naked'}) { } 8 358 366
209 8 $self->push_lines(
210 "warn 'ERROR ' . ($c_status) . ': '. ($c_msg);",
211 'return undef;',
212 );
213 } else {
214 358 $self->push_lines("return [$c_status, $c_msg];");
215 }
216 }
217  
218 270 ▶sub _errif {
Subroutine _errif: called CC=1 SCAR=0.0
219 270 my ($self, $c_status, $c_msg, $c_cond) = @_;
220 270 $self->push_lines("if ($c_cond) {");
221 270 $self->indent;
222 270 $self->_err($c_status, $c_msg);
223 270 $self->unindent;
224 270 $self->push_lines('}');
225 }
226  
227 1266 ▶sub select_section {
Subroutine select_section: called CC=1 SCAR=0.0
Pod undocumented
228 1266 my ($self, $section) = @_;
229 1266 $self->_check_known_section($section);
230 1266 $self->{_cur_section} = $section;
231 1266 $self;
232 }
233  
234 646 ▶sub indent {
Subroutine indent: called CC=2 SCAR=7.0
Pod undocumented
235 646 my ($self) = @_;
236 646 my $section = $self->{_cur_section};
237 646 ▶ $self->{_codes}{$section} //= undef;
Condition50%1/2
$self->{"_codes"}{$section} //= undef
l!l
6460
MC/DC0%0/1
$self->{"_codes"}{$section} //= undef
$self->{"_codes"}{$section}
Truth table50%1/2
A
A= $self->{"_codes"}{$section}
Aresult
11
00
238 646 $self->{_levels}{$section}++;
239 646 $self;
240 }
241  
242 640 ▶sub unindent {
Subroutine unindent: called CC=2 SCAR=6.9
Pod undocumented
243 640 my ($self) = @_;
244 640 my $section = $self->{_cur_section};
245 640 ▶ $self->{_codes}{$section} //= undef;
Condition100%2/2
$self->{"_codes"}{$section} //= undef
l!l
536104
MC/DC100%1/1
$self->{"_codes"}{$section} //= undef
$self->{"_codes"}{$section}
Truth table100%2/2
A
A= $self->{"_codes"}{$section}
Aresult
11
00
246 640 $self->{_levels}{$section}--;
247 640 $self;
248 }
249  
250 86 ▶sub get_indent_level {
Subroutine get_indent_level: called CC=2 SCAR=7.1
Pod undocumented
251 86 my ($self) = @_;
252 86 my $section = $self->{_cur_section};
253 86 ▶ $self->{_levels}{$section} // 0;
Condition50%1/2
$self->{"_levels"}{$section} // 0
l!l
860
MC/DC0%0/1
$self->{"_levels"}{$section} // 0
$self->{"_levels"}{$section}
Truth table50%1/2
A
A= $self->{"_levels"}{$section}
Aresult
11
00
254 }
255  
256 # line can be code or comment. code should not contain string literals that
257 # cross lines (i.e. contain literal newlines) because push_lines() might add
258 # comment at the end of each line.
259  
260 2534 ▶sub push_lines {
Subroutine push_lines: called CC=6 SCAR=18.9
Pod undocumented
261 2534 my ($self, @lines) = @_;
262 2534 my $section = $self->{_cur_section};
263  
264 2534 ▶ unless (exists $self->{_codes}{$section}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_codes"}{$section}) 780 1754 2534
265 780 unshift @lines, "# * section: $section";
266 # don't give blank line for the top-most section (order=>0)
267 780 ▶ unshift @lines, "" if $self->_known_sections->{$section}{order};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_known_sections->{$section}{"order"} 780 0 780
268 780 $self->{_codes}{$section} = [];
269 780 $self->{_levels}{$section} = 0;
270 }
271  
272 2534 @lines = map {[$self->{_levels}{$section}, $_]} @lines;
273 2534 ▶ if ($self->{_args}{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_args"}{"debug"}) 0 2534 2534
274 0 for my $l (@lines) {
275 $l->[2] =
276 $self->{_cur_handler} ?
277 "$self->{_cur_handler} prio=".$self->{_cur_handler_meta}{prio}
278 0 ▶ : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_cur_handler'} ? : 0 0 0
279 }
280 }
281 2534 push @{$self->{_codes}{$section}}, @lines;
282 2534 $self;
283 }
284  
285 208 ▶sub _join_codes {
Subroutine _join_codes: called CC=9 SCAR=22.7
286 208 my ($self, $crit, $prev_section_level) = @_;
287 208 my @lines;
288 208 my $ks = $self->_known_sections;
289 208 ▶ $prev_section_level //= 0;
Condition100%2/2
$prev_section_level //= 0
l!l
104104
MC/DC100%1/1
$prev_section_level //= 0
$prev_section_level
Truth table100%2/2
A
A= $prev_section_level
Aresult
11
00
290 208 my $i = 0;
291 208 for my $s (sort {$ks->{$a}{order} <=> $ks->{$b}{order}}
292 keys %$ks) {
293 4576 ▶ next if $self->section_empty($s);
Branch: 1 branch
BranchTrueFalseTotal
if $self->section_empty($s) 2832 1744 4576
294 1744 ▶ next unless $crit->(section => $s);
Branch: 1 branch
BranchTrueFalseTotal
unless &$crit("section", $s) 1080 664 1744
295 664 $i++;
296 664 for my $l (@{ $self->{_codes}{$s} }) {
297 3919 $l->[0] += $prev_section_level;
298 3919 ▶ die "BUG: Negative indent level in line $i (section $s): '$l->[1]'"
Branch: 1 branch
BranchTrueFalseTotal
if $l->[0] < 0 0 3919 3919
299 if $l->[0] < 0;
300 3919 my $s = ($self->{_args}{indent} x $l->[0]) . $l->[1];
301 3919 ▶ if (defined $l->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l->[2]) 0 3919 3919
302 0 my $num_ws = 80 - length($s);
303 0 ▶ $num_ws = 1 if $num_ws < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num_ws < 1 0 0 0
304 0 $s .= (" " x $num_ws) . "## $l->[2]";
305 }
306 3919 push @lines, $s;
307 }
308 664 $prev_section_level += $self->{_levels}{$s};
309 }
310 208 [join("\n", @lines), $prev_section_level];
311 }
312  
313 52 ▶sub _format_dyn_wrapper_code {
Subroutine _format_dyn_wrapper_code: called CC=1 SCAR=0.0
314 52 my ($self) = @_;
315 52 my $ks = $self->_known_sections;
316 $self->_join_codes(
317 436 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
318 436 my %args = @_;
319 436 my $section = $args{section};
320 436 !$ks->{$section}{embed};
321 52 })->[0];
322 }
323  
324 # for embedded, we need to produce three sections which will be inserted in
325 # different places, demonstrated below:
326 #
327 # $SPEC{foo} = {
328 # ...
329 # };
330 # sub foo {
331 # my %args = @_;
332 # # do stuffs
333 # }
334 #
335 # becomes:
336 #
337 # #PRESUB1: require modules (inserted before sub declaration)
338 # require Data::Dumper;
339 # require Scalar::Util;
340 #
341 # $SPEC{foo} = {
342 # ...
343 # };
344 # #PRESUB2: modify metadata piece-by-piece (inserted before sub declaration &
345 # #after $SPEC{foo}). we're avoiding dumping the new modified metadata because
346 # #metadata might contain coderefs which is sometimes problematic when dumping
347 # {
348 # my $meta = $SPEC{foo};
349 # $meta->{v} = 1.1;
350 # $meta->{result_naked} = 0;
351 # }
352 #
353 # sub foo {
354 # my %args = @_;
355 # #PREAMBLE: before call sections (inserted after accept args), e.g.
356 # #validate arguments, convert argument type, setup eval block
357 # #...
358 #
359 # # do stuffs
360 #
361 # #POSTAMBLE: after call sections (inserted before sub end), e.g.
362 # #validate result, close eval block and do retry/etc.
363 # #...
364 # }
365 52 ▶sub _format_embed_wrapper_code {
Subroutine _format_embed_wrapper_code: called CC=1 SCAR=0.0
366 52 my ($self) = @_;
367  
368 52 my $res = {};
369 52 my $ks = $self->_known_sections;
370 52 my $j;
371  
372 $j = $self->_join_codes(
373 436 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
374 436 my %args = @_;
375 436 my $section = $args{section};
376 436 $section =~ /\A(before_sub_require_modules)\z/;
377 52 });
378 52 $res->{presub1} = $j->[0];
379  
380 # no longer needed/produce, code to modify metadata
381 52 $res->{presub2} = '';
382  
383 $j = $self->_join_codes(
384 436 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
385 436 my %args = @_;
386 436 my $section = $args{section};
387 436 my $order = $ks->{$section}{order};
388 return 1 if $order > $ks->{ACCEPT_ARGS}{order} &&
389 436 ▶ $order < $ks->{CALL}{order};
Branch: 1 branch
BranchTrueFalseTotal
if $order > $ks->{"ACCEPT_ARGS"}{"order"} and $order < $ks->{"CALL"}{"order"} 94 342 436
Condition100%3/3
$order > $ks->{"ACCEPT_ARGS"}{"order"} and $order < $ks->{"CALL"}{"order"}
!ll&&!rl&&r
13221094
MC/DC100%2/2
$order > $ks->{"ACCEPT_ARGS"}{"order"} and $order < $ks->{"CALL"}{"order"}
$order > $ks->{"ACCEPT_ARGS"}{"order"} $order < $ks->{"CALL"}{"order"}
Truth table100%3/3
A and B
A= $order > $ks->{"ACCEPT_ARGS"}{"order"}B= $order < $ks->{"CALL"}{"order"}
ABresult
0X0
100
111
390 342 0;
391 52 }, 1);
392 52 $res->{preamble} = $j->[0];
393  
394 $j = $self->_join_codes(
395 436 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
396 436 my %args = @_;
397 436 my $section = $args{section};
398 436 my $order = $ks->{$section}{order};
399 return 1 if $order > $ks->{CALL}{order} &&
400 436 ▶ $order < $ks->{CLOSE_SUB}{order};
Branch: 1 branch
BranchTrueFalseTotal
if $order > $ks->{"CALL"}{"order"} and $order < $ks->{"CLOSE_SUB"}{"order"} 106 330 436
Condition100%3/3
$order > $ks->{"CALL"}{"order"} and $order < $ks->{"CLOSE_SUB"}{"order"}
!ll&&!rl&&r
27852106
MC/DC100%2/2
$order > $ks->{"CALL"}{"order"} and $order < $ks->{"CLOSE_SUB"}{"order"}
$order > $ks->{"CALL"}{"order"} $order < $ks->{"CLOSE_SUB"}{"order"}
Truth table100%3/3
A and B
A= $order > $ks->{"CALL"}{"order"}B= $order < $ks->{"CLOSE_SUB"}{"order"}
ABresult
0X0
100
111
401 330 0;
402 52 }, $j->[1]);
403 52 $res->{postamble} = $j->[0];
404  
405 52 $res;
406 }
407  
408 108 ▶sub handlemeta_v { {} }
Subroutine handlemeta_v: called CC=1 SCAR=0.0
Pod undocumented
409 0 ▶sub handlemeta_name { {} }
Subroutine handlemeta_name: not called CC=1 SCAR=6.9
Pod undocumented
410 0 ▶sub handlemeta_summary { {} }
Subroutine handlemeta_summary: not called CC=1 SCAR=6.9
Pod undocumented
411 0 ▶sub handlemeta_description { {} }
Subroutine handlemeta_description: not called CC=1 SCAR=6.9
Pod undocumented
412 0 ▶sub handlemeta_tags { {} }
Subroutine handlemeta_tags: not called CC=1 SCAR=6.9
Pod undocumented
413 0 ▶sub handlemeta_default_lang { {} }
Subroutine handlemeta_default_lang: not called CC=1 SCAR=6.9
Pod undocumented
414 0 ▶sub handlemeta_links { {} }
Subroutine handlemeta_links: not called CC=1 SCAR=6.9
Pod undocumented
415 0 ▶sub handlemeta_text_markup { {} }
Subroutine handlemeta_text_markup: not called CC=1 SCAR=6.9
Pod undocumented
416 0 ▶sub handlemeta_is_func { {} }
Subroutine handlemeta_is_func: not called CC=1 SCAR=6.9
Pod undocumented
417 0 ▶sub handlemeta_is_meth { {} }
Subroutine handlemeta_is_meth: not called CC=1 SCAR=6.9
Pod undocumented
418 0 ▶sub handlemeta_is_class_meth { {} }
Subroutine handlemeta_is_class_meth: not called CC=1 SCAR=6.9
Pod undocumented
419 0 ▶sub handlemeta_examples { {} }
Subroutine handlemeta_examples: not called CC=1 SCAR=6.9
Pod undocumented
420  
421 # after args
422 4 ▶sub handlemeta_features { {v=>2, prio=>15} }
Subroutine handlemeta_features: called CC=1 SCAR=0.0
Pod undocumented
423 4 ▶sub handle_features {
Subroutine handle_features: called CC=4 SCAR=14.8
Pod undocumented
424 4 my ($self, %args) = @_;
425  
426 4 my $meta = $self->{_meta};
427 4 ▶ my $v = $meta->{features} // {};
Condition50%1/2
$meta->{"features"} // {}
l!l
40
MC/DC0%0/1
$meta->{"features"} // {}
$meta->{"features"}
Truth table50%1/2
A
A= $meta->{"features"}
Aresult
11
00
428  
429 4 $self->select_section('before_call_before_arg_validation');
430  
431 4 ▶ if ($v->{tx} && $v->{tx}{req}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"tx"} and $v->{"tx"}{"req"}) 4 0 4
Condition33%1/3
$v->{"tx"} and $v->{"tx"}{"req"}
!ll&&!rl&&r
004
MC/DC0%0/2
$v->{"tx"} and $v->{"tx"}{"req"}
$v->{"tx"} $v->{"tx"}{"req"}
Truth table33%1/3
A and B
A= $v->{"tx"}B= $v->{"tx"}{"req"}
ABresult
0X0
100
111
432 4 $self->push_lines('', '# check required transaction');
433 4 $self->_errif(412, '"Must run with transaction (pass -tx_manager)"',
434 '!$args{-tx_manager}');
435 }
436 }
437  
438 # run before args
439 108 ▶sub handlemeta_args_as { {v=>2, prio=>1, convert=>1} }
Subroutine handlemeta_args_as: called CC=1 SCAR=0.0
Pod undocumented
440 108 ▶sub handle_args_as {
Subroutine handle_args_as: called CC=32 SCAR=36.6
Pod undocumented
441 108 my ($self, %args) = @_;
442  
443 108 my $value = $args{value};
444 108 my $new = $args{new};
445 108 my $meta = $self->{_meta};
446 108 ▶ my $args_p = $meta->{args} // {};
Condition100%2/2
$meta->{"args"} // {}
l!l
7434
MC/DC100%1/1
$meta->{"args"} // {}
$meta->{"args"}
Truth table100%2/2
A
A= $meta->{"args"}
Aresult
11
00
447 108 my $opt_va = $self->{_args}{validate_args};
448  
449 # We support conversion of arguments between hash/hashref/array/arrayref. To
450 # make it simple, currently the algorithm is as follow: we first form the
451 # %args hash. If args_as is already 'hash', we just do 'my %args = @_'.
452 # Otherwise, we convert from the other forms.
453 #
454 # We then validate each argument in %args (code generated in 'args'
455 # handler).
456 #
457 # Finally, unless original args_as is 'hash' we convert to the final form
458 # that the wrapped sub expects.
459 #
460 # This setup is optimal when both the sub and generated wrapper accept
461 # 'hash', but suboptimal for other cases (especially positional ones, as
462 # they have to undergo a round-trip to hash even when both accept 'array').
463 # This will be rectified in the future.
464  
465 108 ▶ my $v = $new // $value;
Condition66%2/3
$new // $value
l!l&&r!l&&!r
41040
MC/DC0%0/2
$new // $value
$new $value
Truth table66%2/3
A // B
A= $newB= $value
ABresult
1X1
011
000
466  
467 108 $self->select_section('ACCEPT_ARGS');
468 108 ▶ if ($v eq 'hash') {
Branch: 3 branches
BranchTrueFalseTotal
if ($v eq 'hash') { } 100 8 108
elsif ($v eq 'hashref') { } 2 6 8
elsif ($v =~ /\Aarray(ref)?\z/) { } 6 0 6
469 100 ▶ $self->push_lines(q{die 'BUG: Odd number of hash elements supplied' if @_ % 2;})
Branch: 1 branch
BranchTrueFalseTotal
if $opt_va 94 6 100
470 if $opt_va;
471 100 $self->push_lines('my %args = @_;');
472 } elsif ($v eq 'hashref') {
473 2 ▶ $self->push_lines(q{die 'BUG: $_[0] needs to be hashref' if @_ && ref($_[0]) ne "HASH";})
Branch: 1 branch
BranchTrueFalseTotal
if $opt_va 2 0 2
474 if $opt_va;
475 2 $self->push_lines('my %args = %{$_[0] // {}};');
476 } elsif ($v =~ /\Aarray(ref)?\z/) {
477 6 ▶ my $ref = $1 ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 2 4 6
478 6 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 2 4 6
479 2 ▶ $self->push_lines(q{die 'BUG: $_[0] needs to be arrayref' if @_ && ref($_[0]) ne "ARRAY";})
Branch: 1 branch
BranchTrueFalseTotal
if $opt_va 2 0 2
480 if $opt_va;
481 }
482 6 $self->push_lines('my %args;');
483 6 $self->select_section('ACCEPT_ARGS2');
484 6 for my $a (sort keys %$args_p) {
485 12 my $as = $args_p->{$a};
486 12 my $line = '$args{'.dmp($a).'} = ';
487 12 ▶ defined($as->{pos}) or die "Error in args property for arg '$a': ".
Branch: 1 branch
BranchTrueFalseTotal
defined $as->{"pos"} or die "Error in args property for arg '${a}': " . 'no pos defined' 12 0 12
488 "no pos defined";
489 12 my $pos = int($as->{pos} + 0);
490 12 ▶ $pos >= 0 or die "Error in args property for arg '$a': ".
Branch: 1 branch
BranchTrueFalseTotal
$pos >= 0 or die "Error in args property for arg '${a}': " . 'negative value in pos' 12 0 12
491 "negative value in pos";
492 12 ▶ if ($as->{slurpy} // $as->{greedy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as->{'slurpy'} // $as->{'greedy'}) { } 0 12 12
Condition33%1/3
$as->{"slurpy"} // $as->{"greedy"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$as->{"slurpy"} // $as->{"greedy"}
$as->{"slurpy"} $as->{"greedy"}
Truth table33%1/3
A // B
A= $as->{"slurpy"}B= $as->{"greedy"}
ABresult
1X1
011
000
493 0 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 0 0 0
494 0 $line .= '[splice @{$_[0]}, '.$pos.'] if @{$_[0]} > '.$pos;
495 } else {
496 0 $line .= '[splice @_, '.$pos.'] if @_ > '.$pos;
497 }
498 } else {
499 12 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 4 8 12
500 4 $line .= '$_[0]['.$pos.'] if @{$_[0]} > '.$pos;
501 } else {
502 8 $line .= '$_['.$pos.'] if @_ > '.$pos;
503 }
504 }
505 12 $self->push_lines("$line;");
506 }
507 } else {
508 0 die "Unknown args_as value '$v'";
509 }
510  
511 108 $self->select_section('ACCEPT_ARGS');
512 108 ▶ if ($value eq 'hashref') {
Branch: 3 branches
BranchTrueFalseTotal
if ($value eq 'hashref') { } 2 106 108
elsif ($value eq 'array') { } 4 102 106
elsif ($value eq 'arrayref') { } 2 100 102
513 2 $self->push_lines('my $args;');
514 } elsif ($value eq 'array') {
515 4 $self->push_lines('my @args;');
516 } elsif ($value eq 'arrayref') {
517 2 $self->push_lines('my $args;');
518 }
519  
520 108 my $tok;
521 108 $self->select_section('before_call_feed_args');
522 108 $v = $value;
523 108 ▶ if ($v eq 'hash') {
Branch: 3 branches
BranchTrueFalseTotal
if ($v eq 'hash') { } 100 8 108
elsif ($v eq 'hashref') { } 2 6 8
elsif ($v =~ /\Aarray(ref)?\z/) { } 6 0 6
524 100 $tok = '%args';
525 } elsif ($v eq 'hashref') {
526 2 $tok = '$args';
527 2 $self->push_lines($tok.' = \%args;'); # XXX should we set each arg instead?
528 } elsif ($v =~ /\Aarray(ref)?\z/) {
529 6 ▶ my $ref = $1 ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 2 4 6
530 6 ▶ $tok = ($ref ? '$':'@') . 'args';
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 2 4 6
531 6 for my $a (sort {$args_p->{$a}{pos} <=> $args_p->{$b}{pos}}
532 keys %$args_p) {
533 12 my $as = $args_p->{$a};
534 12 my $t = '$args{'.dmp($a).'}';
535 12 my $line;
536 12 ▶ defined($as->{pos}) or die "Error in args property for arg '$a': ".
Branch: 1 branch
BranchTrueFalseTotal
defined $as->{"pos"} or die "Error in args property for arg '${a}': " . 'no pos defined' 12 0 12
537 "no pos defined";
538 12 my $pos = int($as->{pos} + 0);
539 12 ▶ $pos >= 0 or die "Error in args property for arg '$a': ".
Branch: 1 branch
BranchTrueFalseTotal
$pos >= 0 or die "Error in args property for arg '${a}': " . 'negative value in pos' 12 0 12
540 "negative value in pos";
541 12 ▶ if ($as->{slurpy} // $as->{greedy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as->{'slurpy'} // $as->{'greedy'}) { } 0 12 12
Condition33%1/3
$as->{"slurpy"} // $as->{"greedy"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$as->{"slurpy"} // $as->{"greedy"}
$as->{"slurpy"} $as->{"greedy"}
Truth table33%1/3
A // B
A= $as->{"slurpy"}B= $as->{"greedy"}
ABresult
1X1
011
000
542 0 $line = 'splice @args, '.$pos.', scalar(@args)-1, @{'.$t.'}';
543 } else {
544 12 ▶ $line = '$args'.($ref ? '->':'').'['.$pos."] = $t if exists $t";
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 4 8 12
545 }
546 12 $self->push_lines("$line;");
547 }
548 } else {
549 0 die "Unknown args_as value '$v'";
550 }
551 108 $self->{_args_token} = $tok;
552 }
553  
554 11 ▶sub _sah {
Subroutine _sah: called CC=1 SCAR=0.0
555 11 require Data::Sah;
556  
557 11 my $self = shift;
558 11 state $sah = Data::Sah->new;
559 11 $sah;
560 }
561  
562 86 ▶sub _plc {
Subroutine _plc: called CC=1 SCAR=0.0
563 86 my $self = shift;
564 86 state $plc = do {
565 7 my $plc = $self->_sah->get_compiler("perl");
566 7 $plc->comment_style('shell2'); # to make all comment uses ## instead of #
567 7 $plc;
568 };
569 }
570  
571 78 ▶sub _handle_args {
Subroutine _handle_args: called CC=38 SCAR=36.6
572 78 my ($self, %args) = @_;
573  
574 78 ▶ my $v = $args{v} // $self->{_meta}{args};
Condition66%2/3
$args{"v"} // $self->{"_meta"}{"args"}
l!l&&r!l&&!r
4740
MC/DC0%0/2
$args{"v"} // $self->{"_meta"}{"args"}
$args{"v"} $self->{"_meta"}{"args"}
Truth table66%2/3
A // B
A= $args{"v"}B= $self->{"_meta"}{"args"}
ABresult
1X1
011
000
575 78 ▶ return unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 78 78
576  
577 78 my $opt_sin = $self->{_args}{_schema_is_normalized};
578 78 my $opt_va = $self->{_args}{validate_args};
579  
580 78 ▶ my $prefix = $args{prefix} // '';
Condition100%2/2
$args{"prefix"} // ""
l!l
474
MC/DC100%1/1
$args{"prefix"} // ""
$args{"prefix"}
Truth table100%2/2
A
A= $args{"prefix"}
Aresult
11
00
581 78 ▶ my $argsterm = $args{argsterm} // '%args';
Condition100%2/2
$args{"argsterm"} // "%args"
l!l
474
MC/DC100%1/1
$args{"argsterm"} // "%args"
$args{"argsterm"}
Truth table100%2/2
A
A= $args{"argsterm"}
Aresult
11
00
582  
583 78 ▶ if ($opt_va) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_va) 76 2 78
584 76 $self->select_section('before_call_arg_validation');
585 76 ▶ $self->push_lines('', '# check args') if $prefix eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $prefix eq "" 72 4 76
586 76 $self->push_lines("for my \$_w_arg (sort keys $argsterm) {");
587 76 $self->indent;
588 76 $self->_errif(400, q["Invalid argument name (please use letters/numbers/underscores only)'].$prefix.q[$_w_arg'"],
589 '$_w_arg !~ /\A(-?)\w+(\.\w+)*\z/o');
590 76 $self->_errif(400, q["Unknown argument '].$prefix.q[$_w_arg'"],
591 '!($1 || grep { $_ eq $_w_arg } @{ '.dmp([sort keys %$v]).' })');
592 76 $self->unindent;
593 76 $self->push_lines('}');
594 }
595  
596 78 for my $argname (sort keys %$v) {
597 108 my $argspec = $v->{$argname};
598  
599 108 my $argterm = $argsterm;
600 108 ▶ if ($argterm =~ /^%\{\s*(.+)\s*\}/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($argterm =~ /^%\{\s*(.+)\s*\}/) { } 8 100 108
elsif ($argterm =~ s/^%/\$/) { } 100 0 100
601 8 $argterm = $1 . "->{'$argname'}";
602 } elsif ($argterm =~ s/^%/\$/) {
603 100 $argterm .= "{'$argname'}";
604 } else {
605 0 $argterm .= "->{'$argname'}";
606 }
607  
608 108 my $has_default_prop = exists($argspec->{default});
609 108 my $sch = $argspec->{schema};
610  
611 108 ▶ if ($sch) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sch) { } 78 30 108
elsif ($has_default_prop) { } 2 28 30
612 my $has_sch_default = ref($sch) eq 'ARRAY' &&
613 78 ▶ exists($sch->[1]{default}) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
ref $sch eq 'ARRAY' && exists $sch->[1]{'default'} ? : 12 66 78
Condition100%3/3
ref $sch eq "ARRAY" and exists $sch->[1]{"default"}
!ll&&!rl&&r
26412
MC/DC100%2/2
ref $sch eq "ARRAY" and exists $sch->[1]{"default"}
ref $sch eq "ARRAY" exists $sch->[1]{"default"}
Truth table100%3/3
A and B
A= ref $sch eq "ARRAY"B= exists $sch->[1]{"default"}
ABresult
0X0
100
111
614 78 ▶ if ($opt_va) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_va) 74 4 78
615  
616 74 $self->push_lines("if (exists($argterm)) {");
617 74 $self->indent;
618  
619 74 ▶ if ($argspec->{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argspec->{"stream"}) 2 72 74
620 2 ▶ die "Error in schema for argument '$argname': must be str/buf/array if stream=1"
Branch: 1 branch
BranchTrueFalseTotal
unless $sch->[0] =~ /\A(str|buf|array)\z/ 0 2 2
621 unless $sch->[0] =~ /\A(str|buf|array)\z/; # XXX allow 'any' if all of its 'of' values are str/buf/array
622 die "Error in schema for argument '$argname': must specify 'of' array clause if stream=1"
623 2 ▶ if $sch->[0] eq 'array' && !$sch->[1]{of};
Branch: 1 branch
BranchTrueFalseTotal
if $sch->[0] eq "array" and not $sch->[1]{"of"} 0 2 2
Condition33%1/3
$sch->[0] eq "array" and not $sch->[1]{"of"}
!ll&&!rl&&r
020
MC/DC0%0/2
$sch->[0] eq "array" and not $sch->[1]{"of"}
$sch->[0] eq "array" not $sch->[1]{"of"}
Truth table33%1/3
A and not(B)
A= $sch->[0] eq "array"B= not $sch->[1]{"of"}
ABresult
0X0
100
111
624  
625 2 $self->_errif(
626 400,
627 qq["Argument '$prefix$argname' (stream) fails validation: must be coderef"],
628 "!(ref($argterm) eq 'CODE')",
629 );
630 2 $self->push_lines('{ ## introduce scope because we want to declare a generic variable $i');
631 2 $self->indent;
632 2 $self->push_lines(
633 'my $i = -1;',
634 "my \$origsub = $argterm;",
635 '# arg coderef wrapper for validation',
636 "$argterm = sub {",
637 );
638 2 $self->indent;
639 2 $self->push_lines(
640 '$i++;',
641 "my \$rec = \$origsub->();",
642 'return undef unless defined $rec;',
643 );
644 }
645  
646 74 my $dn = $argname; $dn =~ s/\W+/_/g;
647 my $cd = $self->_plc->compile(
648 data_name => $dn,
649 data_term => $argspec->{stream} ? '$rec' : $argterm,
650 schema => $argspec->{stream} && $sch->[0] eq 'array' ? $sch->[1]{of} : $sch,
651 schema_is_normalized => $opt_sin,
652 return_type => 'str',
653 indent_level => $self->get_indent_level + 1,
654 core => $self->{_args}{core},
655 core_or_pp => $self->{_args}{core_or_pp},
656 pp => $self->{_args}{pp},
657 74 ▶ %{ $self->{_args}{_extra_sah_compiler_args} // {}},
Branch: 2 branches
BranchTrueFalseTotal
$argspec->{'stream'} ? : 2 72 74
$argspec->{'stream'} && $sch->[0] eq 'array' ? : 2 72 74
Condition60%3/5
$argspec->{"stream"} and $sch->[0] eq "array"
!ll&&!rl&&r
7202
$self->{"_args"}{"_extra_sah_compiler_args"} // {}
l!l
074
MC/DC50%1/2
$argspec->{"stream"} and $sch->[0] eq "array"
$argspec->{"stream"} $sch->[0] eq "array"
MC/DC0%0/1
$self->{"_args"}{"_extra_sah_compiler_args"} // {}
$self->{"_args"}{"_extra_sah_compiler_args"}
Truth table66%2/3
A and B
A= $argspec->{"stream"}B= $sch->[0] eq "array"
ABresult
0X0
100
111
Truth table50%1/2
A
A= $self->{"_args"}{"_extra_sah_compiler_args"}
Aresult
11
00
658 );
659 72 ▶ die "Incompatible Data::Sah version (cd v=$cd->{v}, expected 2)" unless $cd->{v} == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"v"} == 2 0 72 72
660 72 for my $mod_rec (@{ $cd->{modules} }) {
661 208 ▶ next unless $mod_rec->{phase} eq 'runtime';
Branch: 1 branch
BranchTrueFalseTotal
unless $mod_rec->{"phase"} eq "runtime" 72 136 208
662 136 ▶ $self->_add_module($mod_rec->{use_statement} // $mod_rec->{name});
Condition66%2/3
$mod_rec->{"use_statement"} // $mod_rec->{"name"}
l!l&&r!l&&!r
72640
MC/DC0%0/2
$mod_rec->{"use_statement"} // $mod_rec->{"name"}
$mod_rec->{"use_statement"} $mod_rec->{"name"}
Truth table66%2/3
A // B
A= $mod_rec->{"use_statement"}B= $mod_rec->{"name"}
ABresult
1X1
011
000
663 }
664 $self->_add_var($_, $cd->{vars}{$_})
665 72 for sort keys %{ $cd->{vars} };
666 72 $cd->{result} =~ s/\A\s+//;
667 72 $self->push_lines(
668 "my \$err_$dn;",
669 "$cd->{result};",
670 );
671 72 ▶ if ($argspec->{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argspec->{'stream'}) { } 2 70 72
672 2 $self->push_lines(
673 'if ('."\$err_$dn".') { die "Record #$i of streaming argument '."'$prefix$argname'".' ($rec) fails validation: '."\$err_$dn".'" }',
674 '$rec;',
675 );
676 } else {
677 70 $self->_errif(
678 400, qq["Argument '$prefix$argname' fails validation: \$err_$dn"],
679 "\$err_$dn");
680 }
681 72 ▶ if ($argspec->{meta}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argspec->{"meta"}) 2 70 72
682 2 $self->push_lines("# check subargs of $prefix$argname");
683 $self->_handle_args(
684 %args,
685 v => $argspec->{meta}{args},
686 2 ▶ prefix => ($prefix ? "$prefix/" : "") . "$argname/",
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 2 2
687 argsterm => '%{'.$argterm.'}',
688 );
689 }
690 72 ▶ if ($argspec->{element_meta}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argspec->{"element_meta"}) 2 70 72
691 2 $self->push_lines("# check element subargs of $prefix$argname");
692 2 my $indexterm = "$prefix$argname";
693 2 $indexterm =~ s/\W+/_/g;
694 2 $indexterm = '$i_' . $indexterm;
695 2 $self->push_lines('for my '.$indexterm.' (0..$#{ '.$argterm.' }) {');
696 2 $self->indent;
697 2 $self->_errif(
698 400, qq("Argument '$prefix$argname\[).qq($indexterm]' fails validation: must be hash"),
699 "ref($argterm\->[$indexterm]) ne 'HASH'");
700 $self->_handle_args(
701 %args,
702 v => $argspec->{element_meta}{args},
703 2 ▶ prefix => ($prefix ? "$prefix/" : "") . "$argname\[$indexterm]/",
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 2 2
704 argsterm => '%{'.$argterm.'->['.$indexterm.']}',
705 );
706 2 $self->unindent;
707 2 $self->push_lines('}');
708 }
709 72 $self->unindent;
710 72 ▶ if ($argspec->{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argspec->{"stream"}) 2 70 72
711 2 $self->push_lines('}; ## arg coderef wrapper');
712 2 $self->unindent;
713 2 $self->push_lines('} ## close scope');
714 2 $self->unindent;
715 }
716 72 ▶ if ($has_default_prop) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_default_prop) { } 8 64 72
elsif ($has_sch_default) { } 8 56 64
717 $self->push_lines(
718 '} else {',
719 8 " $argterm //= ".dmp($argspec->{default}).";");
720 } elsif ($has_sch_default) {
721 $self->push_lines(
722 '} else {',
723 8 " $argterm //= ".dmp($sch->[1]{default}).";");
724 }
725 72 $self->push_lines("} ## if exists arg $prefix$argname");
726 } # if opt_va
727  
728 } elsif ($has_default_prop) {
729 # doesn't have schema but have 'default' property, we still need to
730 # set default here
731 2 $self->push_lines("$argterm = ".dmp($argspec->{default}).
732 " if !exists($argterm);");
733 }
734 106 ▶ if ($argspec->{req} && $opt_va) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argspec->{"req"} and $opt_va) 18 88 106
Condition100%3/3
$argspec->{"req"} and $opt_va
!ll&&!rl&&r
86218
MC/DC100%2/2
$argspec->{"req"} and $opt_va
$argspec->{"req"} $opt_va
Truth table100%3/3
A and B
A= $argspec->{"req"}B= $opt_va
ABresult
0X0
100
111
735 18 $self->_errif(
736 400, qq["Missing required argument: $argname"],
737 "!exists($argterm)");
738 }
739 } # for arg
740 }
741  
742 74 ▶sub handlemeta_args { {v=>2, prio=>10} }
Subroutine handlemeta_args: called CC=1 SCAR=0.0
Pod undocumented
743 74 ▶sub handle_args {
Subroutine handle_args: called CC=1 SCAR=0.0
Pod undocumented
744 74 my ($self, %args) = @_;
745 74 $self->_handle_args(%args);
746 }
747  
748 # after args
749 4 ▶sub handlemeta_args_rels { {v=>2, prio=>11} }
Subroutine handlemeta_args_rels: called CC=1 SCAR=0.0
Pod undocumented
750 4 ▶sub handle_args_rels {
Subroutine handle_args_rels: called CC=10 SCAR=23.9
Pod undocumented
751 4 my ($self, %args) = @_;
752  
753 4 ▶ my $v = $args{v} // $self->{_meta}{args_rels};
Condition33%1/3
$args{"v"} // $self->{"_meta"}{"args_rels"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$args{"v"} // $self->{"_meta"}{"args_rels"}
$args{"v"} $self->{"_meta"}{"args_rels"}
Truth table33%1/3
A // B
A= $args{"v"}B= $self->{"_meta"}{"args_rels"}
ABresult
1X1
011
000
754 4 ▶ return unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 4 4
755  
756 4 ▶ my $argsterm = $args{argsterm} // '%args';
Condition50%1/2
$args{"argsterm"} // "%args"
l!l
04
MC/DC0%0/1
$args{"argsterm"} // "%args"
$args{"argsterm"}
Truth table50%1/2
A
A= $args{"argsterm"}
Aresult
11
00
757  
758 4 $self->select_section('before_call_arg_validation');
759 4 $self->push_lines('', '# check args_rels');
760  
761 4 my $dn = "args_rels";
762 4 my $hc = $self->_sah->get_compiler("human");
763 4 my $cd_h = $hc->init_cd;
764 4 ▶ $cd_h->{args}{lang} //= $cd_h->{default_lang};
Condition33%1/3
$cd_h->{"args"}{"lang"} //= $cd_h->{"default_lang"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$cd_h->{"args"}{"lang"} //= $cd_h->{"default_lang"}
$cd_h->{"args"}{"lang"} $cd_h->{"default_lang"}
Truth table33%1/3
A //= B
A= $cd_h->{"args"}{"lang"}B= $cd_h->{"default_lang"}
ABresult
1X1
011
000
765  
766 my $cd = $self->_plc->compile(
767 data_name => $dn,
768 data_term => "\\$argsterm",
769 schema => ['hash', $v],
770 return_type => 'str',
771 indent_level => $self->get_indent_level + 1,
772 human_hash_values => {
773 field => $hc->_xlt($cd_h, "argument"),
774 fields => $hc->_xlt($cd_h, "arguments"),
775 },
776 core => $self->{_args}{core},
777 core_or_pp => $self->{_args}{core_or_pp},
778 pp => $self->{_args}{pp},
779 4 );
780 2 ▶ die "Incompatible Data::Sah version (cd v=$cd->{v}, expected 2)" unless $cd->{v} == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"v"} == 2 0 2 2
781 2 for my $mod_rec (@{ $cd->{modules} }) {
782 6 ▶ next unless $mod_rec->{phase} eq 'runtime';
Branch: 1 branch
BranchTrueFalseTotal
unless $mod_rec->{"phase"} eq "runtime" 2 4 6
783 4 ▶ $self->_add_module($mod_rec->{use_statement} // $mod_rec->{name});
Condition66%2/3
$mod_rec->{"use_statement"} // $mod_rec->{"name"}
l!l&&r!l&&!r
220
MC/DC0%0/2
$mod_rec->{"use_statement"} // $mod_rec->{"name"}
$mod_rec->{"use_statement"} $mod_rec->{"name"}
Truth table66%2/3
A // B
A= $mod_rec->{"use_statement"}B= $mod_rec->{"name"}
ABresult
1X1
011
000
784 }
785 2 $self->_add_var($_, $cd->{vars}{$_}) for sort keys %{ $cd->{vars} };
786 2 $cd->{result} =~ s/\A\s+//;
787 2 $self->push_lines(
788 "my \$err_$dn;",
789 "$cd->{result};",
790 );
791 2 $self->_errif(
792 400, qq["\$err_$dn"],
793 "\$err_$dn");
794 }
795  
796 14 ▶sub handlemeta_result { {v=>2, prio=>50} }
Subroutine handlemeta_result: called CC=1 SCAR=0.0
Pod undocumented
797 14 ▶sub handle_result {
Subroutine handle_result: called CC=32 SCAR=42.2
Pod undocumented
798 14 require Data::Sah;
799  
800 14 my ($self, %args) = @_;
801  
802 14 my $meta = $self->{_meta};
803 14 my $v = $meta->{result};
804 14 ▶ return unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 14 14
805  
806 14 my $opt_sin = $self->{_args}{_schema_is_normalized};
807 14 my $opt_vr = $self->{_args}{validate_result};
808  
809 14 my %schemas_by_status; # key = status, value = schema
810  
811 # collect and check handlers
812 my %handler_args;
813 14 my %handler_metas;
814 14 for my $k0 (keys %$v) {
815 18 my $k = $k0;
816 18 $k =~ s/\..+//;
817 18 ▶ next if $k =~ /\A_/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /\A_/ 2 16 18
818  
819 # check builtin result spec key
820 16 ▶ next if $k =~ /\A(
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /\A( summary|description|tags|default_lang| schema|statuses|stream| x )\z/x 16 0 16
821 summary|description|tags|default_lang|
822 schema|statuses|stream|
823 x
824 )\z/x;
825 # try a property module first
826 0 require "Perinci/Sub/Property/result/$k.pm"; ## no critic: Modules::RequireBarewordIncludes
827 0 my $meth = "handlemeta_result__$k";
828 0 ▶ unless ($self->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($meth)) 0 0 0
829 0 die "No handler for property result/$k0 ($meth)";
830 }
831 0 my $hm = $self->$meth;
832 0 ▶ $hm->{v} //= 1;
Condition0%0/2
$hm->{"v"} //= 1
l!l
00
MC/DC0%0/1
$hm->{"v"} //= 1
$hm->{"v"}
Truth table0%0/2
A
A= $hm->{"v"}
Aresult
11
00
833 0 ▶ next unless defined $hm->{prio};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hm->{"prio"} 0 0 0
834 die "Please update property handler result/$k which is still at v=$hm->{v} ".
835 "(needs v=$protocol_version)"
836 0 ▶ unless $hm->{v} == $protocol_version;
Branch: 1 branch
BranchTrueFalseTotal
unless $hm->{"v"} == $Perinci::Sub::Wrapper::protocol_version 0 0 0
837 my $ha = {
838 0 prio=>$hm->{prio}, value=>$v->{$k0}, property=>$k0,
839 meth=>"handle_result__$k",
840 };
841 0 $handler_args{$k} = $ha;
842 0 $handler_metas{$k} = $hm;
843 }
844  
845 # call all the handlers in order
846 0 for my $k (sort {$handler_args{$a}{prio} <=> $handler_args{$b}{prio}}
847 keys %handler_args) {
848 0 my $ha = $handler_args{$k};
849 0 my $meth = $ha->{meth};
850 0 local $self->{_cur_handler} = $meth;
851 0 local $self->{_cur_handler_meta} = $handler_metas{$k};
852 0 local $self->{_cur_handler_args} = $ha;
853 0 $self->$meth(args=>\%args, meta=>$meta, %$ha);
854 }
855  
856 # validate result
857 14 my @modules;
858 14 ▶ if ($v->{schema} && $opt_vr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"schema"} and $opt_vr) 4 10 14
Condition100%3/3
$v->{"schema"} and $opt_vr
!ll&&!rl&&r
824
MC/DC100%2/2
$v->{"schema"} and $opt_vr
$v->{"schema"} $opt_vr
Truth table100%3/3
A and B
A= $v->{"schema"}B= $opt_vr
ABresult
0X0
100
111
859 4 $schemas_by_status{200} = $v->{schema};
860 }
861 14 ▶ if ($v->{statuses} && $opt_vr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"statuses"} and $opt_vr) 0 14 14
Condition33%1/3
$v->{"statuses"} and $opt_vr
!ll&&!rl&&r
1400
MC/DC0%0/2
$v->{"statuses"} and $opt_vr
$v->{"statuses"} $opt_vr
Truth table33%1/3
A and B
A= $v->{"statuses"}B= $opt_vr
ABresult
0X0
100
111
862 0 for my $s (keys %{$v->{statuses}}) {
863 0 my $sv = $v->{statuses}{$s};
864 0 ▶ if ($sv->{schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv->{"schema"}) 0 0 0
865 0 $schemas_by_status{$s} = $sv->{schema};
866 }
867 }
868 }
869  
870 14 my $sub_name = $self->{_args}{sub_name};
871  
872 14 ▶ if ($opt_vr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_vr) 12 2 14
873 12 $self->select_section('after_call_res_validation');
874 $self->push_lines(
875 'my $_w_res2 = $_w_res->[2];',
876 12 ▶ 'my $_w_res_is_stream = $_w_res->[3]{stream} // ' . ($v->{stream} ? 1:0) . ';',
Branch: 1 branch
BranchTrueFalseTotal
$v->{'stream'} ? : 6 6 12
877 );
878 12 $self->_errif(
879 500,
880 q["Stream result must be coderef"],
881 '$_w_res_is_stream && ref($_w_res2) ne "CODE"',
882 );
883 12 for my $s (sort keys %schemas_by_status) {
884 4 my $sch = $schemas_by_status{$s};
885 4 ▶ if ($v->{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->{"stream"}) 2 2 4
886 2 ▶ die "Error in result schema: must be str/buf/array if stream=1"
Branch: 1 branch
BranchTrueFalseTotal
unless $sch->[0] =~ /\A(str|buf|array)\z/ 0 2 2
887 unless $sch->[0] =~ /\A(str|buf|array)\z/; # XXX allow 'any' if all of its 'of' values are str/buf/array
888 die "Error in result schema: must specify 'of' array clause if stream=1"
889 2 ▶ if $sch->[0] eq 'array' && !$sch->[1]{of};
Branch: 1 branch
BranchTrueFalseTotal
if $sch->[0] eq "array" and not $sch->[1]{"of"} 0 2 2
Condition33%1/3
$sch->[0] eq "array" and not $sch->[1]{"of"}
!ll&&!rl&&r
020
MC/DC0%0/2
$sch->[0] eq "array" and not $sch->[1]{"of"}
$sch->[0] eq "array" not $sch->[1]{"of"}
Truth table33%1/3
A and not(B)
A= $sch->[0] eq "array"B= not $sch->[1]{"of"}
ABresult
0X0
100
111
890 }
891 4 $self->push_lines("if (\$_w_res->[0] == $s) {");
892 4 $self->indent;
893 4 $self->push_lines('if (!$_w_res_is_stream) {');
894 4 $self->indent;
895  
896 # validation for when not a stream
897 my $cd = $self->_plc->compile(
898 data_name => '_w_res2',
899 # err_res can clash on arg named 'res'
900 err_term => '$_w_err2_res',
901 schema => $sch,
902 schema_is_normalized => $opt_sin,
903 return_type => 'str',
904 indent_level => $self->get_indent_level + 1,
905 core => $self->{_args}{core},
906 core_or_pp => $self->{_args}{core_or_pp},
907 pp => $self->{_args}{pp},
908 4 ▶ %{ $self->{_args}{_extra_sah_compiler_args} // {}},
Condition50%1/2
$self->{"_args"}{"_extra_sah_compiler_args"} // {}
l!l
04
MC/DC0%0/1
$self->{"_args"}{"_extra_sah_compiler_args"} // {}
$self->{"_args"}{"_extra_sah_compiler_args"}
Truth table50%1/2
A
A= $self->{"_args"}{"_extra_sah_compiler_args"}
Aresult
11
00
909 );
910 4 ▶ die "Incompatible Data::Sah version (cd v=$cd->{v}, expected 2)" unless $cd->{v} == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"v"} == 2 0 4 4
911 4 for my $mod_rec (@{ $cd->{modules} }) {
912 16 ▶ next unless $mod_rec->{phase} eq 'runtime';
Branch: 1 branch
BranchTrueFalseTotal
unless $mod_rec->{"phase"} eq "runtime" 6 10 16
913 10 ▶ $self->_add_module($mod_rec->{use_statement} // $mod_rec->{name});
Condition66%2/3
$mod_rec->{"use_statement"} // $mod_rec->{"name"}
l!l&&r!l&&!r
460
MC/DC0%0/2
$mod_rec->{"use_statement"} // $mod_rec->{"name"}
$mod_rec->{"use_statement"} $mod_rec->{"name"}
Truth table66%2/3
A // B
A= $mod_rec->{"use_statement"}B= $mod_rec->{"name"}
ABresult
1X1
011
000
914 }
915 $self->_add_var($_, $cd->{vars}{$_})
916 4 for sort keys %{ $cd->{vars} };
917 4 $self->push_lines("my \$_w_err2_res;");
918 4 $cd->{result} =~ s/\A\s+//;
919 4 $self->push_lines("$cd->{result};");
920 4 $self->_errif(
921 500,
922 qq["BUG: Result from sub $sub_name (\$_w_res2) fails validation: ].
923 qq[\$_w_err2_res"],
924 "\$_w_err2_res");
925 4 $self->unindent;
926 4 $self->push_lines("} else {"); # stream
927 4 $self->indent;
928 4 $self->push_lines(
929 'my $i = -1;',
930 '# wrap result coderef for validation',
931 '$_w_res->[2] = sub {',
932 );
933 4 $self->indent;
934 4 $self->push_lines(
935 '$i++;',
936 'my $rec = $_w_res2->();',
937 'return undef unless defined $rec;',
938 );
939 # generate schema code once again, this time for when stream
940 $cd = $self->_plc->compile(
941 data_name => 'rec',
942 # err_res can clash on arg named 'res'
943 err_term => '$rec_err',
944 schema => $sch->[0] eq 'array' ? $sch->[1]{of} : $sch,
945 schema_is_normalized => $opt_sin,
946 return_type => 'str',
947 indent_level => $self->get_indent_level + 1,
948 core => $self->{_args}{core},
949 core_or_pp => $self->{_args}{core_or_pp},
950 pp => $self->{_args}{pp},
951 4 ▶ %{ $self->{_args}{_extra_sah_compiler_args} // {}},
Branch: 1 branch
BranchTrueFalseTotal
$sch->[0] eq 'array' ? : 2 2 4
Condition50%1/2
$self->{"_args"}{"_extra_sah_compiler_args"} // {}
l!l
04
MC/DC0%0/1
$self->{"_args"}{"_extra_sah_compiler_args"} // {}
$self->{"_args"}{"_extra_sah_compiler_args"}
Truth table50%1/2
A
A= $self->{"_args"}{"_extra_sah_compiler_args"}
Aresult
11
00
952 );
953 4 ▶ die "Incompatible Data::Sah version (cd v=$cd->{v}, expected 2)" unless $cd->{v} == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"v"} == 2 0 4 4
954 # XXX no need to require modules required by validator?
955 4 $self->push_lines('my $rec_err;');
956 4 $cd->{result} =~ s/\A\s+//;
957 4 $self->push_lines("$cd->{result};");
958 4 $self->push_lines('if ($rec_err) { die "BUG: Result stream record #$i ($rec) fails validation: $rec_err" }');
959 4 $self->push_lines('$rec;');
960 4 $self->unindent;
961 4 $self->push_lines('}; ## result coderef wrapper');
962 4 $self->unindent;
963 4 $self->push_lines("} ## if stream");
964 4 $self->unindent;
965 4 $self->push_lines("} ## if status=$s");
966 } # for schemas_by_status
967 }
968 }
969  
970 6 ▶sub handlemeta_result_naked { {v=>2, prio=>99, convert=>1} }
Subroutine handlemeta_result_naked: called CC=1 SCAR=0.0
Pod undocumented
971 6 ▶sub handle_result_naked {
Subroutine handle_result_naked: called CC=4 SCAR=14.2
Pod undocumented
972 6 my ($self, %args) = @_;
973  
974 6 my $old = $args{value};
975 6 ▶ my $v = $args{new} // $old;
Condition33%1/3
$args{"new"} // $old
l!l&&r!l&&!r
600
MC/DC0%0/2
$args{"new"} // $old
$args{"new"} $old
Truth table33%1/3
A // B
A= $args{"new"}B= $old
ABresult
1X1
011
000
976  
977 6 ▶ return if !!$v == !!$old;
Branch: 1 branch
BranchTrueFalseTotal
if !!$v == !!$old 0 6 6
978  
979 6 $self->select_section('AFTER_CALL_ADD_OR_STRIP_RESULT_ENVELOPE');
980 6 ▶ if ($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) { } 2 4 6
981 2 $self->push_lines(
982 '', '# strip result envelope',
983 '$_w_res = $_w_res->[2];',
984 );
985 } else {
986 4 $self->push_lines(
987 '', '# add result envelope',
988 '$_w_res = [200, "OK", $_w_res];',
989 );
990 }
991 }
992  
993 4 ▶sub handlemeta_deps { {v=>2, prio=>0.5} }
Subroutine handlemeta_deps: called CC=1 SCAR=0.0
Pod undocumented
994 4 ▶sub handle_deps {
Subroutine handle_deps: called CC=4 SCAR=14.8
Pod undocumented
995 4 my ($self, %args) = @_;
996 4 my $value = $args{value};
997 4 my $meta = $self->{_meta};
998 4 my $v = $self->{_args}{meta_name};
999 4 $self->select_section('before_call_after_arg_validation');
1000 4 $self->push_lines('', '# check dependencies');
1001 4 $self->_add_module("Perinci::Sub::DepChecker");
1002 #$self->push_lines('use Data::Dump; dd '.$v.';');
1003 4 $self->push_lines('my $_w_deps_res = Perinci::Sub::DepChecker::check_deps('.
1004 $v.'->{deps});');
1005 4 $self->_errif(412, '"Deps failed: $_w_deps_res"', '$_w_deps_res');
1006  
1007 # we handle some deps our own
1008 4 ▶ if ($value->{tmp_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{"tmp_dir"}) 0 4 4
1009 0 $self->_errif(412, '"Dep failed: please specify -tmp_dir"',
1010 '!$args{-tmp_dir}');
1011 }
1012 4 ▶ if ($value->{trash_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{"trash_dir"}) 0 4 4
1013 0 $self->_errif(412, '"Dep failed: please specify -trash_dir"',
1014 '!$args{-trash_dir}');
1015 }
1016 4 ▶ if ($value->{undo_trash_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->{"undo_trash_dir"}) 0 4 4
1017 0 $self->_errif(412, '"Dep failed: please specify -undo_trash_dir"',
1018 '!($args{-undo_trash_dir} || $args{-tx_manager} || '.
1019 '$args{-undo_action} && $args{-undo_action}=~/\A(?:undo|redo)\z/)');
1020 }
1021 }
1022  
1023 110 ▶sub handlemeta_x { {} }
Subroutine handlemeta_x: called CC=1 SCAR=0.0
Pod undocumented
1024 0 ▶sub handlemeta_entity_v { {} }
Subroutine handlemeta_entity_v: not called CC=1 SCAR=6.9
Pod undocumented
1025 0 ▶sub handlemeta_entity_date { {} }
Subroutine handlemeta_entity_date: not called CC=1 SCAR=6.9
Pod undocumented
1026  
1027 108 ▶sub _reset_work_data {
Subroutine _reset_work_data: called CC=2 SCAR=6.9
1028 108 my ($self, %args) = @_;
1029  
1030 # to make it stand out more, all work/state data is prefixed with
1031 # underscore.
1032  
1033 108 $self->{_cur_section} = undef;
1034 108 $self->{_cur_handler} = undef;
1035 108 $self->{_cur_handler_args} = undef;
1036 108 $self->{_cur_handler_meta} = undef;
1037 108 $self->{_levels} = {};
1038 108 $self->{_codes} = {};
1039 108 $self->{_modules} = []; # modules loaded by wrapper sub
1040 108 $self->{$_} = $args{$_} for keys %args;
1041 }
1042  
1043 112 ▶sub wrap {
Subroutine wrap: called CC=72 SCAR=48.2
Pod undocumented
1044 112 require Scalar::Util;
1045  
1046 112 my ($self, %args) = @_;
1047  
1048 112 my $wrap_log_prop = "x.perinci.sub.wrapper.logs";
1049  
1050 # required arguments
1051 112 my $sub = $args{sub};
1052 112 my $sub_name = $args{sub_name};
1053 112 ▶ $sub || $sub_name or return [400, "Please specify sub or sub_name"];
Branch: 1 branch
BranchTrueFalseTotal
$sub or $sub_name or return [400, "Please specify sub or sub_name"] 112 0 112
Condition66%2/3
$sub or $sub_name
l!l&&r!l&&!r
11020
MC/DC0%0/2
$sub or $sub_name
$sub $sub_name
Truth table66%2/3
A or B
A= $subB= $sub_name
ABresult
1X1
011
000
1054 112 ▶ $args{meta} or return [400, "Please specify meta"];
Branch: 1 branch
BranchTrueFalseTotal
$args{"meta"} or return [400, "Please specify meta"] 112 0 112
1055 112 my $meta_name = $args{meta_name};
1056 # we clone the meta because we'll replace stuffs
1057 112 my $meta = clone($args{meta});
1058 112 ▶ my $wrap_logs = $meta->{$wrap_log_prop} // [];
Condition100%2/2
$meta->{$wrap_log_prop} // []
l!l
2110
MC/DC100%1/1
$meta->{$wrap_log_prop} // []
$meta->{$wrap_log_prop}
Truth table100%2/2
A
A= $meta->{$wrap_log_prop}
Aresult
11
00
1059  
1060 # currently internal args, not exposed/documented
1061 112 ▶ $args{_compiled_package} //= 'Perinci::Sub::Wrapped';
Condition50%1/2
$args{"_compiled_package"} //= "Perinci::Sub::Wrapped"
l!l
0112
MC/DC0%0/1
$args{"_compiled_package"} //= "Perinci::Sub::Wrapped"
$args{"_compiled_package"}
Truth table50%1/2
A
A= $args{"_compiled_package"}
Aresult
11
00
1062 112 my $comppkg = $args{_compiled_package};
1063 $args{_schema_is_normalized} //=
1064 112 ▶ $wrap_logs->[-1] && $wrap_logs->[-1]{normalize_schema} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$wrap_logs->[-1] && $wrap_logs->[-1]{'normalize_schema'} ? : 2 108 110
Condition80%4/5
$args{"_schema_is_normalized"} //= $wrap_logs->[-1] && $wrap_logs->[-1]{"normalize_schema"} ? 1 : 0
l!l
2110
$wrap_logs->[-1] and $wrap_logs->[-1]{"normalize_schema"}
!ll&&!rl&&r
10802
MC/DC100%1/1
$args{"_schema_is_normalized"} //= $wrap_logs->[-1] && $wrap_logs->[-1]{"normalize_schema"} ? 1 : 0
$args{"_schema_is_normalized"}
Truth table100%2/2
A
A= $args{"_schema_is_normalized"}
Aresult
11
00
1065 112 ▶ $args{_embed} //= 0;
Condition50%1/2
$args{"_embed"} //= 0
l!l
0112
MC/DC0%0/1
$args{"_embed"} //= 0
$args{"_embed"}
Truth table50%1/2
A
A= $args{"_embed"}
Aresult
11
00
1066 112 ▶ $args{_extra_sah_compiler_args} //= undef;
Condition50%1/2
$args{"_extra_sah_compiler_args"} //= undef
l!l
0112
MC/DC0%0/1
$args{"_extra_sah_compiler_args"} //= undef
$args{"_extra_sah_compiler_args"}
Truth table50%1/2
A
A= $args{"_extra_sah_compiler_args"}
Aresult
11
00
1067  
1068 # defaults for arguments
1069 112 ▶ $args{indent} //= " " x 4;
Condition50%1/2
$args{"indent"} //= " "
l!l
0112
MC/DC0%0/1
$args{"indent"} //= " "
$args{"indent"}
Truth table50%1/2
A
A= $args{"indent"}
Aresult
11
00
1070 112 ▶ $args{convert} //= {};
Condition100%2/2
$args{"convert"} //= {}
l!l
10102
MC/DC100%1/1
$args{"convert"} //= {}
$args{"convert"}
Truth table100%2/2
A
A= $args{"convert"}
Aresult
11
00
1071 112 ▶ $args{compile} //= 1;
Condition50%1/2
$args{"compile"} //= 1
l!l
0112
MC/DC0%0/1
$args{"compile"} //= 1
$args{"compile"}
Truth table50%1/2
A
A= $args{"compile"}
Aresult
11
00
1072 112 ▶ $args{log} //= 1;
Condition100%2/2
$args{"log"} //= 1
l!l
2110
MC/DC100%1/1
$args{"log"} //= 1
$args{"log"}
Truth table100%2/2
A
A= $args{"log"}
Aresult
11
00
1073 $args{validate_args} //= 0
1074 # function states that it can validate args, so by default we don't have
1075 # to do validation for it.
1076 112 ▶ if $meta->{features} && $meta->{features}{validate_args};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"features"} and $meta->{"features"}{"validate_args"} 0 112 112
Condition40%2/5
$meta->{"features"} and $meta->{"features"}{"validate_args"}
!ll&&!rl&&r
10840
$args{"validate_args"} //= 0
l!l
00
MC/DC0%0/2
$meta->{"features"} and $meta->{"features"}{"validate_args"}
$meta->{"features"} $meta->{"features"}{"validate_args"}
MC/DC0%0/1
$args{"validate_args"} //= 0
$args{"validate_args"}
Truth table66%2/3
A and B
A= $meta->{"features"}B= $meta->{"features"}{"validate_args"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $args{"validate_args"}
Aresult
11
00
1077 $args{validate_args} //= 0
1078 # function might want to disable validate_args by default, e.g. if
1079 # source code has been processed with
1080 # Dist::Zilla::Plugin::Rinci::Validate
1081 112 ▶ if $meta->{'x.perinci.sub.wrapper.disable_validate_args'};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"x.perinci.sub.wrapper.disable_validate_args"} 2 110 112
Condition50%1/2
$args{"validate_args"} //= 0
l!l
02
MC/DC0%0/1
$args{"validate_args"} //= 0
$args{"validate_args"}
Truth table50%1/2
A
A= $args{"validate_args"}
Aresult
11
00
1082 $args{validate_args} //= 0
1083 # by default do not validate args again if previous wrapper(s) have
1084 # already done it
1085 112 ▶ if (grep {$_->{validate_args}} @$wrap_logs);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"validate_args"};} @$wrap_logs 2 110 112
Condition50%1/2
$args{"validate_args"} //= 0
l!l
02
MC/DC0%0/1
$args{"validate_args"} //= 0
$args{"validate_args"}
Truth table50%1/2
A
A= $args{"validate_args"}
Aresult
11
00
1086 112 ▶ $args{validate_args} //= 1;
Condition100%2/2
$args{"validate_args"} //= 1
l!l
6106
MC/DC100%1/1
$args{"validate_args"} //= 1
$args{"validate_args"}
Truth table100%2/2
A
A= $args{"validate_args"}
Aresult
11
00
1087 $args{validate_result} //= 0
1088 # function might want to disable validate_result by default, e.g. if
1089 # source code has been processed with
1090 # Dist::Zilla::Plugin::Rinci::Validate
1091 112 ▶ if $meta->{'x.perinci.sub.wrapper.disable_validate_result'};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"x.perinci.sub.wrapper.disable_validate_result"} 2 110 112
Condition50%1/2
$args{"validate_result"} //= 0
l!l
02
MC/DC0%0/1
$args{"validate_result"} //= 0
$args{"validate_result"}
Truth table50%1/2
A
A= $args{"validate_result"}
Aresult
11
00
1092 $args{validate_result} //= 0
1093 # by default do not validate result again if previous wrapper(s) have
1094 # already done it
1095 112 ▶ if (grep {$_->{validate_result}} @$wrap_logs);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"validate_result"};} @$wrap_logs 2 110 112
Condition50%1/2
$args{"validate_result"} //= 0
l!l
02
MC/DC0%0/1
$args{"validate_result"} //= 0
$args{"validate_result"}
Truth table50%1/2
A
A= $args{"validate_result"}
Aresult
11
00
1096 112 ▶ $args{validate_result} //= 1;
Condition100%2/2
$args{"validate_result"} //= 1
l!l
6106
MC/DC100%1/1
$args{"validate_result"} //= 1
$args{"validate_result"}
Truth table100%2/2
A
A= $args{"validate_result"}
Aresult
11
00
1097 112 ▶ $args{core} //= $ENV{PERINCI_WRAPPER_CORE};
Condition33%1/3
$args{"core"} //= $ENV{"PERINCI_WRAPPER_CORE"}
l!l&&r!l&&!r
00112
MC/DC0%0/2
$args{"core"} //= $ENV{"PERINCI_WRAPPER_CORE"}
$args{"core"} $ENV{"PERINCI_WRAPPER_CORE"}
Truth table33%1/3
A //= B
A= $args{"core"}B= $ENV{"PERINCI_WRAPPER_CORE"}
ABresult
1X1
011
000
1098 112 ▶ $args{core_or_pp} //= $ENV{PERINCI_WRAPPER_CORE_OR_PP};
Condition33%1/3
$args{"core_or_pp"} //= $ENV{"PERINCI_WRAPPER_CORE_OR_PP"}
l!l&&r!l&&!r
00112
MC/DC0%0/2
$args{"core_or_pp"} //= $ENV{"PERINCI_WRAPPER_CORE_OR_PP"}
$args{"core_or_pp"} $ENV{"PERINCI_WRAPPER_CORE_OR_PP"}
Truth table33%1/3
A //= B
A= $args{"core_or_pp"}B= $ENV{"PERINCI_WRAPPER_CORE_OR_PP"}
ABresult
1X1
011
000
1099 112 ▶ $args{pp} //= $ENV{PERINCI_WRAPPER_PP};
Condition33%1/3
$args{"pp"} //= $ENV{"PERINCI_WRAPPER_PP"}
l!l&&r!l&&!r
00112
MC/DC0%0/2
$args{"pp"} //= $ENV{"PERINCI_WRAPPER_PP"}
$args{"pp"} $ENV{"PERINCI_WRAPPER_PP"}
Truth table33%1/3
A //= B
A= $args{"pp"}B= $ENV{"PERINCI_WRAPPER_PP"}
ABresult
1X1
011
000
1100  
1101 112 my $sub_ref_name;
1102 # if sub_name is not provided, create a unique name for it. it is needed by
1103 # the wrapper-generated code (e.g. printing error messages)
1104 112 ▶ if (!$sub_name || $sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sub_name or $sub) 110 2 112
Condition66%2/3
not $sub_name or $sub
l!l&&r!l&&!r
11002
MC/DC50%1/2
not $sub_name or $sub
not $sub_name $sub
Truth table66%2/3
not(A) or B
A= not $sub_nameB= $sub
ABresult
1X1
011
000
1105 110 my $n = $comppkg . "::sub".Scalar::Util::refaddr($sub);
1106 16 ▶ no strict 'refs'; no warnings; ${$n} = $sub; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1107 110 ▶ if (!$sub_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub_name) 110 0 110
1108 110 $args{sub_name} = $sub_name = '$' . $n;
1109 }
1110 110 $sub_ref_name = '$' . $n;
1111 }
1112 # if meta name is not provided, we store the meta somewhere, it is needed by
1113 # the wrapper-generated code (e.g. deps clause).
1114 112 ▶ if (!$meta_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta_name) 112 0 112
1115 112 my $n = $comppkg . "::meta".Scalar::Util::refaddr($meta);
1116 16 ▶ no strict 'refs'; no warnings; ${$n} = $meta; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1117 112 $args{meta_name} = $meta_name = '$' . $n;
1118 }
1119  
1120 # shallow copy
1121 112 my $opt_cvt = { %{ $args{convert} } };
1122 112 my $opt_sin = $args{_schema_is_normalized};
1123  
1124 112 ▶ $meta = normalize_function_metadata($meta)
Branch: 1 branch
BranchTrueFalseTotal
unless $opt_sin 108 4 112
1125 unless $opt_sin;
1126  
1127 108 $self->_reset_work_data(_args=>\%args, _meta=>$meta);
1128  
1129 # add properties from convert, if not yet mentioned in meta
1130 108 for (keys %$opt_cvt) {
1131 10 ▶ $meta->{$_} = undef unless exists $meta->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $meta->{$_} 2 8 10
1132 }
1133  
1134 # mark in the metadata that we have done the wrapping, so future wrapping
1135 # can avoid needless duplicated functionality (like validating args twice).
1136 # note that handler can log their mark too.
1137 {
1138 108 ▶ my @wrap_log = @{ $meta->{$wrap_log_prop} // [] };
Condition100%2/2
$meta->{$wrap_log_prop} // []
l!l
2106
MC/DC100%1/1
$meta->{$wrap_log_prop} // []
$meta->{$wrap_log_prop}
Truth table100%2/2
A
A= $meta->{$wrap_log_prop}
Aresult
11
00
1139 push @wrap_log, {
1140 validate_args => $args{validate_args},
1141 validate_result => $args{validate_result},
1142 108 normalize_schema => !$opt_sin,
1143 };
1144 108 ▶ if ($args{log}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"log"}) 106 2 108
1145 106 $meta->{$wrap_log_prop} = \@wrap_log;
1146 }
1147 }
1148  
1149 # start iterating over properties
1150  
1151 108 $self->select_section('OPEN_SUB');
1152 108 $self->push_lines(
1153 "package $comppkg;", 'sub {');
1154 108 $self->indent;
1155  
1156 108 ▶ $meta->{args_as} //= "hash";
Condition100%2/2
$meta->{"args_as"} //= "hash"
l!l
1296
MC/DC100%1/1
$meta->{"args_as"} //= "hash"
$meta->{"args_as"}
Truth table100%2/2
A
A= $meta->{"args_as"}
Aresult
11
00
1157  
1158 108 ▶ if ($meta->{args_as} =~ /hash/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"args_as"} =~ /hash/) 102 6 108
1159 102 $self->select_section('before_call_after_arg_validation');
1160 # tell function it's being wrapped, currently disabled
1161 #$self->push_lines('$args{-wrapped} = 1;');
1162 }
1163  
1164 108 my %props = map {$_=>1} keys %$meta;
1165 108 $props{$_} = 1 for keys %$opt_cvt;
1166  
1167 # collect and check handlers
1168 108 my %handler_args;
1169 my %handler_metas;
1170 108 for my $k0 (keys %props) {
1171 434 my $k = $k0;
1172 434 $k =~ s/\..+//;
1173 434 ▶ next if $k =~ /\A_/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /\A_/ 2 432 434
1174 432 ▶ next if $handler_args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $handler_args{$k} 0 432 432
1175 #if (grep { $_ eq $k } @{ $self->{_args}{skip} }) {
1176 # $log->tracef("Skipped property %s (mentioned in skip)", $k);
1177 # next;
1178 #}
1179 432 ▶ return [500, "Invalid property name $k"] unless $k =~ /\A\w+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /\A\w+\z/ 0 432 432
1180 432 my $meth = "handlemeta_$k";
1181 432 ▶ unless ($self->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($meth)) 0 432 432
1182 # try a property module first
1183 0 require "Perinci/Sub/Property/$k.pm"; ## no critic: Modules::RequireBarewordIncludes
1184 0 ▶ unless ($self->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($meth)) 0 0 0
1185 0 return [500, "No handler for property $k0 ($meth)"];
1186 }
1187 }
1188 432 my $hm = $self->$meth;
1189 432 ▶ $hm->{v} //= 1;
Condition100%2/2
$hm->{"v"} //= 1
l!l
214218
MC/DC100%1/1
$hm->{"v"} //= 1
$hm->{"v"}
Truth table100%2/2
A
A= $hm->{"v"}
Aresult
11
00
1190 432 ▶ next unless defined $hm->{prio};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hm->{"prio"} 218 214 432
1191 die "Please update property handler $k which is still at v=$hm->{v} ".
1192 "(needs v=$protocol_version)"
1193 214 ▶ unless $hm->{v} == $protocol_version;
Branch: 1 branch
BranchTrueFalseTotal
unless $hm->{"v"} == $Perinci::Sub::Wrapper::protocol_version 0 214 214
1194 my $ha = {
1195 214 prio=>$hm->{prio}, value=>$meta->{$k0}, property=>$k0,
1196 meth=>"handle_$k",
1197 };
1198 214 ▶ if (exists $opt_cvt->{$k0}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt_cvt->{$k0}) 10 204 214
1199 return [501, "Property '$k0' does not support conversion"]
1200 10 ▶ unless $hm->{convert};
Branch: 1 branch
BranchTrueFalseTotal
unless $hm->{"convert"} 0 10 10
1201 10 $ha->{new} = $opt_cvt->{$k0};
1202 10 $meta->{$k0} = $opt_cvt->{$k0};
1203 }
1204 214 $handler_args{$k} = $ha;
1205 214 $handler_metas{$k} = $hm;
1206 }
1207  
1208 # call all the handlers in order
1209 108 for my $k (sort {$handler_args{$a}{prio} <=> $handler_args{$b}{prio}}
1210 keys %handler_args) {
1211 214 my $ha = $handler_args{$k};
1212 214 my $meth = $ha->{meth};
1213 214 local $self->{_cur_handler} = $meth;
1214 214 local $self->{_cur_handler_meta} = $handler_metas{$k};
1215 214 local $self->{_cur_handler_args} = $ha;
1216 214 $self->$meth(args=>\%args, meta=>$meta, %$ha);
1217 }
1218  
1219 104 my $needs_store_res = $self->_needs_store_res;
1220 104 ▶ if ($needs_store_res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_store_res) 98 6 104
1221 98 $self->_add_var('_w_res');
1222 }
1223  
1224 104 $self->select_section('CALL');
1225 104 ▶ my $sn = $sub_ref_name // $sub_name;
Condition66%2/3
$sub_ref_name // $sub_name
l!l&&r!l&&!r
10220
MC/DC0%0/2
$sub_ref_name // $sub_name
$sub_ref_name $sub_name
Truth table66%2/3
A // B
A= $sub_ref_nameB= $sub_name
ABresult
1X1
011
000
1226 $self->push_lines(
1227 ($needs_store_res ? '$_w_res = ' : "") .
1228 $sn. ($sn =~ /^\$/ ? "->" : "").
1229 104 ▶ "(".$self->{_args_token}.");");
Branch: 2 branches
BranchTrueFalseTotal
$needs_store_res ? : 98 6 104
$sn =~ /^\$/ ? : 102 2 104
1230 104 ▶ if ($args{validate_result}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"validate_result"}) 98 6 104
1231 98 $self->select_section('after_call_before_res_validation');
1232 98 ▶ unless ($meta->{result_naked}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->{"result_naked"}) 96 2 98
1233 96 $self->push_lines(
1234 '',
1235 '# check that sub produces enveloped result',
1236 'unless (ref($_w_res) eq "ARRAY" && $_w_res->[0]) {',
1237 );
1238 96 $self->indent;
1239 96 ▶ if (log_is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if (log_is_trace()) { } 0 96 96
1240 0 $self->_add_module('Data::Dumper');
1241 0 $self->push_lines(
1242 'local $Data::Dumper::Purity = 1;',
1243 'local $Data::Dumper::Terse = 1;',
1244 'local $Data::Dumper::Indent = 0;',
1245 );
1246 0 $self->_err(500,
1247 qq['BUG: Sub $sub_name does not produce envelope: '.].
1248 qq[Data::Dumper::Dumper(\$_w_res)]);
1249 } else {
1250 96 $self->_err(500,
1251 qq['BUG: Sub $sub_name does not produce envelope']);
1252 }
1253 96 $self->unindent;
1254 96 $self->push_lines('}');
1255 }
1256 }
1257  
1258 104 my $use_eval = $self->_needs_eval;
1259 104 ▶ if ($use_eval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_eval) 0 104 104
1260 0 $self->select_section('CLOSE_EVAL');
1261 0 $self->push_lines('return $_w_res;');
1262 0 $self->unindent;
1263 0 $self->_add_var('_w_eval_err');
1264 0 $self->push_lines(
1265 '};',
1266 '$_w_eval_err = $@;');
1267  
1268 # _needs_eval will automatically be enabled here, due after_eval being
1269 # filled
1270 0 $self->select_section('after_eval');
1271 0 $self->push_lines('warn $_w_eval_err if $_w_eval_err;');
1272 0 $self->_errif(500, '"Function died: $_w_eval_err"', '$_w_eval_err');
1273  
1274 0 $self->select_section('OPEN_EVAL');
1275 0 $self->push_lines('eval {');
1276 0 $self->indent;
1277 }
1278  
1279 # return sub result
1280 104 $self->select_section('BEFORE_CLOSE_SUB');
1281 104 ▶ $self->push_lines('return $_w_res;') if $needs_store_res;
Branch: 1 branch
BranchTrueFalseTotal
if $needs_store_res 98 6 104
1282 104 $self->select_section('CLOSE_SUB');
1283 104 $self->unindent;
1284 104 $self->push_lines('}'); # wrapper sub
1285  
1286 # return wrap result
1287 104 my $result = {
1288 sub_name => $sub_name,
1289 sub_ref_name => $sub_ref_name,
1290 meta => $meta,
1291 meta_name => $meta_name,
1292 use_eval => $use_eval,
1293 };
1294 104 ▶ if ($args{embed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'embed'}) { } 52 52 104
1295 52 $result->{source} = $self->_format_embed_wrapper_code;
1296 } else {
1297 52 my $source = $self->_format_dyn_wrapper_code;
1298 52 ▶ if ($Log_Wrapper_Code && log_is_trace()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perinci::Sub::Wrapper::Log_Wrapper_Code and log_is_trace()) 0 52 52
Condition33%1/3
$Perinci::Sub::Wrapper::Log_Wrapper_Code and log_is_trace()
!ll&&!rl&&r
5200
MC/DC0%0/2
$Perinci::Sub::Wrapper::Log_Wrapper_Code and log_is_trace()
$Perinci::Sub::Wrapper::Log_Wrapper_Code log_is_trace()
Truth table33%1/3
A and B
A= $Perinci::Sub::Wrapper::Log_Wrapper_CodeB= log_is_trace()
ABresult
0X0
100
111
1299 0 require String::LineNumber;
1300 log_trace("wrapper code:\n%s",
1301 0 ▶ $ENV{LINENUM} // 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'LINENUM'} // 1 ? : 0 0 0
Condition0%0/2
$ENV{"LINENUM"} // 1
l!l
00
MC/DC0%0/1
$ENV{"LINENUM"} // 1
$ENV{"LINENUM"}
Truth table0%0/2
A
A= $ENV{"LINENUM"}
Aresult
11
00
1302 String::LineNumber::linenum($source) :
1303 $source);
1304 }
1305 52 $result->{source} = $source;
1306 52 ▶ if ($args{compile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"compile"}) 52 0 52
1307 52 ▶ my $wrapped = eval $source; ## no critic: BuiltinFunctions::ProhibitStringyEval
Subroutine BEGIN: called CC=1 SCAR=0.0
1308 52 ▶ die "BUG: Wrapper code can't be compiled: $@" if $@ || !$wrapped;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $wrapped 0 52 52
Condition33%1/3
$@ or not $wrapped
l!l&&r!l&&!r
0052
MC/DC0%0/2
$@ or not $wrapped
$@ not $wrapped
Truth table33%1/3
A or not(B)
A= $@B= not $wrapped
ABresult
1X1
011
000
1309 52 $result->{sub} = $wrapped;
1310 }
1311 }
1312  
1313 104 [200, "OK", $result];
1314 }
1315  
1316 $SPEC{wrap_sub} = {
1317 15 v => 1.1,
1318 summary => 'Wrap subroutine to do various things, '.
1319 'like enforcing Rinci properties',
1320 result => {
1321 summary => 'The wrapped subroutine along with its new metadata',
1322 description => <<'_',
1323  
1324 Aside from wrapping the subroutine, the wrapper will also create a new metadata
1325 for the subroutine. The new metadata is a clone of the original, with some
1326 properties changed, e.g. schema in `args` and `result` normalized, some values
1327 changed according to the `convert` argument, some defaults set, etc.
1328  
1329 The new metadata will also contain (or append) the wrapping log located in the
1330 `x.perinci.sub.wrapper.logs` attribute. The wrapping log marks that the wrapper
1331 has added some functionality (like validating arguments or result) so that
1332 future nested wrapper can choose to avoid duplicating the same functionality.
1333  
1334 _
1335 schema=>['hash*'=>{keys=>{
1336 sub=>'code*',
1337 source=>['any*' => of => ['str*', ['hash*' => each_value=>'str*']]],
1338 meta=>'hash*',
1339 }}],
1340 },
1341 args => {
1342 sub => {
1343 schema => 'str*',
1344 summary => 'The code to be wrapped',
1345 description => <<'_',
1346  
1347 At least one of `sub` or `sub_name` must be specified.
1348  
1349 _
1350 },
1351 sub_name => {
1352 schema => 'str*',
1353 summary => 'The name of the subroutine, '.
1354 'e.g. func or Foo::func (qualified)',
1355 description => <<'_',
1356  
1357 At least one of `sub` or `sub_name` must be specified.
1358  
1359 _
1360 },
1361 meta => {
1362 schema => 'hash*',
1363 summary => 'The function metadata',
1364 req => 1,
1365 },
1366 meta_name => {
1367 schema => 'str*',
1368 summary => 'Where to find the metadata, e.g. "$SPEC{foo}"',
1369 description => <<'_',
1370  
1371 Some wrapper code (e.g. handler for `dep` property) needs to refer to the
1372 function metadata. If not provided, the wrapper will store the function metadata
1373 in a unique variable (e.g. `$Perinci::Sub::Wrapped::meta34127816`).
1374  
1375 _
1376 },
1377 convert => {
1378 schema => 'hash*',
1379 summary => 'Properties to convert to new value',
1380 description => <<'_',
1381  
1382 Not all properties can be converted, but these are a partial list of those that
1383 can: v (usually do not need to be specified when converting from 1.0 to 1.1,
1384 will be done automatically), args_as, result_naked, default_lang.
1385  
1386 _
1387 },
1388 compile => {
1389 schema => ['bool' => {default=>1}],
1390 summary => 'Whether to compile the generated wrapper',
1391 description => <<'_',
1392  
1393 Can be set to 0 to not actually wrap but just return the generated wrapper
1394 source code.
1395  
1396 _
1397 },
1398 compile => {
1399 schema => ['bool' => {default=>1}],
1400 summary => 'Whether to compile the generated wrapper',
1401 description => <<'_',
1402  
1403 Can be set to 0 to not actually wrap but just return the generated wrapper
1404 source code.
1405  
1406 _
1407 },
1408 debug => {
1409 schema => [bool => {default=>0}],
1410 summary => 'Generate code with debugging',
1411 description => <<'_',
1412  
1413 If turned on, will produce various debugging in the generated code. Currently
1414 what this does:
1415  
1416 * add more comments (e.g. for each property handler)
1417  
1418 _
1419 },
1420 validate_args => {
1421 schema => ['bool'],
1422 summary => 'Whether wrapper should validate arguments',
1423 description => <<'_',
1424  
1425 If set to true, will validate arguments. Validation error will cause status 400
1426 to be returned. The default is to enable this unless previous wrapper(s) have
1427 already done this.
1428  
1429 _
1430 },
1431 validate_result => {
1432 schema => ['bool'],
1433 summary => 'Whether wrapper should validate arguments',
1434 description => <<'_',
1435  
1436 If set to true, will validate sub's result. Validation error will cause wrapper
1437 to return status 500 instead of sub's result. The default is to enable this
1438 unless previous wrapper(s) have already done this.
1439  
1440 _
1441 },
1442 core => {
1443 summary => 'If set to true, will avoid the use of non-core modules',
1444 schema => 'bool',
1445 },
1446 core_or_pp => {
1447 summary => 'If set to true, will avoid the use of non-core XS modules',
1448 schema => 'bool',
1449 description => <<'_',
1450  
1451 In other words, will stick to core or pure-perl modules only.
1452  
1453 _
1454 },
1455 pp => {
1456 summary => 'If set to true, will avoid the use of XS modules',
1457 schema => 'bool',
1458 },
1459 },
1460 };
1461 112 ▶sub wrap_sub {
Subroutine wrap_sub: called CC=1 SCAR=0.0
Pod documented
1462 112 __PACKAGE__->new->wrap(@_);
1463 }
1464  
1465 15 1;
1466 # ABSTRACT: A multi-purpose subroutine wrapping framework
1467  
1468 __END__
↑ Summary Test/Perinci/Sub/Wrapper.pm »