git clone http://github.com/rakudo/rakudo.git cd rakudo perl Configure.pl --gen-parrot make install export PATH=$PATH:`pwd`/parrot_install/bin perl6 -e 'say "success"'
{...}, subs mit subif, for, while wie in Perl 5say, given/when, smart matching nach 5.10 rück-portiert
strict als Voreinstellung@array => @array[$index]if ($condition) { } => if $condition%hash{$var} für Variablen, %hash<literal> für Literalefor my $x (@list) { ... } => for @list -> $x { ... } (lambdas)
use strict; use warnings; my %words; while (my $line = <STDIN>) { for ($line =~ /(\w+)/g) { $words{$1}++; } } my @top_keys = sort { $words{$b} <=> $words{$a} } keys %words; for (@top_keys[0..9]) { print "$words{$_}\t$_\n"; }
use v6; my %words; for $*IN.lines -> $line { for $line.words { %words{$_}++; } } my @top_pairs = %words.sort: { -.value }; for @top_pairs[^10] { say .value, "\t", .key; }
while (my $line = <STDIN>) => for $*IN.lines -> $line$*IN: "contextual" variable, in dynamischen und lexikalischen Scopes
überschreibbar$*IN.lines: "lazy" Liste der Zeilen (ohne \n)-> $line { ... }: "pointy block" mit Signatur
for ($line =~ /(\w+)/g) => for $line.wordsStr.words ist neu, könnte man auch als
Str.comb(/\S+/) schreiben$words{$_}++ => %words{$_}++: Sigil invariant
my @top_keys = sort { $words{$b} <=> $words{$a} } keys %words;
=> my @top_pairs = %words.sort: { -.value };List.sort sortiert nach dem Wert, den eine 1-argument-Closure
zurückgibt.value: Kurz für $_.value@top_keys[0..9] => @top_pairs[^10]^10 ist kurz für 0..9say .value, "\t", .key; Methodenaufrufen auf das Pair-Objekt in $_
.foo ist kurz für $_.foo
sub logarithm($num, $base = 2.7183) { return log($num) / log($base) } say logarithm(4); # Benutzt default say logarithm(4, 2);
sub swap($a is rw, $b is rw) { ($a, $b) = ($b, $a); } swap(my $x = 1, my $y = 3); say $x; # 3 swap(3, 4); # sollte sterben, RT #74820
sub rectangle(:$width!, :$height!, :$char = 'X') { say $char x $width for ^$height; } rectangle char => 'o', width => 8, height => 4; rectangle :width(20), :height<5>;
multi sub what(Int $x) { say "Int" } multi sub what(Str $x) { say "Str" } multi sub what($x) { say "Something else" } what('foo'); # Str what([]); # Something else
multi what(Int $) { say "Int" } multi what(Str $) { say "Str" } multi what($) { say "Something else" } what('foo'); # Str what([]); # Something else
multi postfix:<!>(Int $x) { $x * ($x-1)! } multi postfix:<!>(Int $x where { $x == 0}) { 1 } say 5!; # 120
multi postfix:<!>(Int $x) { $x * ($x-1)! } multi postfix:<!>(0) { 1 } say 5!; # 120
class Entrepreneur { has $.balance = 0; method pay ($amount) { die "bankrupt" if $amount > $!balance; say "Paying $amount Euro"; $!balance -= $amount; } } my $e = Entrepreneur.new(balance => 100); $e.pay(30); say $e.balance; # 70 $e.pay(120); # bankrupt
class MeineKlasse { ... } definiert Klasseclass A is B { ... } Vererbunghas $!private-attribute Privates Attributehas $.foo = has $!foo; method foo { $!foo }
method die-methode { ... } hat Zugriff auf
self und Attributenew
$!attrib, öffentlicher Zugriff $.attrib'is
?, +, *{12,23} => ** 12..23{3,} => ** 3..*
:sigspace (oder :s) modifier durch <.ws> ersetzt
( ) bleibt(?: ) => [ ][a-z] => <[a..z]><alpha>, <panic("Syntax error")>'...' und "..."
"abc" ~~ / ( a (b)) (c) /$/$0 = $/[0], $1 = $/[1] etc.$/[0] eq "ab", $/[0][0] eq "b",
$/[1] eq "c"'abc23' ~~ /<alpha>+/$/<alpha> = $<alpha><alpha> quantifiziert ist, ist $<alpha> eine Liste
grammar URI { token TOP { <schema> '://' <host> [ ':' <port> ]? <path> }; token schema { <[a..z]>+ } token host { [\w+] ** \. } token port { \d+ } token path { '/' \S* } } my $m = URI.parse('http://perl6.org/'); say $m<schema>; # http say $m<path>; # /
grammar URL::HTTP is URI { token schema { 'http' } } for <http://perl6.org/ ftp://cpan.org/> -> $url { my $m = URL::HTTP.parse($url); print "NOT " unless $m; say "parsed $url"; }
grammar URI { token TOP { <schema> '://' <host> [ ':' <port> ]? <path> }; proto token schema { <...> } token schema:sym<http> { <sym> } token schema:sym<https> { <sym> } token schema:sym<ftp> { <sym> } ... }
grammar URI::Other is URI { # durch Vererbung neue Alternative hinzugefügt token schema:sym<irc> { <sym> } } for URI, URI::Other -> $grammar { for <http://perl6.org/ irc://freenode.org/#perl6> -> $url { say $grammar.parse($url) ?? '[X]' !! '[ ]', " $grammar.perl(), $url"; } }
ast Attribut, das mit make() gesetzt werden
kann
class URI::Storage { has $.schema; has $.host; has $.port; has $.path; method Str { $.schema ~ '://' ~ $.host ~ ':' ( $.port || 80 ) ~ $.path; } }
class URI::Actions { method TOP($/) { my %attr; for <schema port path host> { %attr{$_} = ~$/{$_}; } %attr<port> = ~$<port>[0] if $<port>; make URI::Storage.new(|%attr); } }
my $m = URI.parse('http://perl6.org/', :actions(URI::Actions.new)); my $uri = $m.ast; say $uri.WHAT; # URI::Storage() say $uri.schema; # http say $uri; # http://perl6.org:80/
ENDE