blib/lib/Acme/Signature/Arity.pm

filter: Statementstmt 87.1% 34 / 39 Branchbran 33.3% 4 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 9 / 11 Podpod 100% 4 / 4 totaltotal 73.2% 52 / 71 CC 8 SCAR 12.8
CC
8
Cov
72%
coderef_ignoring_extra
6.0
__ANON__
6.0
arity
5.9
CRAP 3.6
↑ 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.002';
8 2 our $AUTHORITY;
9  
10 2 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Acme::Signature::Arity - provides reliable, production-ready signature introspection
19  
20 =head1 DESCRIPTION
21  
22 You'll know if you need this.
23  
24 If you're just curious, perhaps start with L<https://www.nntp.perl.org/group/perl.perl5.porters/2021/11/msg262009.html>.
25  
26 No part of this is expected to work in any way when given a sub that has a prototype.
27 There are other tools for those: L<Sub::Util>.
28  
29 For subs that don't have a prototype, this is I<also> not expected to work. It might help
30 demonstrate where to look if you wanted to write something proper, though.
31  
32 =cut
33  
34 2 our @EXPORT_OK = qw(arity min_arity max_arity coderef_ignoring_extra);
35 2 our @EXPORT = qw(min_arity max_arity);
36  
37 =head1 Exported functions
38  
39 =head2 arity
40  
41 Returns the C<UNOP_aux> details for the first opcode for a coderef CV.
42 If that code uses signatures, this might give you some internal details
43 which mean something about the expected parameters.
44  
45 Expected return information, as a list:
46  
47 =over 4
48  
49 =item * number of required scalar parameters
50  
51 =item * number of optional scalar parameters (probably because there are defaults)
52  
53 =item * a character representing the slurping behaviour, might be '@' or '%', or nothing (undef?) if it's
54 just a fixed list of scalar parameters
55  
56 =back
57  
58 This can also throw exceptions. That should only happen if you give it something that isn't
59 a coderef, or if internals change enough that the entirely-unjustified assumptions made by
60 this module are somehow no longer valid. Maybe they never were in the first place.
61  
62 =cut
63  
64 ▶sub arity ($code) {
Subroutine arity: called CC=5 SCAR=17.8
Pod documented
65 3 ▶ die 'only works on coderefs' unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 3 3
66 3 my $cv = B::svref_2object($code);
67 3 ▶ die 'probably not a coderef' unless $cv->isa('B::CV');
Branch: 1 branch
BranchTrueFalseTotal
unless $cv->isa("B::CV") 0 3 3
68 3 my $next = $cv->START->next;
69 # we pretend sub { } is sub (@) { }, for convenience
70 3 ▶ return (0, 0, '@') unless $next and $next->isa('B::UNOP_AUX');
Branch: 1 branch
BranchTrueFalseTotal
unless $next and $next->isa("B::UNOP_AUX") 0 3 3
Condition33%1/3
$next and $next->isa("B::UNOP_AUX")
!ll&&!rl&&r
003
MC/DC0%0/2
$next and $next->isa("B::UNOP_AUX")
$next $next->isa("B::UNOP_AUX")
Truth table33%1/3
A and B
A= $nextB= $next->isa("B::UNOP_AUX")
ABresult
0X0
100
111
71 3 return $next->aux_list($cv);
72 }
73  
74 =head2 max_arity
75  
76 Takes a coderef, returns a number or C<undef>.
77  
78 If the code uses signatures, this tells you how many parameters you could
79 pass when calling before it complains - C<undef> means unlimited.
80  
81 Should also work when there are no signatures, just gives C<undef> again.
82  
83 =cut
84  
85 ▶sub max_arity ($code) {
Subroutine max_arity: called CC=2 SCAR=7.1
Pod documented
86 1 my ($scalars, $optional, $slurp) = arity($code);
87 1 ▶ return undef if $slurp;
Branch: 1 branch
BranchTrueFalseTotal
if $slurp 0 1 1
88 1 return $scalars
89 }
90  
91 =head2 min_arity
92  
93 Takes a coderef, returns a number or C<undef>.
94  
95 If the code uses signatures, this tells you how many parameters you need to
96 pass when calling - 0 means that no parameters are required.
97  
98 Should also work when there are no signatures, returning 0 in that case.
99  
100 =cut
101  
102 ▶sub min_arity ($code) {
Subroutine min_arity: called CC=1 SCAR=0.0
Pod documented
103 2 my ($scalars, $optional, $slurp) = arity($code);
104 2 return $scalars - $optional;
105 }
106  
107 =head2 coderef_ignoring_extra
108  
109 Given a coderef, returns a coderef (either the original or wrapped)
110 which won't complain if you try to pass more parameters than it was expecting.
111  
112 This is intended for library authors in situations like this:
113  
114 $useful_library->each(sub ($item) { say "item here: $item" });
115  
116 where you later want to add optional new parameters, and don't trust your users
117 to include the mandatory C<< , @ >> signature definition that indicates excess
118 parameters can be dropped.
119  
120 Usage - let's say your first library version looked like this:
121  
122 sub each ($self, $callback) {
123 my $code = $callback;
124 for my $item ($self->{items}->@*) {
125 $code->($item);
126 }
127 }
128  
129 and you later want to pass the index as an extra parameter, without breaking existing code
130 that assumed there would only ever be one callback parameter...
131  
132 sub each ($self, $callback) {
133 my $code = coderef_ignoring_extra($callback);
134 for my $idx (0..$#{$self->{items}}) {
135 $code->($self->{items}{$idx}, $idx);
136 }
137 }
138  
139 Your library is now at least somewhat backwards-compatible, without sacrificing too
140 many signature-related arity checking features: code expecting the new version
141 will still complain if required parameters are not provided.
142  
143 =cut
144  
145 ▶sub coderef_ignoring_extra ($code) {
Subroutine coderef_ignoring_extra: not called CC=2 SCAR=17.9
Pod documented
146 0 my ($scalars, $optional, $slurp) = arity($code);
147 # If we're accepting unlimited parameters, no need to do any more work
148 0 ▶ return $code if $slurp;
Branch: 1 branch
BranchTrueFalseTotal
if $slurp 0 0 0
149  
150 0 my $max_index = $scalars - 1;
151 ▶ return sub (@args) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
152 # Some parameters may be optional, so we allow shorter lists as well
153 0 ▶ $code->(@args ? @args[0 .. min($#args, $max_index)] : ());
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
154 }
155 0 }
156  
157 2 1;
158  
159 __END__
↑ Summary