Grammars in Perl 6

I don’t intend to make it a habit to post solely link posts here, but I felt I had to make an exception for today’s Perl 6 Advent Calendar post. (I don’t intend to limit myself to Perl 6 either, by the way, it’s just easy during the month of December.)

It’s about grammars in Perl 6, which are both groups of regexes and actually for all intents and purposes similar to parser grammars. People claimed that regular expressions were inadequate to solve parsing needs but just easy and approachable enough to make them more attractive than just writing a parser, and they were right. Perl 6 now does a fine job of attempting to solve this by rebuilding regular expressions into actual parsers, and making parsing easy by being a first-class citizen of the language.

How first-class? As shown by the final gift under last year’s tree, Perl 6’s own syntax is enshrined in a Perl 6 grammar. If it can parse Perl (if Perl 6 and not Perl 5), it’s probably good enough for most things you want to throw at it.

The Grand Compromise

If you’ve been following this year’s Perl 6 advent calendar, you may be noticing a sort of theme compared to last year’s: it’s now significantly more about minutia, whereas the 2009 edition focused on all the things Perl 6 gave you. I know I did.

At first I was aghast. My first instinct with Perl 6 has been to show people the new cool stuff in it, to justify the long wait we’ve had to endure, as if to say “it was worth it”, but mostly to affirm that I wasn’t foolish in believing that something would eventually materialize. The new priorities fly in the face of these efforts.

But something started to dawn on me over the course of several days. Much of the informed ridicule of Perl 6 mirrors the informed ridicule of Perl 5: it is too much its own creature, unlike that which we know, for the benefit of what exactly? But where the Perl 5 questions were sensible questions about needing to explicitly mention references (or be able to nest data structures) or the tendency to add a syntax for every feature (form formatting and the magic $a and $b in sorter subs spring to mind), Perl 6 questions are overwhelmingly about grandeur and unification. There’s a type system, unless you wish it away; You can redefine the language grammar; What’s the deal with variable sigils now that there are types, and what’s up with junctions anyway?

Corners of Perl 6, even those seemingly furthest away from the hoopla with lazy lists and making references standard, shine with brilliance in how they attempt to invent pinned-down concepts that stay consistent across the entire language and scale from soup to nuts, solving different problems along the way. The problem is that this takes a lot of time and effort to try to nail down adequately.

The thing that will help Perl 6 success more than anything right now isn’t blindly promoting adoption, but to dedicate resources to working through these concepts. To prove that it can all work out together, and that even if you don’t recognize or worry about the complexity of the whole language, it is coherent, does the right thing and is backed by intentional and specified concepts. For everything about list access sigils and variable scopes, that might be the biggest change from Perl 5.