<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Function::Parameters::Info;

use v5.14.0;
use warnings;

use Function::Parameters;
use Carp ();

our $VERSION = '2.001003';

{
    package Function::Parameters::Param;

    use overload
        fallback =&gt; 1,
        '""'     =&gt; method (@) { $self-&gt;{name} },
    ;

    method new($class: :$name, :$type) {
        bless { @_ }, $class
    }

    method name() { $self-&gt;{name} }
    method type() { $self-&gt;{type} }
}

method new($class:
    :$keyword,
    :$nshift,
    :$_positional_required,
    :$_positional_optional,
    :$_named_required,
    :$_named_optional,
    :$slurpy,
) {
    bless {@_}, $class
}

method keyword() { $self-&gt;{keyword} }
method nshift () { $self-&gt;{nshift}  }
method slurpy () { $self-&gt;{slurpy}  }
method positional_optional() { @{$self-&gt;{_positional_optional}} }
method named_required     () { @{$self-&gt;{_named_required}} }
method named_optional     () { @{$self-&gt;{_named_optional}} }

method positional_required() {
    my @p = @{$self-&gt;{_positional_required}};
    splice @p, 0, $self-&gt;nshift;
    @p
}

method args_min() {
    my $r = 0;
    $r += @{$self-&gt;{_positional_required}};
    $r += $self-&gt;named_required * 2;
    $r
}

method args_max() {
    return 0 + 'Inf' if defined $self-&gt;slurpy || $self-&gt;named_required || $self-&gt;named_optional;
    my $r = 0;
    $r += @{$self-&gt;{_positional_required}};
    $r += $self-&gt;positional_optional;
    $r
}

method invocant() {
    my $nshift = $self-&gt;nshift;
    return undef
        if $nshift == 0;
    return $self-&gt;{_positional_required}[0]
        if $nshift == 1;
    Carp::croak "Can't return a single invocant; this function has $nshift";
}

method invocants() {
    my @p = @{$self-&gt;{_positional_required}};
    splice @p, $self-&gt;nshift;
    @p
}

'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 \&amp;foo;
  my @p0 = $info-&gt;invocants;            # ()
  my @p1 = $info-&gt;positional_required;  # ('$x', '$y')
  my @p2 = $info-&gt;positional_optional;  # ()
  my @p3 = $info-&gt;named_required;       # ('$hello')
  my @p4 = $info-&gt;named_optional;       # ('$world')
  my $p5 = $info-&gt;slurpy;               # undef
  my $min = $info-&gt;args_min;  # 4
  my $max = $info-&gt;args_max;  # inf
  
  my @invocants = Function::Parameters::info(method () { 42 })-&gt;invocants;
  # ('$self')
  
  my $slurpy = Function::Parameters::info(fun (@) {})-&gt;slurpy;  # '@'

=head1 DESCRIPTION

L&lt;C&lt;Function::Parameters::info&gt;|Function::Parameters/Introspection&gt; returns
objects of this class to describe parameter lists of functions. See below for
L&lt;/Parameter Objects&gt;.  The following methods are available:

=head3 $info-&gt;invocants

Returns a list of parameter objects for the variables into which initial
arguments are L&lt;C&lt;shift&gt;|perlfunc/shift ARRAY&gt;ed automatically (or a count in
scalar context). This will usually return C&lt;()&gt; for normal functions and
C&lt;('$self')&gt; for methods.

=head3 $info-&gt;positional_required

Returns a list of parameter objects for the required positional parameters (or
a count in scalar context).

=head3 $info-&gt;positional_optional

Returns a list of parameter objects for the optional positional parameters (or
a count in scalar context).

=head3 $info-&gt;named_required

Returns a list of parameter objects for the required named parameters (or a
count in scalar context).

=head3 $info-&gt;named_optional

Returns a list of parameter objects for the optional named parameters (or a
count in scalar context).

=head3 $info-&gt;slurpy

Returns a parameter object for the final array or hash that gobbles up all remaining
arguments, or C&lt;undef&gt; if no such thing exists.

=head3 $info-&gt;args_min

Returns the minimum number of arguments this function requires. This is
computed as follows: Invocants 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.

=head3 $info-&gt;args_max

Returns the maximum number of arguments this function accepts. This is computed
as follows: If there are any named or slurpy parameters, the result is C&lt;Inf&gt;.
Otherwise the result is the number of all invocants and positional parameters.

=head3 $info-&gt;invocant

Similar to L&lt;/$info-E&lt;gt&gt;invocants&gt; above: Returns C&lt;undef&gt; if the number of
invocants is 0, a parameter object for the invocant if there is exactly 1, and
throws an exception otherwise.

=head3 Parameter Objects

Many of the methods described above return parameter objects.  These objects
have two methods: C&lt;name&gt;, which returns the name of the parameter (as a plain
string), and C&lt;type&gt;, which returns the corresponding type constraint object
(or undef if there was no type specified).

This should be invisible if you don't care about types because the objects also
L&lt;overload|overload&gt; stringification to call C&lt;name&gt;. That is, if you treat
parameter objects like strings, they behave like strings (i.e. their names).

=head1 SEE ALSO

L&lt;Function::Parameters&gt;

=head1 AUTHOR

Lukas Mai, C&lt;&lt; &lt;l.mai at web.de&gt; &gt;&gt;

=head1 COPYRIGHT &amp; LICENSE

Copyright 2013, 2016 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
</pre></body></html>