Rules / Types, classes & roles / composite

Capture Reference

Argument list suitable for passing to a Signature

What it is #

Position in the hierarchy #

Inherits from
Does
Inherited byMatch

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method list #

method list(Capture:D:)

Returns the positional part of the Capture.

method hash #

method hash(Capture:D:)

Returns the named/hash part of the Capture.

method elems #

method elems(Capture:D: --> Int:D)

Returns Int:D.

Returns the number of positional elements in the Capture.

method keys #

multi method keys(Capture:D: --> Seq:D)

Returns Seq:D.

Returns a Seq containing all positional keys followed by all named keys. For positional arguments the keys are the respective arguments' ordinal positions starting from zero.

method values #

multi method values(Capture:D: --> Seq:D)

Returns Seq:D.

Returns a Seq containing all positional values followed by all named argument values.

method kv #

multi method kv(Capture:D: --> Seq:D)

Returns Seq:D.

Returns a Seq of alternating keys and values. The positional keys and values, if any, come first followed by the named keys and values.

method pairs #

multi method pairs(Capture:D: --> Seq:D)

Returns Seq:D.

Returns all arguments, the positional followed by the named, as a Seq of Pairs. Positional arguments have their respective ordinal value, starting at zero, as key while the named arguments have their names as key.

method antipairs #

multi method antipairs(Capture:D: --> Seq:D)

Returns Seq:D.

Returns all arguments, the positional followed by the named, as a Seq of Pairs where the keys and values have been swapped, i.e. the value becomes the key and the key becomes the value. This behavior is the opposite of the pairs method.

method Bool #

method Bool(Capture:D: --> Bool:D)

Returns Bool:D.

Returns True if the Capture contains at least one named or one positional argument.

method Capture #

method Capture(Capture:D: --> Capture:D)

Returns Capture:D.

Returns itself, i.e. the invocant.

method Numeric #

method Numeric(Capture:D: --> Int:D)

Returns Int:D.

Returns the number of positional elements in the Capture.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

1 doc-drift · 6 no-output · 2 not-runnable · 12 ok · 1 rakupp-differs

class Capture { }

Not executed: the documentation states no expected output for this example.

my $a = \(42);                      # Capture with one positional arg
my $b = \(1, 2, verbose => True);   # Capture with two positional args and one named arg
my $c = \(1, 2, :verbose(True));    # same as before
my $c = \(1, 2, 'verbose' => True); # Capture with three positional args

Not executed: the documentation states no expected output for this example.

sub greet(:$name, :$age) {
    "$name, $age"
}

Not executed: the documentation states no expected output for this example.

my $d = \(name => 'Mugen', age => 19);   # OK
my $e = \(:name('Jin'), :age(20));       # OK
my $f = \('name' => 'Fuu', 'age' => 15); # Not OK, keys are quoted.

Not executed: the documentation states no expected output for this example.

say greet |$d;                # OUTPUT: «Mugen, 19␤»
say greet |$e;                # OUTPUT: «Jin, 20␤»

Neither engine can run this in isolation — the example depends on context from the surrounding text.

my $x = \(4, 2, 3, -2);
say reverse |$x;              # OUTPUT: «(-2 3 2 4)␤»
say sort 5, |$x;              # OUTPUT: «(-2 2 3 4 5)␤»
Output
(-2 3 2 4)
(-2 2 3 4 5)

Documentation, Rakudo and Raku++ all agree.

say unique |$x, as => {.abs}; # OUTPUT: «(4 2 3)␤»
say unique |$x, :as({.abs});  # OUTPUT: «(4 2 3)␤»

Neither engine can run this in isolation — the example depends on context from the surrounding text.

my $y = \(1, 7, 3, by => {1/$_});
say min |$y;                  # OUTPUT: «7␤», same as min 1, 7, 3, by => {1/$_}
say max |$y;                  # OUTPUT: «1␤», same as max 1, 7, 3, by => {1/$_}
Output
7
1

Documentation, Rakudo and Raku++ all agree.

sub f($a, |c) {
    say $a;
    say c;
    say c.^name;
    say c.list; # see Methods section
    say c.hash; # see Methods section
}

Not executed: the documentation states no expected output for this example.

f 1, 2, 3, a => 4, :b(5);
# OUTPUT:
# 1
# \(2, 3, :a(4), :b(5))
# Capture
# (2 3)
# Map.new((a => 4, b => 5))

Not executed: the documentation states no expected output for this example.

my $b = 1;
my $c = \(4, 2, $b, 3);
say min |$c;        # OUTPUT: «1␤»
$b = -5;
say min |$c;        # OUTPUT: «-5␤»
Documentation
1
-5
Rakudo
1
-5
Raku++
1
1

Raku++ disagrees with both the documentation and Rakudo — a defect.

my Capture $c = \(2, 3, 5, apples => (red => 2));
say $c.list; # OUTPUT: «(2 3 5)␤»
Output
(2 3 5)

Documentation, Rakudo and Raku++ all agree.

my Capture $c = \(2, 3, 5, apples => (red => 2));
say $c.hash; # OUTPUT: «Map.new((:apples(:red(2))))␤»
Documentation
Map.new((:apples(:red(2))))
Rakudo
Map.new((apples => red => 2))
Raku++
Map.new((apples => red => 2))

Both engines agree; the documentation states something else. Trust the engines.

my Capture $c = \(2, 3, 5, apples => (red => 2));
say $c.elems; # OUTPUT: «3␤»
Output
3

Documentation, Rakudo and Raku++ all agree.

my $capture = \(2, 3, 5, apples => (red => 2));
say $capture.keys; # OUTPUT: «(0 1 2 apples)␤»
Output
(0 1 2 apples)

Documentation, Rakudo and Raku++ all agree.

my $capture = \(2, 3, 5, apples => (red => 2));
say $capture.values; # OUTPUT: «(2 3 5 red => 2)␤»
Output
(2 3 5 red => 2)

Documentation, Rakudo and Raku++ all agree.

my $capture = \(2, 3, apples => (red => 2));
say $capture.kv; # OUTPUT: «(0 2 1 3 apples red => 2)␤»
Output
(0 2 1 3 apples red => 2)

Documentation, Rakudo and Raku++ all agree.

my Capture $c = \(2, 3, apples => (red => 2));
say $c.pairs; # OUTPUT: «(0 => 2 1 => 3 apples => red => 2)␤»
Output
(0 => 2 1 => 3 apples => red => 2)

Documentation, Rakudo and Raku++ all agree.

my $capture = \(2, 3, apples => (red => 2));
say $capture.antipairs; # OUTPUT: «(2 => 0 3 => 1 (red => 2) => apples)␤»
Output
(2 => 0 3 => 1 (red => 2) => apples)

Documentation, Rakudo and Raku++ all agree.

say \(1,2,3, apples => 2).Bool; # OUTPUT: «True␤»
say \().Bool;                   # OUTPUT: «False␤»
Output
True
False

Documentation, Rakudo and Raku++ all agree.

say \(1,2,3, apples => 2).Capture; # OUTPUT: «\(1, 2, 3, :apples(2))␤»
Output
\(1, 2, 3, :apples(2))

Documentation, Rakudo and Raku++ all agree.

say \(1,2,3, apples => 2).Numeric; # OUTPUT: «3␤»
Output
3

Documentation, Rakudo and Raku++ all agree.