Intro to Perl 6 by Damian Conway

These are some lightly edited notes from a one-day intro to Perl 6 course given by Damian Conway. I had the pleasure of attending this session in summer 2009 at EPFL Lausanne, organized by my employer, the Swiss Institute of Bioinformatics.

If these short notes make you curious about Perl 6, see its website, and don’t miss the Perl 6 Advent Calendar, that shows off a lot of cool features – with code examples, in contrast to my notes.

First a nugget of wisdom that Damian Conway shared in the evening at the local Perl/Linux user group: Geeks are people who manipulate reality via language. In that, they are like the wizards in stories. Nice!

The big changes

  • Everything’s an object. That’s one of the biggest changes from Perl 5, however it’s very declarative and easy to use, and still hidden from view until you need it.Perl 6 is statically typed! This comes as a surprise, but its consequences are not drastic (if the programmer doesn’t want to) because of the Any type. It’s like Object in Java, but it’s implicit.

    If you want/need to be explicit: my Str $value.

    The type system has restrictions, a very cool feature: my $short_string of Str where {.chars < 10}.

    Each type is also a method that casts, as in print Str($obj).

  • The punctuation has been changed around in a “Huffmanization of punctuation”. They looked at a lot of CPAN code and made the most often used punctuation the shortest and easiest. And indeed most of the punctuation characters have a new meaning, so expect some re-learning if you’re a Perl 5 programmer.
  • Conway estimates that a Perl 6 program should be 20 to 40% shorter than the equivalent Perl 5 program.
  • It has blocks, written as {}, and they are first class. {} means “block” everywhere, even in strings.
  • Ah, sigils. Perl 6 still has them, but different from Perl 5: they never change for a given variable. For instance, an element of array @foo is now accessed as @foo[0]. Don’t we lose information here in comparison to $foo[0], namely that the foo contains scalar values? Conway
    says that this is true in comparison to Perl 4, but the references in Perl 5 already destroyed this.

    There is also the new sigil & for subs, but it’s not required for calling. Passing a sub looks like foo(&bar).

  • One of the most innovative parts of Perl 6 are Junctions. They are, if I understood correctly, Conway’s invention and are conceptually inspired by quantum physics. The analogy is that something can be in multiple states until it is observed. Transferred to a programming language, that gives us data-centric parallelism. The all, any, and none operators for lists execute the tests in parallel for each list element (with the actual number of threads depending on the hardware). If the list is of blocks, these are also executed in parallel. This gives a concise, readable, and safe to use parallelization facility.I have this hastily scribbled note saying that if a sub gets one of these lists or list expressions, |list| copies of it are executed in parallel – have to check what I meant with this exactly.
  • Perl 6 has extensive introspection:
    • .WHAT # type object
    • .WHERE
    • .WHICH
    • .HOW
    • .WHENCE # auto-vivifier
    • .WHY # comment

    For instance, $foo.WHAT.methods.

  • The language has built-in support for grammars and rules, essentially giving you a parser and lexer built into the language, using the normal (powerful) regular expressions.grammar G {
    rule R {} # whitespace significant
    token T {} # whitespace ignored

    The compiler is being written in this! So you can manipulate Perl 6 code easily as the parse tree is built in.

  • Perl 6 is defined in operational semantics via its comprehensive unit test suite. A compiler is a Perl 6 compiler when it passes all tests. There are currently about 20,000 of them; should be around 100,000!

I have a lot more notes about smaller features and nice syntactical sugar, that I cut out here not to bore the reader. Overall, I’m pretty excited about Perl 6 now, even though it’s not finished yet. It just has so many things that look so handy!

Interestingly, it’s kind of the antithesis to Lisp in that regard. It comes with a ton of syntactic constructs that do specific things, while Lisp is minimal and malleable so you can construct your own language. Both approaches have their merits, and both ways of developing software depend on how well they are executed. I think the Perl 6 designers have succeeded in making the standard language very well suited to writing elegant and succinct programs.


Tags: , , , ,

One Response to “Intro to Perl 6 by Damian Conway”

  1. Allison Randal: Exploring Dynamism « Juggling Bits Says:

    […] these days, it’s worth showing that cool things are still happening in that community. Like Perl 6 and Parrot. A virtual machine that can run 30+ languages? […]

Comments are closed.

%d bloggers like this: