Watch Perl 6

Watch Perl 6

As any competent CTO will tell you, businesses should generally stick with battle-tested technologies for their core computing needs, because betting the success of the company on unproven technologies is insane (unless that unproven technology is the company’s business). However, businesses still need to keep an eye on the future so they don’t get caught being too far behind the curve.

In the past few years we’ve seen Dart, Go, Rust, Swift, Hack, Julia, and many more new languages emerge, each trying to scratch a different itch. Some, like Swift, are exciting, but are in a walled garden, making them unsuitable for general-purpose use. Others, such as Dart, have simply failed to get traction for one reason or another. But let’s add one more language to that list: Perl 6.

Despite the name, Perl 6 is not the successor to Perl 5. They’re “sister” languages in the way that C# is a sister language to Java. Instead, Perl 6 is a powerful new language that’s been under development for over a decade. Although the development time has frustrated some, the Perl 6 developers were insistent on getting the language right, instead of getting it right now. It looks like it’s going to be worth the wait. Like A Song of Ice and Fire (Game of Thrones), which was started back in 1991 and is still being, ahem, “developed,” good things come to those who—well, you know.

So why is Perl 6 interesting? I’ll explain why the potential of Perl 6 is impressive, from both business and technical standpoints. In considering its features, I like to start with math.

Math that works

What is 0.1 plus 0.2? Would you say 0.3? Let’s write some Ruby code to check that:

$ ruby -e ‘puts 0.3 == 0.1 + 0.2 ? “Yes” : “No”‘

If you’re wondering how a popular language got such a simple math problem wrong, it’s because Ruby, like almost every popular programming language, uses something calledfloating-point numbers. These are approximations of the numbers you and I were taught in grade school. For the simple math problem above, Perl 5, Java, Python (both 2 and 3), C, C++, C#, Objective-C, PHP, and many other languages will give you the wrong answer. So developers create workarounds to floating-point math errors when writing software dealing with money or anything requiring mathematical accuracy. Computer scientists write papers on different approaches to solving floating-point math limitations. As of this writing, stackoverflow has over 47,000 questions dealing with floating-point issues. This is not a trivial problem, and it’s not just for new programmers.

Now here’s some Perl 6 code:

$ perl6 -e ‘say .3 == .1 + .2 ?? “Yes” !! “No”‘

You see, Perl 6 defaults to rational numbers whenever possible, though you can force floating-point math if you like. So in Perl 6, -7/3 is stored as -7 over 3. In floating-point math, you can only get an approximation of that number. Having math that actually works the way we were taught in school may well be the only reason you need to adopt Perl 6, but as it turns out, there are many more reasons.

Native gradual typing

Recently I wrote some code that analyzed some error logs, but the numbers weren’t adding up. Due to a bug I wrote, some of the errors were being counted incorrectly. Not a problem. I fixed the bug and moved on. But what if this code was a library I distributed to others? There would probably be quite a few people who were impacted by my inability to add. And what if the programming language itself had that bug? Or what about the worst-case scenario: having that bug in the hardware you’re running on?

The farther down you go in the stack, the more people will be impacted by bugs and the more expensive they are to fix. This is one of the motivations behind gradual typing. While dynamic languages such as Ruby, Perl 5, PHP, and others make it very easy to quickly build systems, getting that 3 a.m. phone call about a batch job that failed after five hours because of bad data being passed around is very frustrating. Sure, you shouldhave written code to ensure that you were passing an integer, but you didn’t. In reality, developers using dynamic languages often just assume (or hope) that they get the right kinds of data passed to their code. For a one-off script, that’s fine, but it often presents challenges as systems grow.

Gradual typing allows developers to optionally add type annotations to their code which, in turn, can either trivially trap type errors at runtime or throw compile-time type exceptions. Now, instead of trying to decide whether static or dynamic languages are appropriate for different parts of your system, you can choose one language that gives you the best of both worlds. Unfortunately, some gradual typing systems are third-party libraries, which cause more trouble than they’re worth, but Perl 6’s gradual typing is native, allowing the language to take full advantage of type information, including skipping the overhead of runtime type checks when the types can be checked at compile time.

Powerful object-oriented programming

I’ve programmed using a number of different languages, but it was in Java when I finally started to “get” object-oriented (OO) programming, and in Smalltalk when I realized just how powerful OO could be. Perl 5, by comparison, has functional, but primitive OO programming capabilities. However, when combined with the Moose library, it has the most advanced OO system I’ve ever programmed in—until Perl 6. Perl 6 boasts an expressive declarative syntax for classes along with a metaobject protocol, roles (Smalltalk traits), multi methods, and much more. So let’s look at a small example:

class Point {
    subset PointLimit of Real where -10.0 .. 10.0;
    has PointLimit $.x is rw is required; # rw means “read-write”
    has PointLimit $.y is rw is required;

That defines a mutable two-dimensional point class, with the x and y each constrained to the range -10 to 10. Once you understand Perl 6, that code is trivial to read. And it’s only four lines of actual code. Building similar functionality in other programming languages usually takes at least four times as many lines of code. One developer sent me equivalent code in Go, and it weighed in at 45 lines! Go has many virtues, but brevity is not among them.

Short, declarative, easy-to-read code that saves both time and money. Combine that with native gradual typing and math that works, and you’ve got a killer language. But wait, there’s more!

Strong concurrency support

Many of the languages being created today try to tackle the problem of concurrency head on. Our programming challenges are getting more complicated, but our CPUs are not getting much faster. Moore’s Law is coming to an end, and despite hardware companies experimenting with novel lithography techniques or chip stacking, the only reliable way of eking out more performance is parallel programming and concurrency (not the same thing, but for the purposes of this article, we’ll ignore the distinction). Unfortunately, there is not a single popular dynamic language with a working concurrency model (sorry, Erlang fans; your language is great, but popular it ain’t).

Perl 6 aims to change this. While it’s hard to guess everything that languages of the future will need, strong concurrency support is a must. However, concurrency might be the hardest challenge in IT. Just talk to developers writing threaded code and ask them how many of them can name the four Coffman Conditions. They’ve been around since 1971, and violating any of them prevents deadlocks in concurrent code, but they’re still not widely known. Furthermore, much of today’s parallel programming is done using forks and threads, and they’re so difficult to work with that the European Union recentlyshelled out €2.5 million on a research project called “Concurrency Made Easy.”

Perl 6 allows you to use forks and threads, but it discourages it. Instead, Perl 6 offers a rich variety of composable concurrency tools such as promises, supplies, and channels, which, while not making concurrency as easy as “Hello World,” nonetheless take much of the drudgery away from writing concurrent code. Under the hood, Perl 6 also has multiple ways of “sneaking in” concurrency when it detects that it’s safe to do so.

Concurrent programming is the future, and Perl 6 stands a good chance of being the first popular dynamic language to support it. You could throw away many of the other benefits of the language, and Perl 6’s powerful concurrency support might be enough to make it a winner.

Perl 6 is a language to watch

December 2015 saw the first “feature stable” release of Perl 6. It’s taken years to get there, but it was a major milestone. Now the work is heavily focused on performance. The developers spent a lot of time ensuring that Perl 6 works correctly and has a clean implementation. Now the optimization has begun, the last major impediment to wider adoption. Perl 6 has a dedicated VM named MoarVM, but it also runs on the JVM, which is a huge deal for those who are restricted to languages requiring the JVM. The module ecosystem is growing every day and invariably when I present on Perl 6 at conferences, developers—including those who know nothing about Perl—are always blown away by its power and want to learn more.

It’s hard to make a business case for Perl 6 right now, but as an upcoming language, it’s definitely one to watch.