Perl och Raku - de första 50 åren




B3 Init

Jonas Linde <jonas.linde@b3.se>

Agenda

[dominoes]

Perl

[Programming Perl]

Historik

[Programming Perl]

Logotyper

Logotyper

Bakåtkompatibilitet

[Perl Camel]

Objektorientering

[Perl Camel]

Webbramverk

[Perl Camel]

Perl 44

[Perl Camel]

Raku

Camelia butterfly

Tidslinje

Camelia butterfly

Tidslinje

Camelia butterfly

Sigill

Sigillen $, @, % och & anger variabeltyp - samma i Perl och Raku

Camelia butterfly

Twigill

Utöver sigillen $, @, % och & finns sekundära sigill:

Camelia butterfly

Twigill-exempel

Camelia butterfly

Typning

Camelia butterfly

233 typer

Camelia butterfly

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
Camelia butterfly

Unicode i koden

> say ¼
0.25

> say 2³
8

> say ٧ + ٣
10

> say τ
6.28318530717959

> my \π = 4;
> say π;
4
Camelia butterfly

Unicode i data

Camelia butterfly

Promise

> 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]
Camelia butterfly

Supply

> 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
Camelia butterfly

Channel

> 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
Camelia butterfly

Proc::Async

> 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.
Camelia butterfly

Procedurell stil

> sub postfix:<!> { [*] 1..$^n }
> say 5!;

120
Camelia butterfly

Objektorienterad stil

> class Trip is Journey does Transport {
>     has $.origin;
>     has $.destination;
>     has @!travellers;
>     has $.notes is rw;
>
>     method go(Rat $speed) { … }
>     method !homesick { … }
> }
Camelia butterfly

Funktionell stil

Camelia butterfly

Metaprogrammering

Camelia butterfly

Regexes

Camelia butterfly

Grammars

> 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
Camelia butterfly

Towards a Raku Foundation

Camelia butterfly

Tack för ordet!

413 Request entity too large [more dominoes]