Welcome to the NetCologne GmbH open source mirroring service!

This machine mirrors various open-source projects. 20 Gbit/s uplink.

If there are any issues or you want another project mirrored, please contact mirror-service -=AT=- netcologne DOT de !

Function::Parameters

Lukas Mai

2017-08-10

functions and methods with parameter lists

… another one?

Features of Function::Parameters

use Function::Parameters;
 
# plain function
fun foo($x, $y, $z = 5) {
    return $x + $y + $z;
}
print foo(1, 2), "\n";  # 8
 
# method with implicit $self
method bar($label, $n) {
    return "$label: " . ($n * $self->scale);
}
use Function::Parameters;

# dynamic default arguments
fun foo($x, $y = $x * 100, $z = []) {
    ...
}

method bar($x = $self->default_x, $y = $self->default_y) {
    ...
}
package Collection;

use strict;
use warnings;

sub values { ... }

my %hash;
my @vals = values %hash;
# Ambiguous call resolved as CORE::values(), qualify as such or use &
package Collection;
use Function::Parameters;
use strict;
use warnings;

method values() { ... }

my %hash;
my @vals = values %hash;
# Ambiguous call resolved as CORE::values(), qualify as such or use &
use Function::Parameters;

# named arguments: order doesn't matter in the call
fun create_point(:$x, :$y, :$color) {
    print "creating a $color point at ($x, $y)\n";
}

create_point(
    color => "red",
    x     => 10,
    y     => 5,
);
use Function::Parameters;
# named arguments: order doesn't matter in the call
# slurpy parameter
fun create_point(:$x, :$y, :$color, %rest) {
    print "creating a $color point at ($x, $y)\n";
}

create_point(
    flavor => "chocolate",
    color  => "red",
    x      => 10,
    y      => 5,
);
package Derived {
    use Function::Parameters qw(:std :modifiers);
    use Moo;
 
    extends 'Base';
 
    has 'go_big' => (
        is => 'ro',
    );
 
    # "around" method with implicit $orig and $self
    around size() {
        return $self->$orig() * 2 if $self->go_big;
        return $self->$orig();
    }
}
use Function::Parameters;
use Types::Standard qw(Int Str ArrayRef HashRef Object);
use MooseX::Types::Moose qw(Int Str ArrayRef HashRef Object);
fun foo(
    Int $n,
    # ^ an integer
    Str $name,
    # ^ a string
    HashRef[ ArrayRef[ Object ] ] $mapping,
    # ^ a reference to a hash
    # … whose values are references to arrays
    # … that contain objects
) { ... }
use Function::Parameters;
use Types::Standard qw(Int Str ArrayRef HashRef Object);
use MooseX::Types::Moose qw(Int Str ArrayRef HashRef Object);
method foo(
    Int $n,
    # ^ an integer
    Str $name,
    # ^ a string
    HashRef[ ArrayRef[ Object ] ] $mapping,
    # ^ a reference to a hash
    # … whose values are references to arrays
    # … that contain objects
) { ... }
use Function::Parameters;
use MooseX::Types::Moose qw(Int Str ArrayRef HashRef Object);

fun foo(
    Int $n,
    # ^ an integer
    Str $name,
    # ^ a string
    HashRef[ ArrayRef[ Object ] ] $mapping,
    # ^ a reference to a hash
    # … whose values are references to arrays
    # … that contain objects
) { ... }
use utf8;
use Function::Parameters { 'λ' => 'function' };

# custom declarators (define your own keywords)
my $fac = λ ($n) { $n < 2 ? 1 : $n * __SUB__->($n - 1) };
use Function::Parameters;

# introspection
fun foo($man, $plan, $canal, @panama) { ... }

my $meta = Function::Parameters::info \&foo;
say $meta->args_min;  # 3
say $meta->args_max;  # Inf

Thank you