File: Info.pm

package info (click to toggle)
libfunction-parameters-perl 2.001003-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 948 kB
  • sloc: perl: 6,478; makefile: 3
file content (199 lines) | stat: -rw-r--r-- 5,517 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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 => 1,
        '""'     => method (@) { $self->{name} },
    ;

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

    method name() { $self->{name} }
    method type() { $self->{type} }
}

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

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

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

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

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

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

method invocants() {
    my @p = @{$self->{_positional_required}};
    splice @p, $self->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 \&foo;
  my @p0 = $info->invocants;            # ()
  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 @invocants = Function::Parameters::info(method () { 42 })->invocants;
  # ('$self')
  
  my $slurpy = Function::Parameters::info(fun (@) {})->slurpy;  # '@'

=head1 DESCRIPTION

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

=head3 $info->invocants

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

=head3 $info->positional_required

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

=head3 $info->positional_optional

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

=head3 $info->named_required

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

=head3 $info->named_optional

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

=head3 $info->slurpy

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

=head3 $info->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->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<Inf>.
Otherwise the result is the number of all invocants and positional parameters.

=head3 $info->invocant

Similar to L</$info-E<gt>invocants> above: Returns C<undef> 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<name>, which returns the name of the parameter (as a plain
string), and C<type>, 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<overload|overload> stringification to call C<name>. That is, if you treat
parameter objects like strings, they behave like strings (i.e. their names).

=head1 SEE ALSO

L<Function::Parameters>

=head1 AUTHOR

Lukas Mai, C<< <l.mai at web.de> >>

=head1 COPYRIGHT & 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