Contents

Motivation for a friendly Perl 6 tutorial

Perl 6 appears slowly before the wondering eyes of the curious, but if they grab Rakudo and the specification (aka Synopses) and try to write their first program, they often don't get too far. The language changes a bit every week and Rakudo doesn't support every command. And synopses aren't an easy read either. They are huge and contain a lot of nifty words. More written to clear edge cases to computer science students than to help the noobs to get a grasp. Therefore we need a tutorial that introduces the reader slowly into the world of Perl 6 and requires as little knowledge as possible. Of course some basic programming skills are needed and some Perl 5 wisdom is helpful, but everything else will be explained on the way. Every chapter of this tutorial is focused on one topic and contains many examples, so you can start to play with the language, which I encourage you to do. A start is made quickly, but it takes time to really learn Perl, and the new generation is optimized even on that.

Greetings Perl 6

Nearly every introduction starts with a "Hello World" example. I tried to come up with something more inventive, but its still the smallest, simplest program that does something joyfully useful. So shall we start with something worn out? No, something well-tried:

say "Greetings, earthlings, err Perl 6 neophytes.";

This first program outputs the expected text. The command say corresponds to the familiar print, but appends a line ending appropriate to the current operating system (e.g. a newline under Unix, a carriage return and newline under Windows). The print command is still available, but the new idiom is to use say.

I can already hear the objections of the more experienced Perl monger: A good script begins with strict and warnings. And right they are. In Perl 5 the program should read:

Perl 5:

use strict;
use warnings;

print "Greetings, earthlings, err Perl 6 neophytes.\n";

But in Perl 6 there is no need, because both pragmas are now enabled by default. This saves not only the two lines of code, which are in almost all of my programs, but it helps those who know nothing about the pragmas become aware of potential problems sooner. If necessary, one or both may be disabled with no strict or no warnings.

Parentheses and Curly Braces

To put more life into our program, lets use a variable, to which we assign a value and then reference:

my $a = 3;
say "I juggle with $a balls.";

We see that scalars still contain a single value (of any data type) and still use the sigil $. There's nothing more to explain. The variable is declared with my in the familiar lexical local scope, since use strict is in effect. And the double quote works basically as in Perl 5, interpolating control chars and variables. Perhaps playing with one or two balls shouldn't be called juggling:

The promt is a handy shortcut that replaces 3 commands. It does say, reads from STDIN (now $*IN) and chomps (cutting trailing \n) the result. In Perl 6 all reading from a data stream gets autochomped. The space after "balls? " is left by intention so that the input doesn't clutch on the question output.

And also in the second line we see a pleasant difference from Perl 5. The term following the 'if' may, or may not be placed in parentheses. Parentheses are used in Perl 6 only for grouping. If we have no nested structure, but only a single statement or a simple sequence of statements, they are not needed. This is true everywhere. In a similar way, an array, a variable that stores multiple values, can now be fed without parentheses.

Perl 6:

my @primes = 2,3,5,7;

This is possible because the comma is a list (creating) operator now. The second, not quite so obvious new feature in Perl 6, which is shown in the 'if' statement above, is the absence of a final semicolon. Since Perl 1.0, the semicolon separates individual commands. Only the last command within curly brackets (a block), does not have to end with a semicolon. Also you can see (above) you no longer have to remember when a semicolon is following a closing brace. (These rules were largely taken from C.) Larry has decided that each semicolon after braces is from now on optional. For 'if' blocks the same is true in Perl 5, but not as in 'eval' blocks.

Perl 5:

eval { print "Find all 3 differences !\n" };

Perl 6:

try { say "Find all 3 differences!" }

The third difference is the 'try' command, because 'eval' accepts only strings as parameter. Use now 'try' if you need a trial execution of blocks where errors can't stop your program, but where you can analyze the during runtime.

A not so free format

In terms of formatting, Perl has become somewhat more stringent. Leading spaces cannot be inserted anywhere at will. Of course, the width of indentation, and in general the number of spaces and line breaks between two parts of an expression are irrelevant.

Perl 6:

$b = $a ; # assign a value
$c = $a; # now I still have a copy of the value
$d
=
$a
; # And even a

But spaces and line breaks are already a divisive issue for operators, you must now pay more attention. Especially the new prefix Operator ('='), which takes the part of the popular perl5 diamond operator ('<>'), and can easily be mistaken for an assignment. The following two instructions can cause confusion: