display | more...

In Perl 6, the pointy sub is the sub keyword spelled "->". The word form still exists, however.

Like sub, it is used to give closures argument lists. It cannot be used to define named subroutines, just anonymous ones.

It was given the spelling "->" to clarify the details of for, but turns out to be more useful than that:

    for @a -> $x {
        print $x
    }
    for @a, @b -> $x, $y {
        print $x ~ " and " ~ $y;
    }
    grep -> $str { $str =~ /foo/&/bar/ }
           @array;

(Note that ~ is the Perl 6 string concatenation operator)

for passes its iterator to the closure argument provided. That argument can have a name other than $_ by using a pointy sub, as shown.

Log in or register to write something here or to contact authors.