Perl 6 Deep Dive

5 (4 reviews total)
By Andrew Shitov
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Free Chapter
    What is Perl 6?
About this book
Perl is a family of high-level, general-purpose, interpreted, dynamic programming languages consisting of Perl 5 and Perl 6. Perl 6 helps developers write concise and declarative code that is easy to maintain. This book is an end-to-end guide that will help non-Perl developers get to grips with the language and use it to solve real-world problems. Beginning with a brief introduction to Perl 6, the first module in the book will teach you how to write and execute basic programs. The second module delves into language constructs, where you will learn about the built-in data types, variables, operators, modules, subroutines, and so on available in Perl 6. Here the book also delves deeply into data manipulation (for example, strings and text files) and you will learn how to create safe and correct Perl 6 modules. You will learn to create software in Perl by following the Object Oriented Paradigm. The final module explains in detail the incredible concurrency support provided by Perl 6. Here you will also learn about regexes, functional programming, and reactive programming in Perl 6. By the end of the book, with the help of a number of examples that you can follow and immediately run, modify, and use in practice, you will be fully conversant with the benefits of Perl 6.
Publication date:
September 2017


What is Perl 6?

In this chapter, we will examine the reasons why Perl 6 appeared and follow the history of the development of the language. You will see some examples of changes that have happened since Perl 5, and you will learn how to download and use the compiler, where to find the documentation, and how to run your first program in Perl 6.

In this chapter, we will cover the following topics:

  • Origins of Perl 6
  • Differences from Perl 5
  • Perl 6 resources
  • Compilers
  • Working with Rakudo Star
  • Writing our Hello World program

Origins of Perl 6

Perl 6 is a programming language from the Perl family. Perl itself emerged in 1987 and since then, it is constantly evolving: its current stable version is 5.26, which was released in May 2017. In 2000, Larry Wall, the creator of Perl, proposed to start working on the next version of the language—Perl 6.

There were a few reasons for that. First, a language should continue developing to reflect the new requirements of developers. Second, it may change the perception of Perl in the non-Perl community. The version 5.0 appeared in 1993 and despite that, the language has continued developing. The major version number was still 5 and in the eyes of many people, it meant that Perl was stalled since 1993. The new major version update would refresh the perception.

The idea was to make Perl 6 "the community rewrite of Perl". Larry asked the community to share what bits of Perl they wanted to change. That call for changes resulted in 361 RFC (Request for Comments) documents, which are published at These documents are only of historical interest as of today.

Later, the various proposals were systematically analyzed, grouped together by similar topics and published as a series of Synopses. The naming and numbering principle behind those documents were to keep the structure of the chapters of the Programming Perl book.

Later, Synopses were once again summarized and explained in a set of documents called Apocalypses and Exegeses. All these papers are available today at, but again, they are not the final specification of the language, only a collection of historical documents.

Another important idea about Perl 6 was about the way compilers are created. In Perl 5, the language rules are indirectly defined by the single available compiler. Some bugs, or not obvious behavior of the compiler, may be considered as part of the language standard. In Perl 6, it was decided to have a clear language specification, and no reference compiler. There can be more than one compiler. The main requirement for them is implementing the specification and passing the set of tests.


Differences from Perl 5

Let's briefly look at some of the changes that happened on the way to Perl 6. In the following sections, you will see a few examples of code in Perl 5 and 6. They are intended for a general understanding of how Perl 5 transforms to Perl 6, but you are not expected to understand every bit of it. All the details about the syntax of Perl 6 will be explained later in this book.


One of the most difficult things for the newcomers to Perl are sigils. A sigil is a character in front of the name of variables in Perl that denotes the structural type of a variable. For example, $ for scalar values, @ for arrays, and % for hashes.

The problem arises when you access an element of an array or hash. Let's consider the following lines of code as an example in Perl 5 of an array with the first few Fibonacci numbers:

my @fibonacci = (0, 1, 1, 2, 3, 5, 8, 13);
print $fibonacci[4];

First, a @fibonacci array is created. The name of the variable includes the @ character as a sigil. In the second line, we access one of the elements of that array and use another sigil, $, this time. This is because a single element of an array is a scalar, and scalars use $ as a sigil. For those who learn Perl, this small change in sigils is a big problem in understanding the basics of the language.

In Perl 6, sigils are unified and are part of the variable names. You do not change it independently, whether you access an element of an array or an array as a whole. The preceding example will look like this in Perl 6:

my @fibonacci = (0, 1, 1, 2, 3, 5, 8, 13);
print @fibonacci[4];

In both lines, the same sigil is used for the @fibonacci array and for its @fibonacci[4] element. This approach is much more consistent and easier for a beginner.


In Perl 5, you had to extract the values of the arguments of a function yourself by using either the built-in shift function, or from the default @_ array.

Let's see this in the following example, with a function to calculate the sum of its two arguments. In Perl 5, you had to do some additional work to get the actual passed parameters.

First, get the argument values with shift in Perl 5:

sub add {
my $x = shift;
my $y = shift;
return $x + $y;

Then, by using the @_ array:

sub add {
my ($x, $y) = @_;
return $x + $y;

Unlike many other programming languages, it was not possible to declare a list of the function's formal parameters directly. For instance, this is how you do it in C or C++:

int add(int x, int y) {
    return x + y;

In Perl 5, it is possible to restrict the number of arguments and their structural types with the help of prototypes. Sigils are used there to tell Perl the type of the argument. The preceding function for addition may look like this in Perl 5:

sub add($$) {
    my ($x, $y) = @_;
    return $x + $y;

Using function prototypes will make the compiler complain when the function is used with the different number of arguments (say, one or three instead of two), but you still have to get their values yourself.

Perl 5.20 introduced function signatures. So, now, you may benefit from declaring the arguments in one go. The following code gives an example of such approach. Both $x and $y arguments are declared in the function header.

use v5.20;

use feature qw(signatures);
no warnings qw(experimental::signatures);

sub add($x, $y) {
    return $x + $y;

say add(4,5);

You will notice that you need to instruct Perl to use the features of Perl 5.20 by mentioning the minimal version number in the script. You will also notice that you must activate the corresponding feature by a separate instruction. However, even more, because signatures are an experimental feature, you have to manually disable the warning message to get a clean output.

In Perl 6, function signatures are allowed from the very beginning, so you may directly use it:

# This is Perl 6
sub add($x, $y) {
    return $x + $y;

Actually, signatures in Perl 5.20 are an example of backporting features from Perl 6 to Perl 5. So, despite the fact that Perl 6 was meant to be the next version of Perl 5, Perl 5 still gets some elements that were designed in Perl 6 to make Perl better.


To make the user experience better, let's take a look at another important example of where the syntax of Perl changes in Perl 6.

Traditionally, object-oriented programming is done in Perl 5 with the help of the so-called blessed hashes. Data members of an object are elements of the hash, and the blessed reference to that hash may be used to call a method on an instance of the class. The following example shows you what to do to define a class and create an instance of it in Perl 5:

package MyClass;

sub new {
    my ($class) = @_;
    my $this = {
        counter => 0
    bless $this, $class;
    return $this;

sub inc {
    my ($this) = @_;
    return $this->{counter};


So far, the class named MyClass has two methods—new, to create a new instance, and inc, to increment the counter and return the new value. When dealing with Perl 5's classes, don't forget to return a true value at the end of the module, and that is the goal of the 1 in the last line of the file.

In the main program, you can use MyClass by creating an instance and calling methods on the variable as follows:

use MyClass;

my $var = MyClass->new;

print $var->inc, "\n";
print $var->inc, "\n";
print $var->inc, "\n";

The implementation of the object-oriented things in Perl 5 was another obstacle for newcomers who may have had an experience of working with classes in other languages but were confused by the way Perl 5 created them.

Classes in Perl 6 are way more familiar to developers who have worked with other object-oriented programming languages.

This is how you define the same class, as shown in the preceding example, in Perl 6:

class MyClass {
    has $!counter;      

    method inc() {
        return $!counter;

As you see, the whole class is defined within the pair of braces. Its data members are explicitly declared with the has keyword, and there's no need to return 1 at the end of the file.

Now, create an object of the class and increment the internal counter three times, like we did in the Perl 5 example earlier. This is how you do it in Perl 6:

my $var =;

say $;
say $;
say $;

Do not focus on the details yet because it will all be explained in later chapters.

So far, we've seen three examples of where it was desired to improve the syntax of Perl 5.

To see more examples of changes between Perl 5 and Perl 6, you may refer to a few articles grouped under the title 'Perl 5 to Perl 6 guide' in the documentation of Perl 6 at, which is dedicated to that specific topic:


Perl 5 to Perl 6, in a nutshell—How do I do what I used to do?

5to6-perlfunc Perl 5 to Perl 6 guide—functions
5to6-perlop Perl 5 to Perl 6 guide—operators
5to6-perlsyn Perl 5 to Perl 6 guide—syntax
5to6-perlvar Perl 5 to Perl 6 guide—special variables

Compatibility with Perl 5

Existing Perl 6 compilers cannot execute Perl 5 programs without modifications in the source code. Perl 5 and Perl 6 are sometimes called sister languages. Both share the same spirit of Perl, and, in many cases, it is possible to convert the program from Perl 5 to Perl 6.

One of the biggest advantages of Perl 5 is the CPAN (Comprehensive Perl Archive Network). It contains a myriad of modules for the immense number of areas. Most probably, your task is already solved by some author of CPAN. To use this useful heritage in your programs in Perl 6, you may want to use the Inline::Perl5 module, which allows using an existing Perl 5 module without modifying the source code.

For example, let's take one of the most popular modules in Perl 5, Text::CSV, and embed it in our program in Perl 6.

use Inline::Perl5;
use Text::CSV:from<Perl5>;

my $csv =;

$csv.parse('First name,Last name');
say $csv.fields.join("\t");

say $csv.fields.join("\t");

With Inline::Perl5 enabled, the :from<Perl5> suffix loads the Text::CSV module from Perl 5 module directory. That module must be installed as a regular Perl 5 module from CPAN.

The rest of the program uses the $csv object, which is an instance of Text::CSV. Notice that you have to follow Perl 6 syntax, so, for instance, instead of creating the object with Text::CSV->new use The same applies to calling the parse method: in Perl 5 it would be $csv->parse(), while in Perl 6 you use dot: $csv.parse(). Working with objects in Perl 6 is described in Chapter 8, Object-Oriented Programming.

Luckily, there is already a module Text::CSV for Perl 6. You can find it on the page. Using Inline::Perl5 can be very useful for those modules on CPAN, which do not yet have equivalents or replacement, written in Perl 6. For example, the following example taken from the module documentation shows how to connect to the database (of course, you need PostgreSQL to be installed to test the example):

use Inline::Perl5;
use DBI:from<Perl5>;

my $dbh = DBI.connect('dbi:Pg:database=test');
my $products = $dbh.selectall_arrayref(
'select * from products', {Slice => {}}

The Inline::Perl5 module is available at


Perl 6 resources

Perl 6 has a long history and many documents were created during that time, for instance, language ideas, draft specifications, and compiler documentation. Many enthusiasts wrote articles and blog posts about Perl 6. Some of that is outdated and does not reflect the current state of the language. In this chapter, I will give you a brief list of materials that are up-to-date and that you should use in your practice of working with Perl 6.


The main entry point for documentation of the Perl 6 programming language is the Documentation section of Perl 6's site ( It contains a few sections with a comprehensive description of the types, operators, and built-in classes that are available in Perl 6. As the language is still developing, you may sometimes find places in the documentation where it does not reflect the current state of the language. In this case, you may consult the community of language developers or check the files from the test suite.

Test Suite

Perl 6's test suite, called Roast, can be found in the repository at It contains thousands of tests covering many corners of Perl 6. The test suite is also a good place to look if you want to see ways you can write programs in Perl 6. It may be a long read sometimes, but many tests are checking the features from all possible angles.

In Roast, the tests are grouped in directories with names such as S32-io. These names correspond to the numbers of the Synopses, and are split into thematic sections. For example, for the Synopses 11 'Compilation Units', there exist three directories in testsS11-compunits, S11-modules, and S11-repository. is a huge file describing the formal Perl 6 grammar. The grammar of Perl 6 is written in Perl 6 itself. The repository of contains the grammar and the viv utility that translates the grammar into the Perl 5 code. We have mentioned the grammar because it may be interesting for those readers who want to dig deeper into the internal structure of the grammar. In the rest of this book, we will explain the grammar based on examples of code in Perl 6.


The developers of Perl 6 traditionally use IRC for communication. You may also join the #perl6 channel to ask questions about the language or execute a piece of Perl 6 online.

To join the channel, follow the instructions listed on the page.

If you want to run the code in IRC, refer to the rakudo bot as follows:

<me> rakudo: say "Hello, World!"
<+camelia> rakudo-moar cb8fa0: OUTPUT: «Hello, World!␤»

In the output, you can see that Rakudo is using the MoarVM backend by default. The string that was printed by the program is displayed after the OUTPUT keyword.

Use this feature carefully as the result of your requests will be visible to the whole room and also be logged. The best use case is to show the behavior of the compiler when you find a bug or when you see a different result from what is said in the documentation. The Perl 6 developers are always present in the IRC channel and will advise on what is wrong or will aim to fix the bug and make the documentation correct.

If you are on Facebook, visit the Perl 6 group here:

In the offline, you will see many people who love Perl 6 at various conferences. Go to the Perl Conference (previously known as YAPC, (Yet Another Perl Conference)), which is held every year in Europe and in the USA. You may find more about them on and For many years, there were work Perl booths at the big open-source conferences like OSCON and FOSDEM. There are also many local conferences, workshops, and local group meetings. Find the closest group to your location at



During the process of Perl 6 development, a number of compilers were created. Some of them were just a playground to test some ideas, whereas some were more mature. Among the most important, we should mention the following four projects:

  • Parrot
  • Pugs
  • Perlito
  • Rakudo

There were more attempts to create a Perl 6 compiler, which were less successful or less complete. We will go through the preceding list to see the highlights of each project and then focus on Rakudo, which is the compiler you should use today.


Parrot is the first virtual machine aimed to be the base of the Perl 6 compiler. The initial design of the language suggested that the source code is compiled to the bytecode, which is executed by the virtual machine. Parrot's goal was to create a virtual machine suitable for handling all the needs of Perl 6 from simple data types, such as integers, to more complicated structures, such as classes, with the ability to call methods on objects and follow the object hierarchy.

The project is available at After some time, Parrot started supporting other programming languages, such as Lua or Python, and the virtual machine became less focused on Perl 6 itself. For example, the Ponie project was an attempt to create a compiler that would execute Perl 5 programs using Parrot.

Parrot became one of the virtual machines inside another project, Rakudo. However, before we go to Rakudo, let's follow the historical path and talk about Pugs.


Pugs (Perl User's Golfing System) is a Perl 6 compiler written in Haskell. It was started in 2005 by a sole developer and soon attracted more people to the team. Pugs was the most sophisticated compiler of its time. It was difficult and very time consuming to compile the project, the execution speed was low, but the quality of the compilation and the coverage of Perl 6 specification were outstanding.

Pugs main role in Perl 6 as of today is the vast test suite. It was created to test Pugs itself, but turned out to be an official test suite for Perl 6. A compiler that claims to call itself a Perl 6 compiler must pass the test suite tests.

Pugs are not developing anymore, but its source code is available on GitHub at


Perlito is another very interesting example of a project of building a Perl 6 compiler. It was aimed at cross-compiling Perl 5 or Perl 6 to one of these languages—JavaScript, Java, Go, Python, Ruby, or Lisp. You can find the project's repository at

Perlito offers a web interface to compile the subset of Perl 6 in the browser. It compiles the code in Perl 6 to JavaScript and executes it immediately. This page is available at This project covers the Perl 6 specification only partially, but it may still be used to create various online educational systems for both Perl 5 and 6.


Rakudo is a compiler initially built on the Parrot virtual machine. Later, it started using the Java Virtual Machine (JVM), but in the end, the developers of Parrot created their own virtual machine, MoarVM (Metamodel on a Runtime Virtual Machine ( Currently, the support of JVM is limited, and the main virtual machine is MoarVM.

Rakudo itself is a Perl 6 compiler. For us, the most useful compiler is Rakudo Star, which is a distribution including the compiler as well as a number of Perl 6 modules and a few command-line tools, such as a module installer. In this book, we will use the Rakudo Star compiler to run programs in Perl 6.

Rakudo's website is


Working with Rakudo Star

Rakudo is the most complete compiler available today. It supports the biggest subset of the Perl 6 language, and it would not be a mistake to say that Rakudo is the only compiler you should use to learn Perl 6.

Downloading and installing Rakudo Star

There are a few ways of installing Rakudo Star on your computer. You can either download the source code and compile it or download an installer for your platform. Rakudo Star is available for all major platforms, namely, Windows (both 32- and 64-bit versions), Mac OS X, and Linux.

The main download page of Rakudo Star is On that page, you will find links to the latest versions of the Rakudo Star distributions for different platforms and instructions on how to install them.

On Windows, the process is extremely simple. Just download the most recent version of the MSI installer, run it, and follow the instructions.

On Mac OS X, you either download a .dmg installer, or use the brew manager, as shown here:

$ brew install rakudo-star

On Linux, you have to install Rakudo Star from the source files.

After you have installed Rakudo Star, you will find the perl6 executable file in its bin directory. Make sure to add the path to that directory to your system-wide PATH variable so that you can type perl6 from any location.

In the rest of this book, we will assume that Rakudo Star is installed, and we will use the perl6 executable to run programs.

Command-line options

The Perl 6 compiler of Rakudo Star accepts a few command-line options. Let's take a look at some of them.

The -c command

The -c command-line checks the syntax of the program and exits. It also runs the BEGIN and CHECK blocks in the program, which are discussed in the section Phasers of Chapter 2, Writing Code later in this book. This command-line option is useful if you only want to check that there are no syntax errors in the code and don't want to execute it, with the exception being the code in the BEGIN and CHECK code blocks.

In the case of correct programming, it prints the following output:

Syntax OK

If there were compile-time errors, the compilation will stop at the first error and will display it on the console, mentioning the line number where it found an error.

The error message contains the description of the error and indicates the exact place in the code with the help of the eject character (). If your console supports colors, the fragment of the code before the eject character is green, and the rest of the line is red.

Here is an example of a program that misses the closing quote for the string:

say "Hello;

Run it to check the syntax, as shown here:

$ perl6 -c

The program did not compile, and this is what the compiler prints:

===SORRY!=== Error while compiling /Users/ash/code/
Unable to parse expression in double quotes; couldn't find final '"' 
at /Users/ash/code/
------> <BOL><EOL>
    expecting any of:
        argument list
        double quotes

The --doc command

The --doc (notice the double hyphen) command-line extracts the documentation from the program and prints it. Here, the so-called Pod documentation is meant. We will cover the Pod syntax in Chapter 2, Writing Code.

Let's see the small program that includes the documentation inside itself:

=begin pod
=head1 Hello, World program
=item This program prints "Hello, World!"
=end pod

say "Hello, World!";

Run it with the --doc command-line option as follows:

$ perl6 --doc

It will print only parts of the documentation. The code itself will not be executed:

 Hello, World program
   * This program prints "Hello, World!"

The -e command

The -e option allows you to pass the whole program in a command line. This is useful for short programs that do a few actions or, for example, for small tests when you check how things work in Perl 6.

Run it with the program enclosed in quotes:

$ perl6 -e'say "Hello"'

And this is the result you will see:


The -h and --help commands

The -h and --help commands print the text with a list of available command-line options.

The -n command

The -n command-line option creates a loop so that the program is executed once for every line of the text submitted to the input of the program.

It may be, for example, a one-line utility that prints the first letter of the strings from the STDIN input:

perl6 -n -e'print $_.substr(0, 1)' < file.txt

It will print the line composed from the first characters of the lines in file.txt.

The -p command

The -p command-line option acts like the previously described -n option, but it also prints the value of the default variable $_ at the end of each line. We will see the meaning of the default variable in the following chapters.

The -I  and -M commands

The -I and -M options are used to load modules into the program. The module's name is passed to the -M option and if necessary, the path to the module should be passed in the -I option.

The -v and --version command

The -v and --version options print the version of the current Perl 6 compiler as follows:

$ perl6 -v

At the time of writing, I am using Rakudo Star version 2017.01, and this is what the output looks like:

This is Rakudo version 2017.01 built on MoarVM version 2017.01 implementing Perl 6.c.

The important thing here, apart from the version itself, is the virtual machine that is used to execute Perl 6 (MoarVM, as shown earlier) and the version of the Perl 6 language specification (it is 6.c in this example).

The Rakudo Star versioning scheme uses the year and the month of the release date of the distributive. Rakudo is rapidly developing, so check the site regularly to get updates.

The --stagestats command

The --stagestats is a command-line option that is more Rakudo-specific than the others we have described earlier. It prints the time spent by the compiler at different stages of compiling and executing the program.

The output differs depending on whether you are running a program or checking its syntax with the -c command-line option. Let's first take a look at what is printed when the -c option is used:

$ perl6 --stagestats -c

The output is as follows:

Stage start      :   0.000
Stage parse      :   0.107
Stage syntaxcheck: Syntax OK

Without the -c option, you will see more statistics, because the program will not only be compiled but also executed, as shown here:

$ perl6 --stagestats

The regular output of the program is printed:

Stage start      :   0.000
Stage parse      :   0.327
Stage syntaxcheck:   0.000
Stage ast        :   0.000
Stage optimize   :   0.003
Stage mast       :   0.008
Stage mbc        :   0.000
Stage moar       :   0.000
Hello, World!

Writing our Hello World program

So far, as we have installed the Rakudo Star compiler, it's now time to create the first program in Perl 6. It will print Hello, World! and exit.

The program is really easy. All you need is a single line with the only instruction to call the built-in say function. It takes the string, prints it to the console, and adds a new line after it.

This is how the whole program looks:

say 'Hello, World!'

Save the code to the file, say,, and pass it to the compiler as follows:

$ perl6

It will compile the program and immediately execute it. The result is the desired string on the screen:

Hello, World!

Notice that the output ends with a new line. This is the behavior of the built-in say function. Alternatively, we could use another method of printing the output, using the print built-in function. Unlike say, it will not add the new line at the end of the output, so you have to do it yourself by adding the special symbol \n:

print "Hello, World!\n"

Notice that this time, a pair of double quotes is used. Double quotes treat special characters such as \n differently compared to single quotes. Inside double quotes, the \n converts to a new line character. That will not happen in single quotes, and, in that case, \n will appear on the screen as a sequence of two characters, and \n.

Because the program contains only one line of code, it is not necessary to end it with a semicolon. However, you can always do that:

say "Hello, World!";

This program produces exactly the same output as before.



In this chapter, we looked briefly at what kind of programming language Perl 6 is and how it differs from its sister language, Perl 5. We looked at the history of Perl 6 and at different projects of the Perl 6 compilers. Finally, we installed Rakudo Star, the best compiler tool of today and created the first 'Hello, World!' program.

In the following chapter, we will look at how to organize the code in the program.

About the Author
  • Andrew Shitov

    Andrew Shitov has been a Perl enthusiast since the end of the 1990s, and is the organizer of over 30 Perl conferences in eight countries. He worked as a developer and CTO in leading web-development companies, such as Art. Lebedev Studio, Booking dotCom, and eBay, and he learned from the "Fathers of the Russian Internet", Artemy Lebedev and Anton Nossik. Andrew has been following the Perl 6 development since its beginning in 2000. He ran a blog dedicated to the language, published a series of articles in the Pragmatic Perl magazine, and gives talks about Perl 6 at various Perl events. In 2017, he published the Perl 6 at a Glance book by DeepText, which was the first book on Perl 6 published after the first stable release of the language specification.

    Browse publications by this author
Latest Reviews (4 reviews total)
Excellent Perl 6 book: "Perl 6 Deep Dive" by Andrew Shitov.
A very good book. Thanks to the author.
Good book. Editor went a bit overboard when it came to editing: this is a book about a programming language, and when there's a command that is "say" and the writer uses that specific command in a line in a specific way, don't move the word to another place, because you will change the meaning of the command.
Perl 6 Deep Dive
Unlock this book and the full library FREE for 7 days
Start now