|
Perl 6
Best Of Perl 6 Tablet: Revision 10
Overview - Chapter: 0:Intr, 1:Hist, 2:Design, 3:Var, 4:Op, 5:IO, 6:{}, 7:Sub, 8:OOP, 9:Rx - Appendices: A:Index, B:Tables, C:Best of, D:Delta, E:Links This is a small tour, showing the best features and improvements of Perl 6 in short examples, compared to similar working Perl 5 examples. OutputMaking output will become simpler and easier for newbies.
File IOFile::Slurp is in the Perl 6 core language and file reading is simpler then ever (and autochomp included).
Automatic multithreadingApplying operations to junctions and arrays is now syntactically compact and readable. Perl 6 will create threads where appropriate to use multiple processors, cores or hyperthreading for high level language SIMD concurrent processing.
The Perl 5 code is a simplification, of course Perl6 "does the right thing" when the arrays have different lengths. ComparsionHere are junctions, then chained comparison operators. Perl 5: if ($a = 3 or $a = 4 or $a = 7) {...} Perl 6: if $a = 3|4|7 {...} Perl 5: {{if (4 < $a and $a < 12) {...} Perl 6: if 4 < $a < 12 {...} Perl 5: if (4 < $a and $a <= 12) {...} Perl 6: if $a ~~ 4^..12 {...} The err operator eases lot of cases where Perl 5 newbies could fall into traps. Perl 5: $a = defined $b ? $b : $c; Perl 6: $a = $b // $c; Case constructPerl 5: if ($a = 2 or $a = 5) {...} Perl 6: given $a {when 2 | 5 {...}} It's clear to read, very versatile and when used in combination with junctions, becomes even clearer. powerful for loopsPerl 5: for my $i (1..15) {...} Perl 6: for 1..15 -> $i {...} Perl 5: for (my $i=15; $i>1; $i-2) {} Perl 6: for 1..15:by(-2)- > $i {...} Perl 5: for my $key (keys %hash) {print "$key => $hash{$key}\n"; ... Perl 6: for %hash.kv -> $key, $value {say "$key => $value"; ... Perl 5: for my $i (0..$#a) {my $a = @a[$i]; Perl 6: for zip(@a; @b; @c)- > $a, $b, $c {...} named parameters in subroutinesPerl 5: sub routine {my $a = shift;} Perl 6: sub routine ($a, $b, *@rest) {...} Objects with auto generated new and getters and settersSimple Object creation is now as easy as it gets. Perl 5: package Heart::Gold; sub new {bless {speed => 0 }, shift;} sub speed {my $self = shift;} sub stop {my $self = shift;} Perl 6: class Heart::Gold {has $.speed;} The new, getter, setter and clone methods are auto generated. Mthods now are called "method". Overview - Chapter: 0:Intr, 1:Hist, 2:Design, 3:Var, 4:Op, 5:IO, 6:{}, 7:Sub, 8:OOP, 9:Rx - Appendices: A:Index, B:Tables, C:Best of, D:Delta, E:Links |