Documentation for routine Capture

Documentation for routine Capture, assembled from the following types:

class List

From List

(List) method Capture

Defined as:

method Capture(--> Capture:D)

Returns a Capture where each Pair, if any, in the List has been converted to a named argument. All other elements in the List are converted to positional arguments in the order they are found, i.e. the first non pair item in the list becomes the first positional argument, which gets index 0, the second non pair item becomes the second positional argument, getting index 1 etc.

my $list = (75=> 2=> 17);
my $capture = $list.Capture;
say $capture.keys;                                # OUTPUT: «(0 1 a b)␤» 
my-sub(|$capture);                                # RESULT: «7, 5, 2, 17» 
 
sub my-sub($first$second:$a:$b{
    say "$first$second$a$b"
}

A more advanced example demonstrating the returned Capture being matched against a Signature.

my $list = (75=> 2=> 17);
say so $list.Capture ~~ :($ where * == 7,$,:$a,:$b); # OUTPUT: «True␤» 
 
$list = (85=> 2=> 17);
say so $list.Capture ~~ :($ where * == 7,$,:$a,:$b); # OUTPUT: «False␤» 

class Capture

From Capture

(Capture) method Capture

Defined as:

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

Returns itself, i.e. the invocant.

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

class Map

From Map

(Map) method Capture

Defined as:

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

Returns a Capture where each key, if any, has been converted to a named argument with the same value as it had in the original Map. The returned Capture will not contain any positional arguments.

my $map = Map.new('a' => 2'b' => 17);
my $capture = $map.Capture;
my-sub(|$capture);                                # RESULT: «2, 17» 
 
sub my-sub(:$a:$b{
    say "$a$b"
}