THE ROAD TO PERL 6
IS PAVED WITH FEATURES

Perl 6 is a pretty big language

With a lot of features meant for one-liners ...

But a lot of features for larger applications as well !

Let's see how we can get the best of them ...

 

one-liners : the awesome parts

 

# auto-priming allows us to create a function with a simpler syntax
# The stars ("Whatever"s) are "holes" to be passed as parameters
my $sub = *+*;
say $sub(3, 4);

# allows for very neat mapping:
# map detects the arity, and feeds as many values as needed
say map(*+*, 1..10);

# you can also use the "implicit parameter" $^ that auto-declares parameters
# these parameter are ordered lexicographically, not by order of usage
say filter { "$^b$^a" eq $^a }, <a ba ab c d>; # list of words, like perl5's qw()
# the lambda is equivalent to: (using eq to compare strings)
-> $a, $b { "$a$b" eq $a }; # {} and -> {} are the same, except for the parameter list

my @values = 0, 5 ... *; # infinite list with the sequence op (deduces next element)
# ^N is an alternative to 0..^N (where ^ means exclusive)
say @values[^10]; # ^10 is like 0..^10 (excluse range, like 0..9)

# you can change multiple values at once, stops at the first empty list
@values[3, 4, 5] = 'a' .. 'z';

one-liners: matching

A lot of time in one-liners is spent with filtering

Let's look at what Perl 6 gives us:

 

.say if /foo/ when /bar/

apw

By Vendethiel

apw

  • 1,306