1. What Is Perl 6?
Perl 6 is a programming language. It is designed to be easily learned, read, and written by humans, and is inspired by natural language. It allows the beginner to write in baby Perl, while giving the experienced programmer freedom of expression, from concise to poetic.
Perl 6 is gradually typed . It mostly follows the paradigm of dynamically typed languages in that it accepts programs whose type safety it cant guarantee during compilation. However, unlike many dynamic languages, it accepts and enforces type constraints. Where possible, the compiler uses type annotations to make decisions at compile time that would otherwise only be possible at runtime.
Many programming paradigms have influenced Perl 6. You can write imperative, object-oriented, and functional programs in Perl 6. Declarative programming is supported through features like multiple-dispatch, sub-typing, and the regex and grammar engine.
Most lookups in Perl 6 are lexical, and the language avoids global state. This makes parallel and concurrent execution of programs easier, as does Perl 6s focus on high-level concurrency primitives. When you dont want to be limited to one CPU core, instead of thinking in terms of threads and locks, you tend to think about promises and message queues.
Perl 6 as a language is not opinionated about whether Perl 6 programs should be compiled or interpreted. Rakudo Perl 6 the main implementationprecompiles modules on the fly and interprets scripts.
1.1 Perl 5, the Older Sister
Around the year 2000, Perl 5 development faced major strain from the conflicting desires to evolve and to keep backward compatibility.
Perl 6 was the valve to release this tension. All the extension proposals that required a break in backward compatibility were channeled into Perl 6, leaving it in a dreamlike state where everything was possible and nothing was fixed. It took several years of hard work to get into a more solid state.
During this time, Perl 5 also evolved, and the two languages are different enough that most Perl 5 developers dont consider Perl 6 a natural upgrade path anymore, to the point that Perl 6 does not try to obsolete Perl 5 (at least not more than it tries to obsolete any other programming language :-), and the first stable release of Perl 6 in 2015 does not indicate any lapse in support for Perl 5.
Perl 5 is developed by a separate community of enthusiasts, who keep an eye on Perl 6 to find features worth adopting into Perl 5. So while the Perl 5 and Perl 6 communities have a certain overlap and communicate with each other, both thrive mostly independently.
1.2 Library Availability
Being a relatively young language, Perl 6 lacks the mature module ecosystem that languages such as Perl 5 and Python provide.
To bridge this gap, interfaces exist that allow you to call into libraries written in C, Python, Perl 5, and Ruby. The Perl 5 and Python interfaces are sophisticated enough that you can write a Perl 6 class that subclasses a class written in either language, and the other way around.
So if you like a particular Python library, for example, you can simply load it into your Perl 6 program through the Inline::Python module.
1.3 Why Should I Use Perl 6?
If you like the quick prototyping experience from dynamically typed programming languages, but you also want enough safety features to build big, reliable applications, Perl 6 is a good fit for you. Its gradual typing allows you to write code without having a full picture of the types involved, and later introduce type constraints to guard against future misuse of your internal and external APIs.
Perl has a long history of making text processing via regular expressions ( regexes ) very easy, but more complicated regexes have acquired a reputation of being hard to read and maintain. Perl 6 solves this by putting regexes on the same level as code, allowing you to name them like subroutines, and even to use object-oriented features such as class inheritance and role composition to manage code and regex reuse. The resulting grammars are very powerful and easy to read. In fact, the Rakudo Perl 6 compiler parses Perl 6 source code with a Perl 6 grammar!
Speaking of text, Perl 6 has amazing Unicode support. If you ask your user for a number, and they enter it with digits that dont happen to be the Arabic digits from the ASCII range, Perl 6 still has you covered. And if you deal with graphemes that cannot be expressed as a single Unicode code point, Perl 6 still presents it as a single character.
There are more technical benefits that I could list, but more importantly, the language is designed to be fun to use. An important aspect of that is good error messages. Have you ever been annoyed at Python for typically giving just SyntaxError: invalid syntax when somethings wrong? This error could come from forgetting a closing parenthesis, for example. In this case, a Perl 6 compiler says
Unable to parse expression in argument list; couldn't find final ')'
which actually tells you whats wrong. But this is just the tip of the iceberg. The compiler catches common mistakes and points out possible solutions, and even suggests fixes for spelling mistakes. The Perl 6 community considers error messages that are less than awesome , short LTA, to be worthy of bug reports, and much effort is spent into raising the bar for error messages.
Finally, Perl 6 gives you the freedom to express your problem domain and solution in different ways and with different programming paradigms. And if the options provided by the core language are not enough, it is designed with extensibility in mind, allowing you to introduce both new semantics for object-oriented code and new syntax.
1.4 Summary
Perl 6 is a flexible programming language that offers many cool and convenient features to both beginners and experts. It offers flexibility, type checking, and powerful Unicode and text processing support.
2. Running Rakudo Perl 6
Before we start exploring Perl 6, you should have an environment where you can run Perl 6 code. So you need to install Rakudo Perl 6, currently the only actively developed Perl 6 compiler. Or even better, install Rakudo Star, which is a distribution that includes Rakudo itself, a few useful modules, and a tool that can help you install more modules.
Installing Rakudo itself gives you just the compiler. It follows a monthly release cycle, so it allows you to keep up to date with the latest developments.
When you choose to install Rakudo Star , which is typically released every three months, you get a more stable base for development, and some tools like a debugger and a module installer. You can use the module installer to make use of prepackaged software libraries that are included neither in Rakudo itself nor in Rakudo Star.
The following sections discuss a few options for installing Rakudo Star. Choose whatever works for you.
The examples in this book use Rakudo 2017.04.03 or Rakudo Star 2017.04 (which is built on top of Rakudo 2017.04.03) and should work with this or any newer version of Rakudo, as long as it supports Perl 6 version 6.c.
Note
The examples and source code used in this book can be accessed via the Download Source Code button at https://www.apress.com/9781484228982 .