Rakudo and Perl 6

by Gabor Szabo
http://szabgab.com/

Minor changes by Daniel Allen
for http://kw.pm.org/

Thursday, 17 June 2010

Perl 6 vs Perl 5

  • Perl 6 is a new dialect of the Perl language family, a sibling of Perl 5.
  • As opposed to Perl 5, Perl 6 has a definition and can have several implementations.
  • Currently the most advanced implementation is called Rakudo.
  • It runs on top of the Parrot Virtual Machine.

Rakudo

  • "Rakudo" is short for "Rakuda-dō" (with a long 'o'; 駱駝道), which is Japanese for "Way of the Camel".
  • "Rakudo" (with a short 'o'; 楽土) also means "paradise" in Japanese.

Status

  • Both Perl 6 and Rakudo are under heavy development.
  • Rakudo *, the first reasonably usable version of it
  • Planned to be relesed in the next few weeks

Compability

  • Perl 6 is not compatible with Perl 5 but there are several plans for transition
  • Create a Perl 5 to Perl 6 translator.
  • perl will recognize Perl 5 and Perl 6 code and use the right engine

use v6;

Hello World


say "Hello World";

Everything is an object


say "Perl 6 rocks".chars;

Reading from STDIN, prompting


my $name = prompt("Please type in yourname: ");

say "Hello $name";

Interpolation expressions


    my @names = ("Foo", "Bar", "Moo");
    say "Hello { join(', ', @names) } how are you?";
  • The output will look like this:
    Hello Foo, Bar, Moo how are you?

if condition


my $age = 23;
if $age > 18 {
   say "You can drink beer with Perl Mongers.";
}
  • No parenthesis needed

Chained comparison


if 23 <= $age and $age <= 42 {
}
  • better written as

if 23 <= $age <= 42 {
}

Junctions

  • Instead of

    if $age == 3 or $age == 4 or $age == 5 {
    }
  • you will be able to write
    if $age == 3|4|5 {
    }

for loop - One element a time

    my @fellows = <Foo Bar Baz>;
    for @fellows -> $name {
        say $name;
    }

for loop - Two elements a time


    my @phones = <Foo 123 Bar 456 Moo 789 Foo 976>;
    for @phones -> $name, $number {
        say "$name  $number";
    }

for loop - Going over elements of a hash


    my %phone = (
        "Foo" => 123,
        "Bar" => 456,
    );

    for %phone -> $key, $value {
        say "$key $value";
    }

Several elements at a time

    my @scores = <
        Valencia         1  1  Recreativo_Huelva
        Athletic_Bilbao  2  5  Real_Madrid
        Malaga  	     2  2  Sevilla_FC
        Sporting_Gijon   3  2  Deportivo_La_Coruna
     	Valladolid 	     1  0  Getafe
     	Real_Betis 	     0  0  Osasuna
     	Racing_Santander 5  0  Numancia
     	Espanyol         3  3  Mallorca
     	Atletico_Madrid  3  2  Villarreal
     	Almeria          0  2  Barcelona
    >;

    for @scores -> $home, $home_score, $guest_score, $guest {
        say "$home $guest $home_score : $guest_score";
    }

Missing values

    for (1, 2, 3, 4, 5) -> $x, $y {
        say "$x $y";
    }
  • Will generate the following:
    1 2
    3 4
    StopIteration
  • But we can say the second value is optional:
    for (1, 2, 3, 4, 5) -> $x, $y? {
            say "$x $y";
    }

Iterating over more than one array in parallel

    my @chars   = <a b c>;
    my @numbers = <1 2 3>;

    for @chars Z @numbers -> $letter, $number {
        say "$letter $number";
    }

    a 1
    b 2
    c 3

Infinite lists


    for (1..Inf) -> $i {

    }

Twigils and special variables

$*PROGRAM_NAME
  • the path to the currently running Perl 6 program
$*CWD
  • current working directory.
$*IN
  • Is the standard input (STDIN). You can read a line using $*IN.get
@*ARGS

Subroutines

  • Signature
sub add ($a, $b) {
    return $a + $b;
}

say add(2, 3);      # returns 5

#say add(2);        # is an error
#say add(2, 3, 4);  # is an error

  • Another example
sub process_template($input, $output, %params) {
}

multi method dispatch


multi sub process_template($input, %params) {
}

multi sub process_template($input, $output, %params) {
}

Optional parameters

multi sub process_template($input, $output, %params?) {
}

Type restriction

sub abc(Int $x) { say 1 };
abc(2);    # 1

abc("a");  #  Nominal type check failed for parameter '$x'; expected Int but got Str instead

Constraints

sub abc($x where {$^a < 2} ) { 
    say $x
}; 

abc(1); # 1

abc(2); # Constraint type check failed for parameter '$x'

Meta Operators

  • Reduction operator
say [*] 1..10;
  • Hyper operators
my @x = (1, 2) >>+<< (3, 4);
say @x.perl;  # [4, 6]
  • Cross operators
my @x = (1, 2) X+ (4, 7);
say @x.perl;        # [5, 8, 6, 9]

Create operators

our sub infix:<rrr>($a, $b) { 
    $a + 2* $b
};

say 2 rrr 3;        # 8
  • Get meta operators
say [rrr] 1,1,1;    # 5

Regexes - Matching

my $c = "What was the colour of the cat?";
if ($c ~~ m/colour/) {
    say "Matching '$/'";                      # 'colour'
}

Named subrules


regex num { \d ** 1..3 };

if 10 ~~ m/ <num> / {
} 

tokens and rules

  • They are regex-es that do not backtrack

    $string ~~ regex { \d+ }
    $string ~~ token { \d+ }
    $string ~~ rule { \d+ }
  • and these are equivalent to

    $string ~~ m/^ \d+ $/;
    $string ~~ m/^ \d+: $/;
    $string ~~ m/^ <.ws> \d+: <.ws> $/;

Grammars

  • Inheritable set of regexes, rules and tokens
  • "A grammar is a collection of rules, like a class is a collection of methods."

Rakudo *

  • Very easy install of git candidate
  • roadmap
  • 8 high-priority items
  • including a few "low degree of effort": eg. "attention-grabbing examples"
  • many other items to do (mostly before Rakudo * release)
  • szabgab++ for giving the nudge to present, and for his slides! (to build: fork that git, run "perl6 bin/generate.p6 slides/perl6.txt")

How to get involved?

Questions?


Gabor Szabo
http://szabgab.com