perl5i::Signature - Representing what parameters a subroutine accepts
func hello( $greeting, $place ) { say "$greeting, $place" } my $code = \&hello; my $signature = $code->signature; say $signature->num_positional_params; # 2 say $signature->is_method; # false
A Signature is a representation of what parameters a subroutine accepts. Each subroutine defined with func or method will have a signature associated with it. You can get at it by calling the signature method on the code reference. See "Signature Introspection" in perl5i for more details.
func
method
signature
Subroutines declared with Perl's built in sub will have no signature.
sub
my $params = $sig->params;
An array ref of the parameters a subroutine takes in the order it takes them. Currently they are just strings. In the future they will be string overloaded objects.
my $params = $sig->positional_params;
Like $sig->params but it is just the positional parameters.
$sig->params
In the future there will be named parameters.
my $num_positional_params = $sig->num_positional_params;
The number of named parameters the subroutine takes.
In the future there will be named parameters. For the purposes of determining how many arguments a function takes, it is most useful to look just at the positional ones.
This is mostly an optimization for $sig->positional_params->size.
$sig->positional_params->size
my $params = $sig->as_string;
The original signature string.
my $invocant = $sig->invocant;
The invocant is the object or class a method is called on. invocant will return the parameter which contains this, by default it is $self on a method, and nothing a regular subroutine.
invocant
$self
my $is_method = $sig->is_method;
Returns if the subroutine was declared as a method.
Signature objects are string overloaded to return as_string. They are also always true to avoid objects taking no parameters from being confused with subroutines with no signatures.
as_string
To install perl5i, copy and paste the appropriate command in to your terminal.
cpanm
cpanm perl5i
CPAN shell
perl -MCPAN -e shell install perl5i
For more information on module installation, please visit the detailed CPAN module installation guide.