Perl 6 in a hands-on test

Double Down

Article from Issue 191/2016

Perl 6 gives the Perl 5 syntax a new paint job and introduces a genuine object system. Mike gives it a hands-on test based on a re-implementation of the old blackjack game.

You should never write off the old dogs: After decades of anticipation, Perl 6 finally seems to be taking shape. I found this out recently when I attended the grass roots Perl Conference YAPC 2016 in Orlando, Florida. About two-thirds of the talks were dedicated to Perl 5 topics, but to my amazement, more and more people in the community are seriously taking to the successor and problem child of many years standing, Perl 6.

No less than 11 years ago at the OSCON Conference 2005, Damian Conway seriously announced that the "endgame" of the development had started, but multiple setbacks and new beginnings ensued. Then, suddenly, at the end of 2015, the Rakudo virtual machine finally become operational, and although it does not exactly impress with its performance, it does at least reliably parse Perl 6 code.

What Is Perl 6?

To find out what you need Perl 6 for, it is helpful to analyze what Perl 5 lacked. Shortcoming number one was without a doubt a full-blown object system. In Perl 5, it was a makeshift affair, and no one in their right mind would have used the blessed hash structures, dubbed classes, without additional strictures in anything but toy-sized systems.

Instead, for the past 10 years, professional Perl developers have used the CPAN module Moose or one of its offshoots to define classes and access their attributes. This approach not only removes the need for developers to type tedious boilerplate code but also establishes a safety net, because the Perl engine immediately notices if a typing mistake has crept in. This is not the case if you have an incorrect hash key, resulting in undefined values being returned and random programmatic outcomes that are destined to creep up on you unawares. Any other modern language has solved this problem, but Perl 5 unfortunately only has a patchwork implementation for historic reasons.

Noisy Code

The makers, Larry Wall and Damian Conway, thus gave Perl 6 syntax for defining classes and invoking object methods that can take lists of named parameters. While they were at it, they adjusted some logical inconsistencies in the Perl 5 syntax that had proved to be pitfalls for beginners.

They didn't stop there, however; they kept going by borrowing syntax from various modern languages such as Python or Ruby, with the promise of practical use and clever programming tricks. The result is a language with a relatively large number of special characters that completely breaks compatibility with the old Perl 5 and needs a new interpreter named Rakudo.

Schrodinger's Cat

One of the smart and exotic new constructs in the new version involves superpositions, called "junctions" in Perl 6 and penned by Damian Conway. Conway peddled the idea 13 years ago at conferences, and later introduced it to the language in the form of quantum variables as a CPAN module by the name of Quantum::Superpositions.

Like the famous cat in Schrodinger's experiment [1] that is simultaneously dead and alive, these variables can simultaneously assume several values. For example, an integer can be 1 and 2 at the same time, and adding 5, gives you a result of 6 and 7. Back in the day, while trying to come up with a practical example for this, I remembered the casino game blackjack as an example, where an ace counts as either 1 or 11.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Python match

    Exploring the new Python match statement, Python's implementation of switch/case.

  • Perl: Automating Color Correction

    If you have grown tired of manually correcting color-casted images (as described in last month's Perl column), you might appreciate a script that automates this procedure.

  • Perl: Retouching Photos

    In many cases, whole series of digital images need the same kind of modifications, which forces the photo-grapher to repeat the same steps time and time again in GIMP. Have you ever considered retouching in Perl?

  • Calculating Probability

    To tackle mathematical problems with conditional probabilities, math buffs rely on Bayes' formula or discrete distributions, generated by short Perl scripts.

  • Perl: Call Waiting

    If you can’t get through to your home number, a script can check the line status and send a signal to you via the Web to tell you when the line is free.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More