๐Ÿ“˜ Prefix operator temp in Perl 6

temp creates a temporary variable and restores its value at the end of the scope (like it does the local built-in operator in Perl 5).

my $x = 'x';
{
ย ย ย  temp $x = 'y';
ย ย ย  say $x; # y
}
say $x;ย ย ย  ย # x

Compare it with the following operator, let.

๐Ÿ“˜ Built-in types in Perl 6

Perl 6 allows using typed variables. To tell the compiler that the variable is typed, you simply need to name the type while declaring the variable.

Some of the types available in Perl 6 are obvious and do not need comments:

Bool, Int, Str
Array, Hash, Complex

Some might require a small comment:

Num, Pair, Rat

The Num type is used to handle floating-point variables, and a Pair is a โ€œkey; valueโ€ pair. The Rat type introduces rational numbers with numerators and denominators.

๐Ÿ“˜ Variables in Perl 6: Twigils

In Perl 6, a variable name may be preceded by either a single-character sigil, such as $, @ or %, or with a double character sequence. In the latter case, this is called a twigil. The first character of it means the same thing that a bare sigil does, while the second one extends the description.

For example, the second character of the twigil can describe the scope of the variable. Consider *, which symbolises dynamic scope (more on this in Chapter 3). The following call prints the command line arguments one by one:

.say for @*ARGS;

Here, the @*ARGS array is a global array containing the arguments received from the command line (note that this is called ARGS and not ARGV as in Perl 5). The .say construction is a call of the say method on a loop variable. If you want to make it more verbose, you would write it like this:

for @*ARGS {
ย ย ย  $_.say;
}

Letโ€™s list a few other useful predefined dynamic variables with the star in their twigils. The first element of the twigil denotes the type of a container (thus a scalar, an array, or a hash):

$*PERL contains the Perl version (Perl 6)

$*PID โ€” process identifier

$*PROGRAM-NAME โ€” the name of the file with the currently executing programme (for a one-liner its value is set to -e)

$*EXECUTABLE โ€” the path to the interpreter

$*VM โ€” the name of the virtual machine, which your Perl 6 has been compiled with

$*DISTRO โ€” the name and the version of the operation system distribution

$*KERNEL โ€” similar, but for the kernel

$*CWD โ€” the current working directory

$*TZ โ€” the current timezone

%*ENV โ€” the environment variables

In my case, the variables above took the following values:

Perl 6 (6.c)
90177
twigil-vars.pl
"/usr/bin/perl6".IO
moar (2016.11)
macosx (10.10.5)
darwin (14.5.0)
"/Users/ash/Books/Perl 6/code".IO
{Apple_PubSub_Socket_Render => /private/tmp/com.appleโ€ฆ., DISPLAY => /private/tmp/com.appleโ€ฆ, HISTCONTROL => ignorespace, HOME => /Users/ash, LC_CTYPE => UTF-8, LOGNAME => ash ...

The next group of the predefined variables include those with the ? character as their twigil. These are โ€œconstantsโ€ or so-called compile-time constants, which contain information about the current position of the programme flow.

$?FILE โ€” the name of the file with a programme (no path included; contains the string -e for one-liners)

$?LINE โ€” the line number (is set to 1 for one-liners)

$?PACKAGE โ€” the name of the current module; on a top level, this is (GLOBAL)

$?TABSTOP โ€” the number of spaces in tabs (might be used in heredocs)

๐Ÿ“˜ Variables in Perl 6: Sigils

Perl 6 uses sigils to mark variables. The sigils are partially compatible with the Perl 5 syntax. For instance, scalars, lists and hashes use, respectively, the $, @, and % sigils.

my $scalar = 42;
say $scalar;

Itโ€™s not a surprise that the code prints 42.

Consider the following fragment, which also gives a predictable result (the square brackets indicate an array):

my @array = (10, 20, 30);
say @array; # [10 20 30]

Now, let’s use the advantages of Perl 6 and rewrite the above code, using less typing, both fewer characters and less punctuation:

my @list1 = <10 20 30>;

Or even like this:

my @list2 = 10, 20, 30;

Similarly, we can omit parenthesis when initializing a hash, leaving the bare content:

my %hash =
    'Language' => 'Perl',
    'Version'  => '6';
say %hash;

This small programme prints this (the order of the hash keys in the output may be different, and you should not rely on it):

{Language => Perl, Version => 6}

To access the elements of a list or a hash, Perl 6 uses brackets of different types. It is important to remember that the sigil always remains the same. In the following examples, we extract a scalar out of a list and a hash:

my @squares = 0, 1, 4, 9, 16, 25;
say @squares[3]; # This prints the 4th element, thus 9

my %capitals =
    'France'  => 'Paris',
    'Germany' => 'Berlin';
say %capitals{'Germany'};

An alternative syntax exists for both creating a hash and for accessing its elements. To understand how it works, examine the next piece of code:

my %month-abbrs =
    :jan('January'),
    :feb('February'),
    :mar('March');
say %month-abbrs<mar>; # prints March

Naming a variable is a rather interesting thing as Perl 6 allows not only ASCII letters, numbers, and the underscore character but also lots of the UTF-8 elements, including the hyphen and apostrophe:

my $hello-world = "Hello, World";
say $hello-world;

my $don't = "Isnโ€™t it a Hello?";
say $don't;

my $ะฟั€ะธะฒะตั‚ = "A Cyrillic Hi!";
say $ะฟั€ะธะฒะตั‚;

Would you prefer non-Latin characters in the names of the variables? Although it may slow down the speed of your typing, because it will require switching the keyboard layout, using non-Latin characters in names of variables does not have any performance impact. But if you do, always think of those developers, who may need to read your code in the future.

๐Ÿ“˜ Bool in Perl 6

The usage of the Bool variables is straightforward although there are some details about which you might want to know. The Bool type is a built-in enumeration and provides two values: True and False (or, in a full form, Bool::True and Bool::False). It is permissible to increment or decrement the Boolean variables:

my $b = Bool::True;
$b--;
say $b; # prints False

$b = Bool::False;
$b++;
say $b; # True

The Perl 6 objects (namely, all variables) contain the Bool method, which converts the value of the variable to one of the two Boolean values:

say 42.Bool; # True

my $pi = 3.14;
say $pi.Bool; # True

say 0.Bool;ย ย  ย # False
say "00".Bool; # True

Similarly, you may call the Int method on a variable and get the integer representation of the Boolean values (or values of any other types):

say Bool::True.Int; # 1

๐Ÿ“˜ Str type in Perl 6

Str is no doubt a string. In Perl 6, there are methods to manipulate strings. Again, you call them as methods on objects.

my $str = "My string";

say $str.lc; # my string
say $str.uc; # MY STRING

say $str.index('t'); # 4

Let us now get the length of a string. The naรฏve attempt to write $str.length produces an error message. However, a hint is also provided:

No such method 'length' for invocant of type 'Str'
Did you mean 'elems', 'chars', 'graphs' or 'codes'?

Thus, we have a simple and a mono-semantic method to get the length of a Unicode string.

say "ฯ€ฮตฯฮป 6".chars; # 6

Getting used to the new way of working with strings as objects may take some time. For example, this how you can call the printf as a method on a string:

"Today is %02i %s %i\n".printf($day, $month, $year);

๐Ÿ“˜ Hash in Perl 6

Hashes provide a few methods with clear semantics, for instance:

my %hash = Language => 'Perl', Version => 6;

say %hash.elems;ย  # number of pairs in the hash
say %hash.keys;ย ย  # the list of the keys
say %hash.values; # the list of the values

Hereโ€™s the output:

ย 2
(Version Language)
(6 Perl)

It is possible to iterate not only over the hash keys or values but also over whole pairs:

for %hash.pairs {
ย ย ย  say $_.key;
ย ย ย  say $_.value;
}

The .kv method returns a list containing the alternating keys and values of the hash:

say %hash.kv # (Version 6 Language Perl)