Prev: Optional parameters

Named only parameters

The user can decide to pass the parameters in as key-value pairs.

Parameters that are declared as positional can always passed as named but if the variable name is prefixed with a colon : then it can only be passed as a named argument and it is optional. To make a named-only parameter required put an exclamation mark ! after it: :$b!

#!/usr/bin/env perl6
use v6;

sub foo($a, :$b) {
    return $a + $b * 2;

say foo(b => 2, 3);   # 7
#say foo(4, 9);        # Too many positional parameters passed; got 2 but expected 1

say foo(2);           # 2   Use of uninitialized value in numeric context
#say foo(2, 3, 4);    # Too many positional parameters passed; got 3 but expected 1

Prev: Optional parameters

Table of Contents


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.