|
Perl 6
Best Of Perl 6 Tablet: Revision 11
"Overview"[Perl 6 Tablets] - _Chapter:_ "0:Intr"[Perl 6 Intro Tablet], "1:Hist"[Perl 6 History Tablet], "2:Design"[Perl 6 Language Design Tablet], "3:Var"[Perl 6 Variables Tablet], "4:Op"[Perl 6 Operators Tablet], 5:IO, 6:{}, 7:Sub, 8:OOP, 9:Rx - _Appendices:_ "A:Index"[Perl 6 Index Tablet], "B:Tables"[Perl 6 Lookup Tablet], *"C:Best of"[Best Of Perl 6 Tablet]*, "D:Delta"[Perl 6 Delta Tablet], "E:Links"[Perl 6 Links Tablet]
---- {toc: } This is a small tour, showing the best features and improvements of Perl 6 in short examples, compared to similar working Perl 5 examples. ^^^ Output Making output will become simpler and easier for newbies. | Perl 5(.8) | Perl 6 | | print "bananas are good\n"; | say "bananas are good"; | | print "and I said: ".$quotes{"me"}.".\n"; | say "and I said: {%quotes<me>}."; | ^^^ File IO File::Slurp is in the Perl 6 core language and file reading is simpler then ever (and autochomp included). | Perl 5 | Perl 6 | | open my $FH, '<', "poetry.txt"; | $content = slurp "poetry.txt"; | | read $FH, $content, $block_len until eof; | | | close $FH; | | | open my $FH, '<', "poetry.txt"; | @content = lines "poetry.txt"; | | while (<$FH>) { chomp; push @content, $_; } | | | close $FH; | | ^^^ Automatic multithreading Applying 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. | Perl 5 | Perl 6 | | my $sum; | {{my $sum = [+] @numbers;}} | | $sum += $_ for @numbers; | | | for (0 .. $#factor1) { | {{@product = @factor1 >>*<< @factor2;}} | | $product[$] = $factor1[$] * $factor2[$_]; | | | } | | The Perl 5 code is a simplification, of course Perl6 "does the right thing" when the arrays have different lengths. ^^^ Comparsion Here are junctions, then chained comparison operators. | Perl 5 | Perl 6 | | {{if ($a = 3 or $a = 4 or $a = 7) {...} }} | if $a = 3 | 4 | 7 {...} | | {{if (4 < $a and $a < 12) {...} | if 4 < $a < 12 {...} | | if (4 < $a and $a <= 12) {...} | if $a ~~ 4^..12 {...} | The err operator eases lot of cases where Perl 5 newbies could fall into traps. | Perl 5 | Perl 6 | | $a = defined $b ? $b : $c; | $a = $b // $c; | ^^^ Case construct | Perl 5 | Perl 6 | | {{if ($a = 2 or $a = 5) {...} }} | given $a { | | {{elseif ($a = 6 or $a = 7 or $a = 8 or $a = 9) {...} }} | when 2 | 5 {...} | | {{elseif ($a =~ /g/) {...} }} | when 6 .. 9 {...} | | else {...} | when 'g' {...} | | | default {...} | | | } | It's clear to read, very versatile and when used in combination with junctions, becomes even clearer. ^^^ powerful for loops | Perl 5 | Perl 6 | | for my $i (1..15) {...} | for 1..15 -> $i {...} | | for (my $i=15; $i>1; $i-2) {} | {{for 1..15:by(-2)- > $i {...} }} | | for my $key (keys %hash) { | for %hash.kv -> $key, $value { | | print "$key => $hash{$key}\n"; ... | say "$key => $value"; ... | | for my $i (0..$#a) { | for zip(@a; @b; @c)- > $a, $b, $c {...} | | my $a = @a[$i]; | | | my $b = @b[$i]; | | | my $c = @c[$i]; | | | ... | | ^^^ named parameters in subroutines Perl 5: > sub routine { >> my $a = shift; >> my $b = shift; >> my @rest = @_; > } Perl 6: > sub routine ($a, $b, *@rest) {...} ^^^ Objects with auto generated new and getters and setters Simple 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; >> my $speed = shift; >> if (defined $speed) { $self->{speed} = $speed } >> else { $self->{speed} } > } > sub stop { >> my $self = shift; >> $self->{speed} = 0; > } Perl 6: > class Heart::Gold { >> has $.speed; >> method stop { $.speed = 0 } > } > my Heart::Gold $hg1 .= new; > $hg1.speed = 100; > my $hg2 = $hg1.clone; > $hg2.speed = 50; The new, getter, setter and clone methods are auto generated. Mthods now are called "method". ---- "Overview"[Perl 6 Tablets] - _Chapter:_ "0:Intr"[Perl 6 Intro Tablet], "1:Hist"[Perl 6 History Tablet], "2:Design"[Perl 6 Language Design Tablet], "3:Var"[Perl 6 Variables Tablet], "4:Op"[Perl 6 Operators Tablet], 5:IO, 6:{}, 7:Sub, 8:OOP, 9:Rx - _Appendices:_ "A:Index"[Perl 6 Index Tablet], "B:Tables"[Perl 6 Lookup Tablet], *"C:Best of"[Best Of Perl 6 Tablet]*, "D:Delta"[Perl 6 Delta Tablet], "E:Links"[Perl 6 Links Tablet] ---- |