๐Ÿ“˜ 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)

๐Ÿ“˜ Typed variables in Perl 6

This is how you declare a typed variable:

my Int $x;

Here, a scalar container $x may only hold an integer value. Attempts to assign it a value that is not an integer leads to an error:

my Int $x;
$x = "abc"; # Error: Type check failed in assignment to '$x';
ย ย ย ย ย ย ย ย ย ย ย  # expected 'Int' but got 'Str'

For typecasts, a respective method call is quite handy. Remember that while $x holds an integer, it is treated as a container object as a whole, which is why you may use some predefined methods on it. The same you can do directly on a string. For example:

my Int $x;
$x = "123".Int; # Now this is OK
say $x; # 123