fix typo
[p5sagit/Function-Parameters.git] / lib / Function / Parameters / Info.pm
CommitLineData
53c979f0 1package Function::Parameters::Info;
2
3use v5.14.0;
4
5use warnings;
6
7use Moo;
8
9our $VERSION = '0.01';
10
11my @pn_ro = glob '{positional,named}_{required,optional}';
12
13for my $attr (qw[keyword invocant slurpy], map "_$_", @pn_ro) {
14 has $attr => (
15 is => 'ro',
16 );
17}
18
19for my $gen (join "\n", map "sub $_ { \@{\$_[0]->_$_} }", @pn_ro) {
20 eval "$gen\n1" or die $@;
21}
22
ebdc721b 23sub args_min {
24 my $self = shift;
25 my $r = 0;
26 $r++ if defined $self->invocant;
27 $r += $self->positional_required;
28 $r += $self->named_required * 2;
29 $r
30}
31
32sub args_max {
33 my $self = shift;
34 return 0 + 'Inf' if defined $self->slurpy || $self->named_required || $self->named_optional;
35 my $r = 0;
36 $r++ if defined $self->invocant;
37 $r += $self->positional_required;
38 $r += $self->positional_optional;
39 $r
40}
41
53c979f0 42'ok'
43
44__END__
ebdc721b 45
46=encoding UTF-8
47
48=head1 NAME
49
50Function::Parameters::Info - Information about parameter lists
51
52=head1 SYNOPSIS
53
54 use Function::Parameters;
55
56 fun foo($x, $y, :$hello, :$world = undef) {}
57
58 my $info = Function::Parameters::info \&foo;
59 my $p0 = $info->invocant; # undef
60 my @p1 = $info->positional_required; # ('$x', '$y')
61 my @p2 = $info->positional_optional; # ()
62 my @p3 = $info->named_required; # ('$hello')
63 my @p4 = $info->named_optional; # ('$world')
64 my $p5 = $info->slurpy; # undef
65 my $min = $info->args_min; # 4
66 my $max = $info->args_max; # inf
67
68 my $invocant = Function::Parameters::info(method () { 42 })->invocant; # '$self'
69
70 my $slurpy = Function::Parameters::info(fun {})->slurpy; # '@_'
71
72=head1 DESCRIPTION
73
74L<C<Function::Parameters::info>|Function::Parameters/Introspection> returns
75objects of this class to describe parameter lists of functions. The following
76methods are available:
77
0d87c016 78=head2 $info->invocant
ebdc721b 79
80Returns the name of the variable into which the first argument is
420e3b82 81L<C<shift>|perlfunc/shift>ed automatically, or C<undef> if no such thing
ebdc721b 82exists. This will usually return C<'$self'> for methods.
83
0d87c016 84=head2 $info->positional_required
ebdc721b 85
86Returns a list of the names of the required positional parameters (or a count
87in scalar context).
88
0d87c016 89=head2 $info->positional_optional
ebdc721b 90
91Returns a list of the names of the optional positional parameters (or a count
92in scalar context).
93
0d87c016 94=head2 $info->named_required
ebdc721b 95
96Returns a list of the names of the required named parameters (or a count
97in scalar context).
98
0d87c016 99=head2 $info->named_optional
ebdc721b 100
101Returns a list of the names of the optional named parameters (or a count
102in scalar context).
103
0d87c016 104=head2 $info->slurpy
ebdc721b 105
106Returns the name of the final array or hash that gobbles up all remaining
107arguments, or C<undef> if no such thing exists.
108
109As a special case, functions defined without an explicit parameter list (i.e.
110without C<( )>) will return C<'@_'> here because they accept any number of
111arguments.
112
0d87c016 113=head2 $info->args_min
ebdc721b 114
115Returns the minimum number of arguments this function requires. This is
116computed as follows: Invocant and required positional parameters count 1 each.
117Optional parameters don't count. Required named parameters count 2 each (key +
118value). Slurpy parameters don't count either because they accept empty lists.
119
0d87c016 120=head2 $info->args_max
ebdc721b 121
122Returns the maximum number of arguments this function accepts. This is computed
123as follows: If there is any named or slurpy parameter, the result is C<Inf>.
124Otherwise the result is the sum of all invocant and positional parameters.
125
126=head1 SEE ALSO
127
128L<Function::Parameters>
129
130=head1 AUTHOR
131
132Lukas Mai, C<< <l.mai at web.de> >>
133
134=head1 COPYRIGHT & LICENSE
135
136Copyright 2012 Lukas Mai.
137
138This program is free software; you can redistribute it and/or modify it
139under the terms of either: the GNU General Public License as published
140by the Free Software Foundation; or the Artistic License.
141
142See http://dev.perl.org/licenses/ for more information.
143
144=cut