The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Sub::Meta::Parameters - meta information about parameters

SYNOPSIS

    use Sub::Meta::Parameters;

    my $p1 = Sub::Meta::Parameters->new(
        args => ['Str']
    );
    $p1->invocant;            # => undef;
    $p1->invocants;           # => [];
    $p1->positional;          # => [Sub::Meta::Param->new('Str')]
    $p1->positional_required; # => [Sub::Meta::Param->new('Str')]
    $p1->positional_optional; # => []
    $p1->named;               # => []
    $p1->named_required;      # => []
    $p1->named_optional;      # => []
    $p1->nshift;              # => 0
    $p1->slurpy;              # => 0
    $p1->args_min;            # => 1
    $p1->args_max;            # => 1


    my $x = Sub::Meta::Param->new({ type => 'Int', name => '$x', named => 1 });
    my $y = Sub::Meta::Param->new({ type => 'Int', name => '$y', named => 1 });

    my $p2 = Sub::Meta::Parameters->new(
        nshift => 1,
        args => [
            'ClassName', $x, $y
        ]
    );

    $p2->invocant;            # => Sub::Meta::Param->new('ClassName');
    $p2->invocants;           # => [Sub::Meta::Param->new('ClassName')];
    $p2->positional;          # => []
    $p2->positional_required; # => []
    $p2->positional_optional; # => []
    $p2->named;               # => [$x, $y]
    $p2->named_required;      # => [$x, $y]
    $p2->named_optional;      # => []
    $p2->nshift;              # => 1
    $p2->slurpy;              # => 0
    $p2->args_min;            # => 5
    $p2->args_max;            # => 0+'Inf'

METHODS

new

Constructor of Sub::Meta::Parameters.

    my $p = Sub::Meta::Parameters->new(
        args   => ['Str'], # required. arguments
        nshift => 0,       # optional. number of shift arguments
        slurpy => 0,       # optional. whether get all rest arguments
    );

ACCESSORS

args

    method args() => ArrayRef[InstanceOf[Sub::Meta::Param]]

Subroutine arguments arrayref.

has_args

    method has_args() => Bool

Whether Sub::Meta::Parameters has args or not.

set_args

    method set_args(ArrayRef[InstanceOf[Sub::Meta::Param]]) => $self
    method set_args(ArrayRef[$sub_meta_param_args]) => $self
    method set_args(Dict[Str, $sub_meta_param_args]) => $self
    method set_args(Ref $type) => $self

Setter for subroutine arguments. An element can be an argument of Sub::Meta::Param.

    use Types::Standard -types;

    my $p = Sub::Meta::Parameters->new(args => []);
    $p->set_args([Int,Int]);
    $p->set_args([{ type => Int, name => 'num' }]);

    # named case:
    $p->set_args({ a => Str, b => Str });
    $p->set_args({
        a => { isa => Str, default => 123 },
        b => { isa => Str, optional => 1 }
    });

    # single ref:
    $p->set_args(Str); # => $p->set_args([Str])

all_args

    method all_args() => ArrayRef[InstanceOf[Sub::Meta::Param]]

Subroutine invocants and arguments arrayref.

nshift

    method nshift() => Enum[0,1]

Number of shift arguments.

set_nshift($nshift)

    method nshift(Enum[0,1] $nshift) => $self

Setter for nshift. For example, it is assumed that 1 is specified in the case of methods, and 0 is specified in the case of normal functions.

slurpy

    method slurpy() => Maybe[InstanceOf[Sub::Meta::Param]]

Subroutine all rest arguments.

has_slurpy

    method has_slurpy() => Bool

Whether Sub::Meta::Parameters has slurpy or not.

set_slurpy($param_args)

    method set_slurpy(InstanceOf[Sub::Meta::Param]) => $self> or
    method set_slurpy($sub_meta_param_args)> or

Setter for slurpy:

    my $p = Sub::Meta::Parameters->new(args => [{ isa => 'Int', name => '$a'}]);
    $p->set_slurpy({ name => '@numbers', isa => 'Int' }); # => (Int $a, Int @numbers)

positional

    method positional() => ArrayRef[InstanceOf[Sub::Meta::Param]]

Returns an arrayref of parameter objects for the positional arguments.

positional_required

    method positional_required() => ArrayRef[InstanceOf[Sub::Meta::Param]]

Returns an arrayref of parameter objects for the required positional arguments.

positional_optional

    method positional_optional() => ArrayRef[InstanceOf[Sub::Meta::Param]]

Returns an arrayref of parameter objects for the optional positional arguments.

named

    method named() => ArrayRef[InstanceOf[Sub::Meta::Param]]

Returns an arrayref of parameter objects for the named arguments.

named_required

    method named_required() => ArrayRef[InstanceOf[Sub::Meta::Param]]

Returns an arrayref of parameter objects for the required named arguments.

named_optional

    method named_optional() => ArrayRef[InstanceOf[Sub::Meta::Param]]

Returns an arrayref of parameter objects for the optional named arguments.

invocant

    method invocant() => Maybe[InstanceOf[Sub::Meta::Param]]

First element of invocants.

invocants

    method invocants() => ArrayRef[InstanceOf[Sub::Meta::Param]]

Returns an arrayref of parameter objects for the variables into which initial arguments are shifted automatically. This will usually return () for normal functions and ('$self') for methods.

has_invocant

    method has_invocant() => Bool

Whether Sub::Meta::Parameters has invocant or not.

set_invocant($param_args)

    method set_invocant(InstanceOf[Sub::Meta::Param]) => $self
    method set_invocant($sub_meta_param_args) => $self

Setter for invocant:

    my $invocant = Sub::Meta::Param->new(name => '$self');
    my $p = Sub::Meta::Parameters->new(args => []);
    $p->set_invocant($invocant);

    $p->invocant; # => Sub::Meta::Param->new(name => '$self', invocant => 1);
    $p->nshift; # => 1

args_min

    method args_min() => NonNegativeInt

Returns the minimum number of required arguments.

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.

args_max

    method args_max() => NonNegativeInt

Returns the maximum number of arguments.

This is computed as follows: If there are any named or slurpy parameters, the result is Inf. Otherwise the result is the number of all invocants and positional parameters.

METHODS

is_same_interface($other_meta)

    method is_same_interface(InstanceOf[Sub::Meta::Parameters] $other_meta) => Bool

A boolean value indicating whether Sub::Meta::Parameters object is same or not. Specifically, check whether args, nshift and slurpy are equal.

is_relaxed_same_interface($other_meta)

    method is_relaxed_same_interface(InstanceOf[Sub::Meta::Parameters] $other_meta) => Bool

A boolean value indicating whether Sub::Meta::Parameters object is same or not. Specifically, check whether args, nshift and slurpy are satisfy the condition of $self side:

    my $meta = Sub::Meta::Parameters->new(args => []);
    my $other = Sub::Meta::Parameters->new(args => ["Str"]);
    $meta->is_same_interface($other); # NG
    $meta->is_relaxed_same_interface($other); # OK. The reason is that $meta does not specify the elements of args.

is_same_interface_inlined($other_meta_inlined)

    method is_same_interface_inlined(InstanceOf[Sub::Meta::Parameters] $other_meta) => Str

Returns inlined is_same_interface string.

is_relaxed_same_interface_inlined($other_meta_inlined)

    method is_relaxed_same_interface_inlined(InstanceOf[Sub::Meta::Parameters] $other_meta) => Str

Returns inlined is_relaxed_same_interface string.

error_message($other_meta)

    method error_message(InstanceOf[Sub::Meta::Parameters] $other_meta) => Str

Return the error message when the interface is not same. If same, then return empty string.

relaxed_error_message($other_meta)

    method relaxed_error_message(InstanceOf[Sub::Meta::Parameters] $other_meta) => Str

Return the error message when the interface does not satisfy the $self meta. If match, then return empty string.

display

    method display() => Str

Returns the display of Sub::Meta::Parameters:

    use Sub::Meta::Parameters;
    use Types::Standard qw(Num);
    my $meta = Sub::Meta::Parameters->new(
        args => [
            { name => '$lat', type => Num, named => 1 },
            { name => '$lng', type => Num, named => 1 },
        ],
    );
    $meta->display; # 'Num :$lat, Num :$lng'

OTHERS

param_class

    method param_class() => Str

Returns class name of param. default: Sub::Meta::Param Please override for customization.

SEE ALSO

Function::Parameters::Info.

The methods in this module are almost copied from the Function::Parameters::Info methods.

LICENSE

Copyright (C) kfly8.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

kfly8 <kfly@cpan.org>