<

Perl 6

A Quick Introduction

Solomon Foster <colomon@gmail.com>

#perl6

Perl 6

Perl 6 Online

cards.pl (by Patrick Michaud)

my @suits  = <♣ ♢ ♡ ♠>;
my @ranks  = 2..10, <J Q K A>;

# concatenate each rank with each suit (2♣ 2♢ 2♡ ... A♠)
my @deck = @ranks X~ @suits;

# build a hash of card names to point values
my %points = @deck Z @( (2..10, 10, 10, 10, 11) Xxx 4 );

@deck .= pick(*); # shuffle the deck
my @hand = @deck.splice(0, 5); # grab first five cards
say ~@hand; # display my hand
say [+] %points{@hand}; # tell me how many points it's worth

cards.pl sample output

A♡ K♡ 3♡ 6♠ 10♣
40

Perl 6 compared to classic Perl

What about Perl 5?

What is Perl 6?

Compilers: Pugs

Compilers: Rakudo

Compilers: Niecza

Whirlpool Development

Signatures (I)

Signatures (II)

sub logarithm($num, $base = 2.7183) {
    return log($num) / log($base)
}
say logarithm(4);       # default base
say logarithm(4, 2);

Signatures (III)

sub swap($a is rw, $b is rw) {
    ($a, $b) = ($b, $a);
}
my $x = 1; 
my $y = 3;
swap($x, $y);
say $x;         # 3

Signatures (IIII)

sub rectangle(:$width!, :$height!, :$char = 'X') {
    say $char x $width for ^$height;
}

rectangle char => 'o', width => 8, height => 4;
rectangle :width(20), :height<5>;

Signatures (V)

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

Object System (I)

Object System (II)

role ABC::Duration {
    has $.ticks;
    our method duration-to-str() {
        given $.ticks {
            when Int { .Str; }
            when Rat { .perl; }
        }
    }
}

class ABC::Note does ABC::Duration {
    has $.pitch;
    method Str() { $.pitch ~ self.duration-to-str; }
}

Grammars (I)

Grammars (II)

grammar ABC::Grammar
{
    regex basenote { <[a..g]+[A..G]> }
    regex octave { "'"+ | ","+ }
    regex accidental { '^' | '^^' | '_' | '__' | '=' }
    regex pitch { <accidental>? <basenote> <octave>? }

    regex number { <digit>+ }
    regex note_length_denominator { '/' <bottom=number>? }
    regex note_length { <top=number>? <note_length_denominator>? }
    regex note { <pitch> <note_length> }
}

Grammars (III)

Grammars (IIII)

class ABC::Actions {
    method note_length($/) {
        if $<note_length_denominator> {
            make duration-from-parse($<top>[0], $<note_length_denominator>[0]<bottom>[0]);
        } else {
            make duration-from-parse($<top>[0]);
        }
    }
    method note($/) {
        make ABC::Note.new(~$<pitch>, 
                           $<note_length>.ast, 
                           $<tie> eq '-');
    }
}

Grammars (V)

Smartmatching (I)

Smartmatching (II)

Switch statement (I)

Switch statement (II)

given $.ticks {
    when 1 { ""; }
    when 1/2 { "/"; }
    when Int { .Str; }
    when Rat { .perl; }
    die "Duration must be Int or Rat, but it's { .WHAT }";
}

Switch statement (III)

Switch statement (IIII)

my $boring-lines = 0;
for $*IN.lines {
    when /"Lunasa" | "Altan"/ { say "Found band!"; }
    when /"fiddle" | "flute"/ { say "Found instrument!"; }
    $boring-lines++;
}

Whatever

Whatever Closure (I)

Whatever Closure (II)

Lazy lists (I)

Lazy lists (II)

Lazy lists (III)

Meta operators (I)

Meta operators (II)

Meta operators (III)

Meta operators (IIII)

Meta operators (V)

Creating new operators

A word about operators (I)

A word about operators (II)

Perl 6 Online Again