Category: Chapter 7. Appendix

In Perl 6, the star character * can be associated with one of the predefined classes, Whatever and WhateverCode.

We’ll start with an object of the Whatever class.

say *.WHAT; # (Whatever)

The construction like 1 .. * creates a Range object, where its upper limit is not fixed to any particular number.

say (1 .. *).WHAT; # (Range)

Here is an example with a loop that prints the numbers from 5 to 10 line by line:

for (5 .. *) {
.say;
last if $_ == 10;
}

Now, try array indices and ask to take all the elements starting from the fourth one:

my @a = <2 4 6 8 10 12>;
say @a[3 .. *]; # (8 10 12)

The “three dots” operator in combination with a star creates a sequence.

say (1 ... *).WHAT; # (Seq)

You can use it when you need a lazy and potentially infinite list. A lazy list is a list whose elements are evaluated only when they are necessary for the execution of the programme.

In the following example, an array does not know its size, but you can read infinitely from it; the lazy list will supply new elements:

my @a = (100 ... *);
for (0 .. 5) {
say "Element $_ is @a[$_]";
}

This programme will print five lines corresponding to the first five elements of the @a array, which contain values from 100 to 105, including 105. If you change the range in the for loop from 0 .. 5 to 0 .. *, you will get a programme that prints infinitely.

It is possible to modify the algorithm for generating the new values of the sequence by giving a hint to the compiler:

In Perl 6, the transformation from a code with a star to an anonymous code block is called whatever-currying. In the traditional style of programming, you introduce a variable to get the same result. In Perl 6, a compiler creates that for you. The following two examples are equivalent to the two above.

Whatever-currying is also happening, for example, when we want to refer to the last elements of an array using negative indices. In the following example, we pick array elements from the fourth to the second-to-last one.

my @a = <2 4 6 8 10 12>;
say @a[3 .. *-2]; # (8 10)

In Perl 5, you could get the last element of an array with the -1 index. In Perl 6, the access @a[-1] will generate an error:

Unsupported use of a negative -1 subscript to index from the end; in Perl 6 please use a function such as *-1

So, you need to add a star:

say @a[*-1]; # 12

Here, the compiler will convert @a[*-1] into the following code:

@a[@a.elems - 1]

Another common use case of WhateverCode is to provide a compiler with a rule for generating infinite sequences.

This example creates a lazy list containing the Fibonacci numbers. The * + * construction will be implicitly replaced with something like {$^a + $^b}. Note that the first two stars in the example are part of what will become an anonymous code block, while the last one is a single Whatever object.

The simplest way to build a web server in Perl 6 is to use a PSGI server called Bailador. This is a module that you can find on the official page with the list of Perl 6 modules: modules.perl6.org. If you are using the Rakudo Star distribution, use the panda* command line utility to install the module.

$ panda install Bailador

Bailador copies the interface of the well-known framework Dancer for Perl 5. The name is the same but in Spanish.

Here is the minimal programme that implements the web server.

use Bailador;
get '/' => sub {
'Hello, world!'
}
baile;

The programme describes the action, which the server does in response to the request to its home page. The baile (dance in Spanish) method starts the main loop of the PSGI server.

Run the programme:

$ perl6 web1.pl

You will get the output informing you that the server is ready to accept requests.

Entering the development dance floor: http://0.0.0.0:3000
[2016-12-27T20:27:34Z] Started HTTP server.

Open that page in a browser, and you will see the desired output: “Hello, world!”

The next step is to parse the URL and respond accordingly. Bailador allows extract parameters from the URL with the colon syntax:

get '/:name' => sub ($name) {
"Hello, $name!"
}

Please note that you cannot omit the space after the sub keyword. There is an alternative. As the sub is anonymous, you may use the pointy block instead:

get '/:name' => -> $name {
"Hello, $name!"
}

Add it to the programme, restart the server, and go to, for example, http://0.0.0.0:3000/abc. You should get the “Hello, abc” output in the browser.

Bailador is happy to accept regexes instead of the fixed URLs. For example, let’s create the URL /square-of/N, where the N can be any non-negative integer.

get / 'square-of/' (<digit>+) / => sub ($n) {
$n * $n
}

The regex pattern / ‘square-of/’ (<digit>+) / contains the capturing part, and so the variable $n will be set to the number from the URL. As Bailador reads the address patterns in the order they appear in the file, make sure to put the method above the handler of /name. Now, test how it works at http://0.0.0.0:3000/square-of/5; it should print 25.

It is possible to access some environment variables in the URL handler. Use the request method and take the request.env hash from it, as is demonstrated in the example:

You can access the data from a template. It receives the argument containing everything that you just passed.

% my ($params) = @_;
Hi, <%= $params<name> %>!

At the moment of writing this book, panda was about to become outdated, and the new recommended tool will be zef. Please refer to the documentation of your Perl 6 distribution on how to install modules.

The strings in Perl 6 are internally handled in the format called NFG (Normalization Form Grapheme). From a practical point of view, that means that, for any symbol, you can get its NFC, NFD, NFKC and KFKD forms. I will refer you to read about the details of these formats to the Unicode standard. In simple words, these are different canonical and decomposed forms of a symbol.

There are four methods with those names, and you may call them on character strings:

say $s.NFC; # codepoint
say $s.NFD;
say $s.NFKC;
say $s.NFKD;

The full canonical name of a character is returned by the method uniname:

say 'λ'.uniname; # GREEK SMALL LETTER LAMDA

In the string class, the encode method is defined; it helps to see how the string is built internally in one of the Unicode charsets:

Install the DBIish module to get a powerful tool for working with databases*:

$ panda install DBIish

You also will need the database driver; for example, libmysqlclient for working with MySQL. Check the documentation of the DBIish module on modules.perl6.org if you want to work with a different database engine.

The module provides an interface similar to the DBI’s in Perl 5. You obtain a database handler, $dbh, and they work via the statement handler, $sth. Let us see some details in the following example.

With the insert statements, placeholders may be used to avoid the need of escaping the values before injecting them into the SQL query. In the following example, a new row will be written to the database table. The actual values are passed to the execute method.