<

Perl 6 - Features und Status

Die Zukunft ist hier, nur ungleichmässig verteilt

Moritz Lenz <moritz@faui2k3.org>

#perl6

Perl 6 - Übersicht

Was ist Perl 6?

Beziehung zu Perl 5

Compiler

Rakudo

Niecza

Vergleich von Rakudo und Niecza

Perl 6 Features: Basics

Junctions

my @dice = (1..6).roll(2);
if @dice.join eq '12' | '21' {
    #            ^^^^^^^^^^^  Junction
    say 'Mäxchen!';
}

Junctions: Typen

Subroutinen, Signaturen

sub greet($who) {
    say "hello $who"
}
greet("GPW");       # hello GPW
say &greet.WHAT;    # Sub()

Signaturen: "Slurpy"

sub up-join($sep, *@items) {
    join $sep, map &uc, @items
}
say up-join ", ", <a b c>   # A, B, C

Typen I

sub square(Numeric $x) {
    $x * $x
}
say square(42);     # 1764
my $x = 'foo';
square($x); # BOOM

Interessante Typen: Zahlen

Andere Typen

Typen II: Check zur Compile-Zeit

sub a(Int $x) { }; a "foo"
# ===SORRY!===
# CHECK FAILED:
# Calling 'a' will never work with argument
# types (str) (line 1)
# Expected: :(Int $x)

Typen weiter verfeinern

subset Positive of Real where { $_ > 0 };
sub circle-area(Positive $r) {
    $r * $r * pi;
}
say circle-area(3); # works
say circle-area(-3); # BOOM
# Constraint type check failed for parameter '$r'
#   in sub circle-area at /home/moritz/subset.pl:2

Multi Subroutinen

multi to-json(Real $x) { $x.Str }
multi to-json(@x) { 
    '[' ~ @x.map(&to-json).join(', ') ~ ']'
}
...

Multi Subroutinen: Dispatch

"Faule" Listen

my @even := (0..*).map: { $_ * 2 };
say @even[10];  # 20
my @es:= @even.grep: { $_.sqrt.Int ** 2 == $_ };
say @es[10];    # 400

Sequenzen

my @powers   := 1, 2, 4 ... *;
constant fib := 1, 1, { $^a + $^b } ... *;
say @powers[10];    # 1024
say fib[10];        # 89

Whatever-Blöcke

Whatever-Blöcke

Klassen

class Task {
    has &.action;
    has Bool $.done = False;
    has @.dependencies;

    method do {
        last if $.done;
        .do for @.dependencies;  # resolve dependencies
        &.action();              # perform the action
        $!done = True;
    }
}

Klassen: Objekte benutzen

my $root = Task.new(
    :action({ say 'Eat breakfast' }),
    :dependencies[
      Task.new(:action({ say 'Prepare breakfast' })),
      Task.new(:action({ say 'Set the table' })),
    ]
);
$root.do;

# output:
Prepare breakfast
Set the table
Eat breakfast

Klassen: Delegation

class Task {
    ...
    # method 'add-dependency' is handled
    # by @!dependenies.push
    has @.dependencies handles
        add-dependency => 'push';
    ...
}
...
$root.add-dependency(
    Task.new( :action( { say 'Wake girlfriend' } ) );
);

Rollen

Rollen

role Real { # for real numbers
    method abs  { self <= 0 ?? -self !! self }
    method sqrt { self.Num.sqrt }
    ...
}
class Int does Real { }
say 10.sqrt; # 3.16227766016838

Rollen: Konflikte

role A { method x { } };
role B { method x { } }; 
class C does A does B { };

# BOOM:
# Method 'x' must be resolved by class C because it exists in
# multiple roles (B, A)

Rollen als Interfaces

role A {
    method x { ...}  # tatsächlich ... hier
};
class B does A { };

# BOOM:
# Method 'x' must be implemented by 'B' because it is
# required by role A 

Regexes

my $text = 'This contains a double double word';
if $text ~~ / « (\w+) \s+  $0 » / {
    say "Duplicate word '$0' found";
}

# output: Duplicate word 'double' found

Grammatiken

Grammatiken

grammar Number {
    token TOP { ^ [ <int> | <hex> ] $ }
    token int { \d+ }
    token hex { '0x' (<[a..f A..F 0..9]>+) }
}
Number.parse('0xDEADBEEF');
say $/.keys;        # hex
say $<hex>[0].Str;  # DEADBEEF

Longest-Token Matching

Viel Mehr Features

http://perl6.org/compilers/features

Danke für eure Aufmerksamkeit!

ENDE

Fragen?