Book Image

Perl 6 Deep Dive

By : Andrew Shitov
Book Image

Perl 6 Deep Dive

By: Andrew Shitov

Overview of 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.
Table of Contents (15 chapters)

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 http://perl6.org/downloads. 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 err.pl

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

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

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 pod.pl

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:

Hello

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 rakudo.org 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 hello.pl

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 hello.pl

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!