• Ian Stuart's picture

    Perl Pre-conference tutorial: Intro to Perl 6

    Ian Stuart / August 15, 2018
  • Perl conference, 14th-17th August, Glasgow.

    Perl 5 & Perl 6 are essentially the same language. Yes, there are some differences (using `.` rather than `->` for object references for example, however 95% of what your "perl-head" will still be valid)

    A lot of the `Moose` stuff from perl 5 is actually Perl 6 concepts & constructs back-ported...

    Perl 6 is generally as fast as Perl 5.... rarely slower.

    (I was fascinated by the comment "Perl6 is essentially a regex interpreter - the whole language is parsed using a Perl6 regex")

    There are a bunch of changes... some of which I don't like, some of which I do....

    As the dot-operator is now the method-chain operator, it can no longer be the string-concatination operator, so `say "foo". " bar"` is now `say "foo" ~ " bar"` (noteing that say is the later perl function for print-with-newline)

    Sigils (the character at the front of variable names, which indicate the variable type) are now consistent for the variable, and don't change when refering to content.

    • my @foo = qw|a b c d e|; my $bar=@foo[4];
    • my %foo = { first: 'Ian', second: 'Stuart', age => 21 }; my $age = $foo<age>
    • $doctor[0]{first} in perl5 becomes either @doctor[0]{'first'} or @doctor[0]<first> - they are identical

    The whole quoted strings thing has been vastly overhauled - as well as the existing `qw` and `qq`, you now have `<>` for allomorphs, `qww` for `qw-that-understands-quotes`, `qqw` got `qw-with-interpolation`, `qqww` for `qw-that-understands-quotes-and-does-interpolation` (which can be written as `<< >>` [or even `«»`, 'cos Perl6 is proper Unicode])

    `Heredocs` have been pulled into quoting:

    my $text = q:to/END/; 
    Here is
    some multi-line
    string
    END

    and I've never heard "<" called "Pulp Fiction Brackets" before!

    Perl6 drops (or makes optional) the "parental" braces for may things:

    • my @foo =
          { .... },
          { .... },
          { .... };
    • if $a == $b { ... };

    Perl6 does multi-process (not threading!) by default (eg, lists fork of multiple sub-processes as they work away)

    Funtions

    Functions have become a whole lot more interesting!

    * Functions can have defined params: sub foo($i, $j) { ... }
    * Those params can be typedsub foo( Int $i, Int $j) { ... }
    * Those params can be validatedsub foo(Int $i, Int $j * < 10) { ... }

    Functions can also me "multi" (kinda like Java needs):

    multi plus (Int $a, Int $b) {}
    multi plus (Str $a, Str $b, $Str $c) {}

    So given the above two:

    • my $foo = plus(1, 2) is fine
    • my $foo = plus("aaa", "bbb", "ccc") is also fine
    • my $foo = plus(1, 2, 3) will fail (no 3-Int param option)

    Perl6 always passes by reference - so passing in an 8,000 element array doesn't create a 8,000 element copy, but a copy of the reference to the array.

    Function calling:

    my $b = pop @a is the same as my $b = @a.pop

    for-loop assignments:

    Get every other element: for 0..15 -> $a $b {print $b}

    You can reference sub-arrays directly: for ((1,2),(3,4),(5,6)) -> [$a, $b] {print $b}

    Object Orientated.

    As mentioned above - if you know Moose, then much of the Perl6 OO stuff is familiar: we have Classes & Roles. We also have Methods (which implicitly get a `self` - it's no longer a parameter to manage) and (finally) real private methods.