Get to know Perl 6

Perl 6 finally arrived a few weeks ago, carrying a load of new features without losing its free-form charm. Here's what both Perl diehards and newbies need to know

pearl oyster shell treasure jewel gem

Back in the mid-1990s, I ran an ISP and built much of the code needed to operate the service in Perl. The early versions of Larry Wall’s language were powerful tools that evolved to take advantage of new programming techniques, while staying close to Wall’s concept of a programming language that mapped to the ways we think and communicate.

For many years Wall has been working on the latest version, Perl 6, which was finally released on Christmas Day 2015. Where earlier Perl releases were interpreted languages, Perl 6 is compiled, using the Rakudo compiler to run your code in a JVM. It’s a flexible response to the way platforms have evolved since the initial release of the language, using REPL (read-eval-print-loop) to try out code snippets before you use them in an application.

Like its predecessors, Perl 6 is a free-form language. You can write code any way you like; all that’s needed is a semicolon to end a statement.

Unwrap Perl 6

Perl 6 offer three distinct types of variable: scalars, arrays, and hashes. Each is defined by a prefix to a name, and once they're defined, you can quickly apply appropriate operators to the variable.

For example, you can change the case of a string, count the number of characters, or even reverse their direction in no time at all. The ability to work quickly with string content was always one of Perl’s great strengths (in some circles it was known as “the universal string munging tool”), and it’s good to see that approach continues in the latest version. Similar operators simplify working with integers and rational numbers.

While many modern languages are designed for static types, Perl 6 is focused on dynamic typing -- with the option of static types where necessary. If you’re using static types, Perl offers the option of an interchangeable type that can be either a string or a number. An introspection operand, .WHAT, allows you to see what type is currently associated with a variable, so you can control program flow appropriately.

Another key element of Perl processing has been "lists" -- arrays of multiple values that can be used to contain any variables. By default they’re indefinitely large, and they increase in size as you add new elements. Again, a series of operands can be used with arrays to quickly process their contents, letting you interpolate contents in strings and push and pop elements from an array.

Fixed size and multidimensional arrays add flexibility, and arrays become a key storage element in a Perl 6 program. Similarly, hashes are a set of key/value pairs. You can extract keys and values, as well as quickly looking up a single value.

The charms of Perl

People often find it difficult to pick up Perl quickly. There’s no one right way to write Perl; it’s a language that fits the programmer rather than the other way around. Eventually, however, you’ll look at your code and wonder why all languages don’t work like this.

Take, for example, how Perl 6 handles loops. The familiar if-else loop is there, but there’s also the option of an unless loop, best thought of as a negated if (there’s no else option in an unless loop!).

One of Perl 6’s key features is support for functional programming. There’s no need to learn anything new to start working with Perl’s functional programming features; they fall naturally from Perl's familiar subroutine tooling.

Any Perl 6 function is a first-class citizen. It can be passed as an argument, it can be returned, and it can be assigned to a variable. You can start by passing a function to another higher-order function -- for example, a function that applies a transform to a single variable. Passing that as an argument to the map function, it is used to apply that function to every member of an array. You don’t even need to use a named function; you can create an anonymous function and include it in the call directly.

Once you’ve started calling methods from methods, you can take advantage of Perl 6’s chaining option. Instead of passing a result from method to method, all you need to do is apply the methods in order as operands to the initial variable or array -- for example, you’d call my @result = @array.function1.function2.function3. Alternatively you can use a feed operator ==> to show how each function chains to the next.

Perl 6 also includes tools for handling parallelism, using the race expression to work on all the elements in an array at the same time. Race is a tool for handling data parallelism, where the same task is applied across an array. Task parallelism is handled by promises, in much the same way as JavaScript, using start and await statements to trigger parallel tasks.

This Perl shines

I like Perl 6 a lot. If you’ve used Perl in the past, you’ll find it thoroughly familiar -- you can pick it up, start coding, and add new features and techniques as you need.

Once you’re using them, though, there’s no going back. They bring Perl to the forefront of modern development, giving you an easy-to-operate functional language that works well with string content. Perl 6 is an ideal tool for handling text processing as part of a Web front end, for example, or for parsing information to be delivered to an Internet of things stream processing service.

That flexibility is important. It’s not often you find a language suitable for many different purposes, particularly for delivering a minimum viable product. You might not end up using Perl 6 to deliver an entire application, but it’s an ideal prototyping tool and an excellent placeholder for languages that may be more efficient -- but certainly aren’t as adaptable.

The original Perl was the Unix Swiss Army chainsaw. It may have been overkill, but it did the job. The same is true of Perl 6. However, it's more suited for modern development and for many more platforms.

Copyright © 2016 IDG Communications, Inc.

How to choose a low-code development platform