Amazing Perl 6


This page lists some amazing Perl 6 snippets. The list after the title refers to the keywords for the snippet.

Factorial

sub postfix:<!> { [*] 1..$^n }
say 5!;

Explanation

Custom Operators

There is a set of special sub declarations that interfere in the parsing of the language, these are the:

you declare them by using type:identifier, where the identifier is quoted using the '<' and '>', optionally « » when you want to use '<' or '>' as part of the identifier. Some examples of subs that come defined in Perl 6 core are:

So when you declare a sub postfix:<!> you are declaring a language operator that lives in the same level as any other Perl 6 operator.

Gather/Take

my @a := gather for 1..1000 -> $i { say $i; take $i };
say @a[10]; # prints 1 2 3 4 5 6 7 8 9 10