X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FFunction%2FParameters%2FInfo.pm;fp=lib%2FFunction%2FParameters%2FInfo.pm;h=8604eeb8807b28128b5123b2c06ee6bb96a54a50;hb=ebdc721b189a191184af4bbb1d405ab4ad33a183;hp=15196291dfa2bd08c36a72e419380377fd9ab923;hpb=33bb66c08aea26bd5788d9b0e812076fcb49452a;p=p5sagit%2FFunction-Parameters.git diff --git a/lib/Function/Parameters/Info.pm b/lib/Function/Parameters/Info.pm index 1519629..8604eeb 100644 --- a/lib/Function/Parameters/Info.pm +++ b/lib/Function/Parameters/Info.pm @@ -20,6 +20,125 @@ for my $gen (join "\n", map "sub $_ { \@{\$_[0]->_$_} }", @pn_ro) { eval "$gen\n1" or die $@; } +sub args_min { + my $self = shift; + my $r = 0; + $r++ if defined $self->invocant; + $r += $self->positional_required; + $r += $self->named_required * 2; + $r +} + +sub args_max { + my $self = shift; + return 0 + 'Inf' if defined $self->slurpy || $self->named_required || $self->named_optional; + my $r = 0; + $r++ if defined $self->invocant; + $r += $self->positional_required; + $r += $self->positional_optional; + $r +} + 'ok' __END__ + +=encoding UTF-8 + +=head1 NAME + +Function::Parameters::Info - Information about parameter lists + +=head1 SYNOPSIS + + use Function::Parameters; + + fun foo($x, $y, :$hello, :$world = undef) {} + + my $info = Function::Parameters::info \&foo; + my $p0 = $info->invocant; # undef + my @p1 = $info->positional_required; # ('$x', '$y') + my @p2 = $info->positional_optional; # () + my @p3 = $info->named_required; # ('$hello') + my @p4 = $info->named_optional; # ('$world') + my $p5 = $info->slurpy; # undef + my $min = $info->args_min; # 4 + my $max = $info->args_max; # inf + + my $invocant = Function::Parameters::info(method () { 42 })->invocant; # '$self' + + my $slurpy = Function::Parameters::info(fun {})->slurpy; # '@_' + +=head1 DESCRIPTION + +L|Function::Parameters/Introspection> returns +objects of this class to describe parameter lists of functions. The following +methods are available: + +=head2 C<< $info->invocant >> + +Returns the name of the variable into which the first argument is +L|perlfunc/shift>ed into automatically, or C if no such thing +exists. This will usually return C<'$self'> for methods. + +=head2 C<< $info->positional_required >> + +Returns a list of the names of the required positional parameters (or a count +in scalar context). + +=head2 C<< $info->positional_optional >> + +Returns a list of the names of the optional positional parameters (or a count +in scalar context). + +=head2 C<< $info->named_required >> + +Returns a list of the names of the required named parameters (or a count +in scalar context). + +=head2 C<< $info->named_optional >> + +Returns a list of the names of the optional named parameters (or a count +in scalar context). + +=head2 C<< $info->slurpy >> + +Returns the name of the final array or hash that gobbles up all remaining +arguments, or C if no such thing exists. + +As a special case, functions defined without an explicit parameter list (i.e. +without C<( )>) will return C<'@_'> here because they accept any number of +arguments. + +=head2 C<< $info->args_min >> + +Returns the minimum number of arguments this function requires. This is +computed as follows: Invocant and required positional parameters count 1 each. +Optional parameters don't count. Required named parameters count 2 each (key + +value). Slurpy parameters don't count either because they accept empty lists. + +=head2 C<< $info->args_max >> + +Returns the maximum number of arguments this function accepts. This is computed +as follows: If there is any named or slurpy parameter, the result is C. +Otherwise the result is the sum of all invocant and positional parameters. + +=head1 SEE ALSO + +L + +=head1 AUTHOR + +Lukas Mai, C<< >> + +=head1 COPYRIGHT & LICENSE + +Copyright 2012 Lukas Mai. + +This program is free software; you can redistribute it and/or modify it +under the terms of either: the GNU General Public License as published +by the Free Software Foundation; or the Artistic License. + +See http://dev.perl.org/licenses/ for more information. + +=cut