1 package Function::Parameters::Info;
11 my @pn_ro = glob '{positional,named}_{required,optional}';
13 for my $attr (qw[keyword invocant slurpy], map "_$_", @pn_ro) {
19 for my $gen (join "\n", map "sub $_ { \@{\$_[0]->_$_} }", @pn_ro) {
20 eval "$gen\n1" or die $@;
26 $r++ if defined $self->invocant;
27 $r += $self->positional_required;
28 $r += $self->named_required * 2;
34 return 0 + 'Inf' if defined $self->slurpy || $self->named_required || $self->named_optional;
36 $r++ if defined $self->invocant;
37 $r += $self->positional_required;
38 $r += $self->positional_optional;
50 Function::Parameters::Info - Information about parameter lists
54 use Function::Parameters;
56 fun foo($x, $y, :$hello, :$world = undef) {}
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
68 my $invocant = Function::Parameters::info(method () { 42 })->invocant; # '$self'
70 my $slurpy = Function::Parameters::info(fun {})->slurpy; # '@_'
74 L<C<Function::Parameters::info>|Function::Parameters/Introspection> returns
75 objects of this class to describe parameter lists of functions. The following
76 methods are available:
78 =head2 $info->invocant
80 Returns the name of the variable into which the first argument is
81 L<C<shift>|perlfunc/shift>ed automatically, or C<undef> if no such thing
82 exists. This will usually return C<'$self'> for methods.
84 =head2 $info->positional_required
86 Returns a list of the names of the required positional parameters (or a count
89 =head2 $info->positional_optional
91 Returns a list of the names of the optional positional parameters (or a count
94 =head2 $info->named_required
96 Returns a list of the names of the required named parameters (or a count
99 =head2 $info->named_optional
101 Returns a list of the names of the optional named parameters (or a count
106 Returns the name of the final array or hash that gobbles up all remaining
107 arguments, or C<undef> if no such thing exists.
109 As a special case, functions defined without an explicit parameter list (i.e.
110 without C<( )>) will return C<'@_'> here because they accept any number of
113 =head2 $info->args_min
115 Returns the minimum number of arguments this function requires. This is
116 computed as follows: Invocant and required positional parameters count 1 each.
117 Optional parameters don't count. Required named parameters count 2 each (key +
118 value). Slurpy parameters don't count either because they accept empty lists.
120 =head2 $info->args_max
122 Returns the maximum number of arguments this function accepts. This is computed
123 as follows: If there is any named or slurpy parameter, the result is C<Inf>.
124 Otherwise the result is the sum of all invocant and positional parameters.
128 L<Function::Parameters>
132 Lukas Mai, C<< <l.mai at web.de> >>
134 =head1 COPYRIGHT & LICENSE
136 Copyright 2012 Lukas Mai.
138 This program is free software; you can redistribute it and/or modify it
139 under the terms of either: the GNU General Public License as published
140 by the Free Software Foundation; or the Artistic License.
142 See http://dev.perl.org/licenses/ for more information.