blib/lib/Acme/Signature/Arity.pm

filter: Statementstmt 100% 31 / 31 Branchbran 75.0% 6 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 92.7% 51 / 55 SCAR 17.9
CC
6
Cov
93%
arity
5.2
max_arity
2.0
BEGIN
1.0
CRAP 6.0
↑ Summary
1 package Acme::Signature::Arity;
2 # ABSTRACT: find out how a piece of code expects to be called
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.001';
8 2 our $AUTHORITY;
9  
10 2 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Acme::Signature::Arity - provides reliable, production-ready signature introspection
18  
19 =head1 DESCRIPTION
20  
21 You'll know if you need this.
22  
23 If you're just curious, perhaps start with L<https://www.nntp.perl.org/group/perl.perl5.porters/2021/11/msg262009.html>.
24  
25 No part of this is expected to work in any way when given a sub that has a prototype.
26 There are other tools for those: L<Sub::Util>.
27  
28 For subs that don't have a prototype, this is I<also> not expected to work. It might help
29 demonstrate where to look if you wanted to write something proper, though.
30  
31 =cut
32  
33 2 our @EXPORT_OK = qw(arity min_arity max_arity);
34 2 our @EXPORT = @EXPORT_OK;
35  
36 =head1 Exported functions
37  
38 =head2 arity
39  
40 Returns the C<UNOP_aux> details for the first opcode for a coderef CV.
41 If that code uses signatures, this might give you some internal details
42 which mean something about the expected parameters.
43  
44 Expected return information, as a list:
45  
46 =over 4
47  
48 =item * number of required scalar parameters
49  
50 =item * number of optional scalar parameters (probably because there are defaults)
51  
52 =item * a character representing the slurping behaviour, might be '@' or '%', or nothing (undef?) if it's
53 just a fixed list of scalar parameters
54  
55 =back
56  
57 This can also throw exceptions. That should only happen if you give it something that isn't
58 a coderef, or if internals change enough that the entirely-unjustified assumptions made by
59 this module are somehow no longer valid. Maybe they never were in the first place.
60  
61 =cut
62  
63 ▶sub arity ($code) {
Subroutine arity: called CC=5 SCAR=16.5
Pod documented
64 23 ▶ die 'only works on coderefs' unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 23 23
65 23 my $cv = B::svref_2object($code);
66 23 ▶ die 'probably not a coderef' unless $cv->isa('B::CV');
Branch: 1 branch
BranchTrueFalseTotal
unless $cv->isa("B::CV") 0 23 23
67 23 my $next = $cv->START->next;
68 # we pretend sub { } is sub (@) { }, for convenience
69 23 ▶ return (0, 0, '@') unless $next and $next->isa('B::UNOP_AUX');
Branch: 1 branch
BranchTrueFalseTotal
unless $next and $next->isa("B::UNOP_AUX") 2 21 23
Condition66%2/3
$next and $next->isa("B::UNOP_AUX")
!ll&&!rl&&r
0221
MC/DC50%1/2
$next and $next->isa("B::UNOP_AUX")
$next $next->isa("B::UNOP_AUX")
Truth table66%2/3
A and B
A= $nextB= $next->isa("B::UNOP_AUX")
ABresult
0X0
100
111
70 21 return $next->aux_list($cv);
71 }
72  
73 =head2 max_arity
74  
75 Takes a coderef, returns a number or C<undef>.
76  
77 If the code uses signatures, this tells you how many parameters you could
78 pass when calling before it complains - C<undef> means unlimited.
79  
80 Should also work when there are no signatures, just gives C<undef> again.
81  
82 =cut
83  
84 ▶sub max_arity ($code) {
Subroutine max_arity: called CC=2 SCAR=6.9
Pod documented
85 8 my ($minimum, $optional, $slurp) = arity($code);
86 8 ▶ return undef if $slurp;
Branch: 1 branch
BranchTrueFalseTotal
if $slurp 5 3 8
87 3 return $minimum
88 }
89  
90 =head2 min_arity
91  
92 Takes a coderef, returns a number or C<undef>.
93  
94 If the code uses signatures, this tells you how many parameters you need to
95 pass when calling - 0 means that no parameters are required.
96  
97 Should also work when there are no signatures, returning 0 in that case.
98  
99 =cut
100  
101 ▶sub min_arity ($code) {
Subroutine min_arity: called CC=1 SCAR=0.0
Pod documented
102 8 my ($minimum, $optional, $slurp) = arity($code);
103 8 return $minimum - $optional;
104 }
105  
106 2 1;
107  
108 __END__
↑ Summary