|
Perl 6
Best Of Perl 6 Tablet: Revision 29
"Intro"[Perl 6 Intro Tablet] - _Chapter:_ "0:History"[Perl 6 History Tablet] "1:Design"[Perl 6 Language Design Tablet] "2:Basics"[Perl 6 Basics Tablet] "3:Var"[Perl 6 Variable Tablet] "4:Op"[Perl 6 Operator Tablet] "5:IO"[Perl 6 IO Tablet] "6:{}"[Perl 6 Block Tablet] "7:Sub"[Perl 6 Subroutine Tablet] "8:OOP"[Perl 6 OOP Tablet] "9:Rx"[Perl 6 Regex Tablet] "10:Meta"[Perl 6 Meta Tablet]
"Overview"[Perl 6 Tablets] - _Appendices:_ "A:Index"[Perl 6 Index Tablet] "B:Tables"[Perl 6 Lookup Tablet] "C:Cook"[Perl 6 Cookbook Tablet] "D:Delta"[Perl 6 Delta Tablet] *"E:Best of"[Best Of Perl 6 Tablet]* "F:Links"[Perl 6 Links Tablet] "G:Glossary"[Perl 6 Glossary 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. ^^^ Command Line "Making output"{link: [Perl 6 IO Tablet]Command Line} will become simpler and easier for newbies. .pre Perl 5 Perl 6 print "bananas are good\n"; say "bananas are good"; print "and I said: $quotes{\"me\"}\n"; say "and I said: %quotes{"me"}."; print "and I said: $quotes{\"me\"}\n"; say "and I said: %quotes<me>."; print "What is ... "; $result = prompt "What is ... "; chomp($result = <>); .pre ^^^ File IO File::Slurp is in the Perl 6 core language and "file reading"{link: [Perl 6 IO Tablet]File} is simpler then ever (and autochomp included). .pre Perl 5 Perl 6 $content = do { local $/; $content = slurp "poetry.txt"; open my $FH, "poetry.txt"; <$FH> }; chomp(@content = do { @content = lines "poetry.txt"; open my $FH, "poetry.txt"; <$FH> }); .pre ^^^ 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. .pre Perl 5 Perl 6 my $sum; my $sum = [+] @numbers; $sum += $_ for @numbers; for (0 .. $#factor1) { @product = @factor1 >>*<< @factor2; $product[$] = $factor1[$] * $factor2[$_]; } .pre The Perl 5 code is a simplification, of course Perl6 "does the right thing" when the arrays have different lengths. ^^^ Comparison Here are junctions, then chained comparison operators. .pre 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 {...} $a = defined $b ? $b : $c; $a = $b // $c; .pre The defined-OR operator eases lot of cases where Perl 5 newbies could fall into traps. ^^^ Case construct .pre Perl 5 Perl 6 given $a { if ($a == 2 or $a == 5) {...} }} when 2 | 5 {...} elsif ($a == 6 or $a == 7 or $a == 8 or $a == 9) {} when 6 .. 9 {...} elsif ($a =~ /g/) {...} when 'g' {...} else {...} default {...} } .pre "That new construct"{link: [Perl 6 Block Tablet]given} (backported to 5.10) is clear to read, very versatile and when used in combination with junctions, becomes even clearer. ^^^ Powerful for loops "List iteration via for"{link: [Perl 6 Block Tablet]for} is now much more versatile. .pre Perl 5 Perl 6 for my $i (0..15) {...} for ^16 -> $i {...} for (my $i=15; $i>1; $i-2) {...} for 15,*-2...1 -> $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]; ... .pre ^^^ Named parameters in subroutines .pre Perl 5 Perl 6 sub routine { sub routine ($a, $b, *@rest) {...} my $a = shift; my $b = shift; my @rest = @_; } .pre ^^^ Objects with auto generated new and getters and setters Simple Object creation is now as easy as it gets. .pre Perl 5 Perl 6 package Heart::Gold; class Heart::Gold { has $.speed; sub new { method stop { $.speed = 0 } bless {speed => 0 }, shift; } } my Heart::Gold $hg1 .= new; sub speed { $hg1.speed = 100; my $self = shift; my $hg2 = $hg1.clone; my $speed = shift; if (defined $speed) { $self->{speed} = $speed } else { $self->{speed} } } sub stop { my $self = shift; $self->{speed} = 0; } .pre The new, getter, setter and clone methods are auto generated. Methods now are called "method". ---- "Intro"[Perl 6 Intro Tablet] - _Chapter:_ "0:History"[Perl 6 History Tablet] "1:Design"[Perl 6 Language Design Tablet] "2:Basics"[Perl 6 Basics Tablet] "3:Var"[Perl 6 Variable Tablet] "4:Op"[Perl 6 Operator Tablet] "5:IO"[Perl 6 IO Tablet] "6:{}"[Perl 6 Block Tablet] "7:Sub"[Perl 6 Subroutine Tablet] "8:OOP"[Perl 6 OOP Tablet] "9:Rx"[Perl 6 Regex Tablet] "10:Meta"[Perl 6 Meta Tablet] "Overview"[Perl 6 Tablets] - _Appendices:_ "A:Index"[Perl 6 Index Tablet] "B:Tables"[Perl 6 Lookup Tablet] "C:Cook"[Perl 6 Cookbook Tablet] "D:Delta"[Perl 6 Delta Tablet] *"E:Best of"[Best Of Perl 6 Tablet]* "F:Links"[Perl 6 Links Tablet] "G:Glossary"[Perl 6 Glossary Tablet] ---- |