![[O'Reilly Camel]](img/camel.png)
![[TPRF Onion]](img/onion.png)
![[Perl 5 raptor]](img/perl5raptor.png)
![[New Perl Camel]](img/camel-new.png)
Sigillen $, @, % och & anger variabeltyp - samma i Perl och Raku
Utöver sigillen $, @, % och & finns sekundära sigill:
Java - statisk typning:
private static float foo(boolean a, String b) { ... }
Perl 5 - dynamisk typning:
sub foo { my ($a, $b) = @_; ... }
Perl 6 - gradvis typning:
sub foo($a, Str $b) returns Rat { ... }
> print 1/10
0
> print 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1
0.9999999999999999
> s:=0.; for i:=0; i<10; i++ {s+=.1}; fmt.Println(s);
0.9999999999999999
> console.log(1/10+1/10+1/10+1/10+1/10+1/10+1/10+1/10+1/10+1/10);
0.9999999999999999
> IO.puts 1/10+1/10+1/10+1/10+1/10+1/10+1/10+1/10+1/10+1/10;
0.9999999999999999
> my $a = 0; for (1..10) {$a = $a + 1/10}; say $a;
1
> say ¼ 0.25 > say 2³ 8 > say ٧ + ٣ 10 > say τ 6.28318530717959 > my \π = 4; > say π; 4
strängar konverteras till “Normalization Form Grapheme”
> say "a\c[COMBINING RING ABOVE]" eq "å" True > say "\r\n".chars 1 > say "\r\n" eq "\n" False
vill man hantera bytes kan man använda typen Buf
utf-8 är default vid konvertering
> my @promises;
> for 1..5 -> $t {
> push @promises, start {
> sleep $t;
> my $r = rand;
> die if $r < 0.2;
> };
> }
> await Promise.allof(@promises);
> say @promises>>.status;
[Kept Kept Kept Kept Broken]
> my $supply = supply {
> for 1 .. 10 {
> emit($_);
> }
> }
> $supply.tap(->$v { print "$v " }); say '';
> $supply.tap(->$v { print "$v " }); say '';
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
> my $channel = Channel.new;
> start {
> my $closed = $channel.closed;
> loop {
> last if $closed;
> print $channel.receive, ' ';
> }
> say '';
> }
>
> for ^10 -> $t {
> sleep $t;
> $channel.send($t);
> }
> $channel.close;
0 1 2 3 4 5 6 7 8 9
> my $proc = Proc::Async.new(:w, 'grep', 'foo');
>
> $proc.stdout.tap(-> $v { print "Output: $v" });
> $proc.stderr.tap(-> $v { print "Error: $v" });
>
> my $promise = $proc.start;
> $proc.say("this line has foo");
> $proc.say("this one doesn't");
> $proc.close-stdin;
> await $promise;
>
> say "Done.";
Output: this line has foo
Done.
> sub postfix:<!> { [*] 1..$^n }
> say 5!;
120
> class Trip is Journey does Transport {
> has $.origin;
> has $.destination;
> has @!travellers;
> has $.notes is rw;
>
> method go(Rat $speed) { … }
> method !homesick { … }
> }
Abstract Syntax Tree:
> my $ast = "42 + 666".AST.statements.head.expression;
> say $ast;
RakuAST::ApplyInfix.new(
left => RakuAST::IntLiteral.new(42),
infix => RakuAST::Infix.new("+"),
right => RakuAST::IntLiteral.new(666)
)
Perl:
> $url ~= m|http://([^:/]+)(:(\d+))?|i; > system "openssl s_client", "-host", $1, "-port", $3||443;
Raku:
> $url ~~ /^ "http://" $<host> = (<-[:/]>+) [":" $<port> = (\d+)]? /; > $url ~~ m :ignorecase / > ^ "http://" > $<host> = ( <-[:/]> + ) > [ ":" $<port> = ( \d + ) ]? > /; > my $sclient = run <openssl s_client -host>, $<host>, > "-port", $<port>//443, :out, :in, :err;
> grammar Calculator {
> token TOP { [ <add> | <sub> ] }
> rule add { <num> '+' <num> }
> rule sub { <num> '-' <num> }
> token num { \d+ }
> }
>
> class Calculations {
> method TOP ($/) { make $<add> ?? $<add>.made !! $<sub>.made; }
> method add ($/) { make [+] $<num>; }
> method sub ($/) { make [-] $<num>; }
> }
>
> say Calculator.parse('2 + 3', actions => Calculations).made;
5