1 package Function::Parameters::Info;
6 our $VERSION = '1.0301';
8 # If Moo isn't loaded yet but Moose is, avoid pulling in Moo and fall back to Moose
9 my ($Moo, $meta_make_immutable);
11 if ($INC{'Moose.pm'} && !$INC{'Moo.pm'}) {
13 $meta_make_immutable = sub { $_[0]->meta->make_immutable };
17 $meta_make_immutable = sub {};
23 package Function::Parameters::Param;
25 BEGIN { $Moo->import; }
28 '""' => sub { $_[0]->name },
31 has $_ => (is => 'ro') for qw(name type);
33 __PACKAGE__->$meta_make_immutable;
36 my @pn_ro = glob '{positional,named}_{required,optional}';
38 for my $attr (qw[keyword invocant slurpy], map "_$_", @pn_ro) {
44 for my $gen (join "\n", map "sub $_ { \@{\$_[0]->_$_} }", @pn_ro) {
45 eval "$gen\n1" or die $@;
51 $r++ if defined $self->invocant;
52 $r += $self->positional_required;
53 $r += $self->named_required * 2;
59 return 0 + 'Inf' if defined $self->slurpy || $self->named_required || $self->named_optional;
61 $r++ if defined $self->invocant;
62 $r += $self->positional_required;
63 $r += $self->positional_optional;
67 __PACKAGE__->$meta_make_immutable;
77 Function::Parameters::Info - Information about parameter lists
81 use Function::Parameters;
83 fun foo($x, $y, :$hello, :$world = undef) {}
85 my $info = Function::Parameters::info \&foo;
86 my $p0 = $info->invocant; # undef
87 my @p1 = $info->positional_required; # ('$x', '$y')
88 my @p2 = $info->positional_optional; # ()
89 my @p3 = $info->named_required; # ('$hello')
90 my @p4 = $info->named_optional; # ('$world')
91 my $p5 = $info->slurpy; # undef
92 my $min = $info->args_min; # 4
93 my $max = $info->args_max; # inf
95 my $invocant = Function::Parameters::info(method () { 42 })->invocant; # '$self'
97 my $slurpy = Function::Parameters::info(fun {})->slurpy; # '@_'
101 L<C<Function::Parameters::info>|Function::Parameters/Introspection> returns
102 objects of this class to describe parameter lists of functions. The following
103 methods are available:
105 =head2 $info->invocant
107 Returns the name of the variable into which the first argument is
108 L<C<shift>|perlfunc/shift>ed automatically, or C<undef> if no such thing
109 exists. This will usually return C<'$self'> for methods.
111 =head2 $info->positional_required
113 Returns a list of the names of the required positional parameters (or a count
116 =head2 $info->positional_optional
118 Returns a list of the names of the optional positional parameters (or a count
121 =head2 $info->named_required
123 Returns a list of the names of the required named parameters (or a count
126 =head2 $info->named_optional
128 Returns a list of the names of the optional named parameters (or a count
133 Returns the name of the final array or hash that gobbles up all remaining
134 arguments, or C<undef> if no such thing exists.
136 As a special case, functions defined without an explicit parameter list (i.e.
137 without C<( )>) will return C<'@_'> here because they accept any number of
140 =head2 $info->args_min
142 Returns the minimum number of arguments this function requires. This is
143 computed as follows: Invocant and required positional parameters count 1 each.
144 Optional parameters don't count. Required named parameters count 2 each (key +
145 value). Slurpy parameters don't count either because they accept empty lists.
147 =head2 $info->args_max
149 Returns the maximum number of arguments this function accepts. This is computed
150 as follows: If there is any named or slurpy parameter, the result is C<Inf>.
151 Otherwise the result is the sum of all invocant and positional parameters.
153 =head2 Experimental feature: Types
155 All the methods described above actually return parameter objects wherever the
156 description says "name". These objects have two methods: C<name>, which
157 returns the name of the parameter (as a plain string), and C<type>, which
158 returns the corresponding type constraint object (or undef if there was no type
161 This should be invisible if you don't use types because the objects also
162 L<overload|overload> stringification to call C<name>. That is, if you treat
163 parameter objects like strings, they behave like strings (i.e. their names).
167 L<Function::Parameters>
171 Lukas Mai, C<< <l.mai at web.de> >>
173 =head1 COPYRIGHT & LICENSE
175 Copyright 2013 Lukas Mai.
177 This program is free software; you can redistribute it and/or modify it
178 under the terms of either: the GNU General Public License as published
179 by the Free Software Foundation; or the Artistic License.
181 See http://dev.perl.org/licenses/ for more information.