Perl (5) are different languages • Sometimes called “sister languages”, belonging to the same family • They share many concepts, but are not compatible • Still, “Perl 6 is Perl”
Perl (1-5)) • Language design started in 2000 • First intention was to get it ready in ~2 years • It actually took 15 years • - When will Perl 6 be released? - On Christmas! • Released in December 2015 (on Christmas)
the official test suite) • NQP (Not Quite Perl) is a subset of Perl 6 • Rakudo is a Perl 6 (and NQP) compiler • Rakudo Star is a Rakudo distribution • MoarVM is a virtual machine (runtime) • Rakudo can also target JVM (Java runtime)
'Saturday' || $day eq 'Sunday' { say 'You should not be working today.'; } elsif $hour !~~ 9..16 { say 'You should not be working at this hour.' } else { say 'Work work work!'; } unless $day eq 'Monday' { say "Yay! It's not Monday!"; }
with $score { $high_score = $score if $score > $high_score; } else { say "Score is not set"; } given $score { when 0..100 { say "You suck"; } when $_ > 9000 { say "IT'S OVER 9000!!!!11"; } default { say "Meh" } }
(my $n = 10; $n > 0; $n--) { say $n; sleep 1; } say "Ignition!"; my @pets = ('cat', 'dog', 'hamster', 'porcupine'); for @pets -> $pet { say "One of my pets is a $pet"; }
• Arrays – fixed-size: my $foo = 42; $foo = 3.14159; $foo = 'Om nom nom'; my @musketeers[3] = [ 'Athos', 'Porthos', 'Aramis' ]; @musketeers[3] = "D'Artagnan"; # Index 3 for dimension 1 out of range (must be 0..2) my @dinosaurs = [ 'Stegosaurus', 'T-Rex' ]; @dinosaurs[2] = 'Velociraptor';
or strings: say [1 .. 5]; # [1 2 3 4 5] say [1 ^.. 5]; # [2 3 4 5] say [1 ..^ 5]; # [1 2 3 4] say [1 ^..^ 5]; # [2 3 4] say [^5]; # [0 1 2 3 4] my @alphabet = 'a' .. 'z'; say @alphabet[5 .. 10]; # (f g h i j k) say @alphabet[^5]; # (a b c d e)
• Can be “infinite”: • Lazy lists made from other lazy lists: my @ints = 1 .. Inf; say @ints[12345]; # 12346 my @even_ints = grep * % 2 == 0, @ints; say @even_ints[12345]; # 24692 my @squares = map * ** 2, @ints; say @squares[12345]; # 152423716
= 1.23; say $foo.WHAT; # (Rat) $foo = 'Bar'; say $foo.WHAT; # (Str) my Rat $foo = 1.23; $foo = 'Bar'; # Type check failed in assignment to $foo; # expected Rat but got Str
constraints to types: • Can be used ad-hoc (not named): subset EvenInt of Int where * % 2 == 0; my EvenInt $foo = 3; # Type check failed in assignment to $foo; # expected EvenInt but got Int sub hello(Str $name where *.chars > 0) { say "Hello, $name!"; }
called with or without parentheses: • Without return, the last value is returned: sub hello { return "Hello, World!"; } say hello(); say hello; # Hello, World! (x2) sub hello { "Hello, World!"; }
value using a preceding argument: sub hello($name = 'World') { "Hello, $name!"; } say hello; # Hello, World! say hello('Jacob'); # Hello, Jacob! sub hello($name, $bangs = $name.chars) { "Hello, $name" ~ "!" x $bangs; } say hello('Mike'); # Hello, Mike!!!!
declared with a return type: sub hello(Str $name) { "Hello, $name!"; } say hello(321); # Calling hello(Int) will never work with declared # signature (Str $name) sub hello(Str $name) returns Str { "Hello, $name!"; }
hello($name, :$title) { "Hello, " ~ ("$title " if $title) ~ "$name!"; } say hello('Tom', title => "Mr."); # Hello, Mr. Tom! sub hello($name, :$title!) { "Hello, $title $name!"; } say hello('Tom'); # Required named parameter 'title' not passed
Example: multi hello(Str $name) { "Hello, $name!"; } multi hello(Numeric $number) { "Hello, number $number!"; } say hello('Angie'); # Hello, Angie! say hello(123); # Hello, number 123!
is object oriented • But, it doesn't impose the object oriented programming style • Perl 6's object system design was an inspiration for Moose, the popular Perl 5 object framework
Implicit private has $!gender; # Explicit private has $.age; # Accessor has $.name is rw; # Read/write } my $moose = Animal.new( species => 'Alces alces', gender => 'male', age => 12, ); say $moose.age; $moose.name = 'Edward';
role Talking { method introduce { say 'Hello, my name is ' ~ self.name; } } class TalkingAnimal is Animal does Talking { } my $moose = TalkingAnimal.new( species => 'Alces alces', name => 'Frank' ); $moose.introduce; # Hello, my name is Frank
that facilitate functional programming • Like with object orientation, it's up to the programmer to make use of these features or not • (This is what this multi-paradigm thing is about)
be passed as arguments: • Can be returned by other functions: sub percentage($value) { return $value * 100 ~ '%'; } say map(&percentage, 0.1, 0.5, 1.5); # (10% 50% 150%) sub multiply($factor) { return sub ($value) { return $factor * $value }; } my &triple = multiply(3); say triple(9); # 27
helpful for complex condition checking: if $code eq 'PL' || $code eq 'SE' || $code eq 'UK' { say "Sorry, payment in Euro not possible"; } if $code eq any('PL', 'SE', 'UK') { say "Sorry, payment in Euro not possible"; } if $code eq 'PL'|'SE'|'UK' { say "Sorry, payment in Euro not possible"; }
(^): • none: my $password = 'Hunter1'; say 'Good' if $password ~~ all(/<[A..Z]>/, /<[0..9]>/); say 'Good' if $password ~~ /<[A..Z]>/ & /<[0..9]>/; my @values = (1 > 2, 3 == 4, 5 < 6); say 'Only one is true' if one(@values); my @numbers = (1, 0, 2, 5, 17); say 'No negative numbers' if none(@numbers) < 0;
Another feature that benefits code readability • So the construct shown earlier: can be written this way: @result = @data.grep(* > 0).unique.sort; @result = sort(unique(grep(* > 0, @data)));
infix operator to all elements of a list: • Zip meta operator (Z?) applies an operator to successive pairs of items from two lists: my @numbers = (1, 2, 3, 4, 5); say "Sum: " ~ [+] @numbers; # Sum: 15 say "Product: " ~ [*] @numbers; # Product: 120 my @columns = <A B C>; my @rows = <1 2 3>; say @columns Z~ @rows; # (A1 B2 C3)
infix operator to all pairs of items from two lists: • Reverse meta operator (R?) reverses the order of the arguments of an infix operator: my @columns = <A B C>; my @rows = <1 2 3>; say @columns X~ @rows; # (A1 A2 A3 B1 B2 B3 C1 C2 C3) say 5 - 3; # 2 say 5 R- 3; # -2
an operator to one or two lists and return a list of results: • If one list is shorter, it can be auto-expanded: • Can also be used with unary operations: say (10,20,30,40,50) »+« (2,4,6,8,10); # (12 24 36 48 60) say (10,20,30,40,50) »+» (2,4,6); # (12 24 36 42 54) say -« (10,20,30,40,50); # (-10 -20 -30 -40 -50)
next level • A grammar is composed of tokens and rules • Example – a simple arithmetic expression rule: my token number { \d+ } my token op { < + - * / > } my rule expression { <number> <op> <number> } say(('42 - 13' ~~ /<expression>/).Bool); # True
support concurrency and asynchronous programming • Built-in features include threads, promises, supplies, and channels • This allows for reactive and event-driven programming in Perl 6
over data structures in parallel • So the grep line in this fragment of code: can be replaced with either of these: my @numbers = (1..100000); my @primes = @numbers.grep(*.is-prime); @primes = @numbers.hyper.grep(*.is-prime); @primes = @numbers.race.grep(*.is-prime);
the order of elements, while race does not • The batch size and degree of parallelism (i.e., the number of workers) can be set explicitly: @numbers.race(batch => 32, degree => 4).grep(*.is-prime);
results • Implemented by means of the Promise class: my $promise = Promise.new; say $promise.status; # Planned $promise.keep('As promised'); say $promise.status; # Kept say $promise.result; # As promised
code and awaiting its result: my @numbers = 1..100000; my $promise = start grep *.is-prime, @numbers; say "Doing something else"; my @primes = await $promise; say @primes.elems; # 9592
(« and ») , Perl 6 embraces the use of Unicode in program code • Unicode characters can be used in identifiers • π, τ are built-in terms in Perl 6: sub circumference(Numeric $r) { τ * $r }
• Unicode data support is exceptionally good: sub Σ(*@elements) { [+] @elements } say Σ(1, 2, 3); # 6 say 'α'.succ; # β say 'Viel Spaß!'.uc; # VIEL SPASS! say 'Yes' if 'foobar' ~~ /foo.bar/; # Yes
glorious CPAN (yet) • A list of existing modules (mostly hosted on GitHub) is published at https://modules.perl6.org/ • Can be installed with the Panda utility (https://github.com/tadzik/panda/)
5 modules in Perl 6 programs • Even complex modules, such as DBI (database access interface), work • Usage is as simple as: use Inline::Perl5; use DBI:from<Perl5>; say DBI.available_drivers; # [DBM ExampleP File ...
Your best (easiest) option is Rakudobrew • Similar to what Perlbrew is for Perl 5, and virtualenv for Python • Installation is as simple as: git clone https://github.com/tadzik/rakudobrew ~/.rakudobrew export PATH=~/.rakudobrew/bin:$PATH rakudobrew init