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