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