1 package Function::Parameters::Info;
12 package Function::Parameters::Param;
17 '""' => sub { $_[0]->name },
20 has $_ => (is => 'ro') for qw(name type);
23 my @pn_ro = glob '{positional,named}_{required,optional}';
25 for my $attr (qw[keyword invocant slurpy], map "_$_", @pn_ro) {
31 for my $gen (join "\n", map "sub $_ { \@{\$_[0]->_$_} }", @pn_ro) {
32 eval "$gen\n1" or die $@;
38 $r++ if defined $self->invocant;
39 $r += $self->positional_required;
40 $r += $self->named_required * 2;
46 return 0 + 'Inf' if defined $self->slurpy || $self->named_required || $self->named_optional;
48 $r++ if defined $self->invocant;
49 $r += $self->positional_required;
50 $r += $self->positional_optional;
62 Function::Parameters::Info - Information about parameter lists
66 use Function::Parameters;
68 fun foo($x, $y, :$hello, :$world = undef) {}
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
80 my $invocant = Function::Parameters::info(method () { 42 })->invocant; # '$self'
82 my $slurpy = Function::Parameters::info(fun {})->slurpy; # '@_'
86 L<C<Function::Parameters::info>|Function::Parameters/Introspection> returns
87 objects of this class to describe parameter lists of functions. The following
88 methods are available:
90 =head2 $info->invocant
92 Returns the name of the variable into which the first argument is
93 L<C<shift>|perlfunc/shift>ed automatically, or C<undef> if no such thing
94 exists. This will usually return C<'$self'> for methods.
96 =head2 $info->positional_required
98 Returns a list of the names of the required positional parameters (or a count
101 =head2 $info->positional_optional
103 Returns a list of the names of the optional positional parameters (or a count
106 =head2 $info->named_required
108 Returns a list of the names of the required named parameters (or a count
111 =head2 $info->named_optional
113 Returns a list of the names of the optional named parameters (or a count
118 Returns the name of the final array or hash that gobbles up all remaining
119 arguments, or C<undef> if no such thing exists.
121 As a special case, functions defined without an explicit parameter list (i.e.
122 without C<( )>) will return C<'@_'> here because they accept any number of
125 =head2 $info->args_min
127 Returns the minimum number of arguments this function requires. This is
128 computed as follows: Invocant and required positional parameters count 1 each.
129 Optional parameters don't count. Required named parameters count 2 each (key +
130 value). Slurpy parameters don't count either because they accept empty lists.
132 =head2 $info->args_max
134 Returns the maximum number of arguments this function accepts. This is computed
135 as follows: If there is any named or slurpy parameter, the result is C<Inf>.
136 Otherwise the result is the sum of all invocant and positional parameters.
140 L<Function::Parameters>
144 Lukas Mai, C<< <l.mai at web.de> >>
146 =head1 COPYRIGHT & LICENSE
148 Copyright 2012 Lukas Mai.
150 This program is free software; you can redistribute it and/or modify it
151 under the terms of either: the GNU General Public License as published
152 by the Free Software Foundation; or the Artistic License.
154 See http://dev.perl.org/licenses/ for more information.