šŸ¦‹ 69. Setting timeouts in Perl 6

In Perl 5, I used to set timeouts using signals (or, at least, that was an easy and predictable way). In Perl 6, you can use promises. Let us see how to do that.

To imitate a long-running task, create an infinite loop that prints its state now and then. Here it is:

for 1 .. * {
    .say if $_ %% 100_000;

As soon as the loop gets control, it will never quit. Our task is to stop the program in a couple of seconds, so the timer should be set before the loop:


for 1 .. * {
    .say if $_ %% 100_000;

Here, theĀ Promise.inĀ method creates a promise that is automatically kept after the given number of seconds. On top of that promise, usingĀ then, we add another promise, whose code will be run after the timeout. The only statement in the body here isĀ exitĀ that stops the main program.

Run the program to see how it works:

$ time perl6 timeout.pl
. . .

real 0m2.196s
user 0m2.120s
sys 0m0.068s

The program counts up to about four millions on my computer and quits in two seconds. That is exactly the behaviour we needed.

For comparison, here is the program in Perl 5:

use v5.10;

alarm 2;
$SIG{ALRM} = sub {

for (my $c = 1; ; $c++) {
    say $c unless $c % 1_000_000;

(It manages to count up to 40 million, but thatā€™s another story.)

šŸ¦‹ 66. Atomic operations in Perl 6

N. B. The examples below require a fresh Rakudo compiler, at least of the version 2017.09.

Discussing parallel computing earlier or later leads to solving race conditions. Let us look at a simple counter that is incremented by two parallel threads:

my $c = 0;

await do for 1..10 {
    start {
        $c++ for 1 .. 1_000_000

say $c;

If you run the program a few times, you will immediately see that the results are very different:

$ perl6 atomic-1.pl 
$ perl6 atomic-1.pl 
$ perl6 atomic-1.pl 
$ perl6 atomic-1.pl 

Of course, the idea was to increase the counter by 1 million in all of the ten threads, but aboutĀ ā…“ of the steps were lost. It is quite easy to understand why that happens: the parallel threads read the variable and write to it ignoring the presence of other threads and not thinking that the value can be changed in-between. Thus, some of the threads work with an outdated value of the counter.

Perl 6 offers a solution: atomic operations. The syntax of the language is equipped with the Atom Symbol (U+0x269B)Ā āš› character (no idea of why it is displayed in that purple colour). Instead ofĀ $c++, you should typeĀ $cāš›++.

my atomicint $c = 0;

await do for 1..10 {
    start {
        $cāš›++ for 1 .. 1_000_000

say $c;

And before thinking of the necessity to use a Unicode character, let us look at the result of the updated program:

$ perl6 atomic-2.pl 

This is exactly the result we wanted!

Notice also, that the variable is declared as a variable of theĀ atomicintĀ type. That is a synonym forĀ int, which is a native integer (unlikeĀ Int, which is a data type represented by a Perl 6 class).

It is not possible to ask a regular value to be atomic. That attempt will be rejected by the compiler:

$ perl6 -e'my $c; $cāš›++'
Expected a modifiable native int argument for '$target'
  in block  at -e line 1

A few other operators can be atomic, for example, prefix and postfix increments and decrementsĀ ++Ā andĀ --, orĀ +=Ā andĀ -=. There are also atomic versions of the assignment operatorĀ =Ā and the one for reading:Ā āš›Ā (sic!).

If you need atomic operations in your code, you are not forced to use theĀ āš›Ā character. There exists a bunch of alternative functions that you can use instead of the operators:

my atomicint $c = 1;

my $x = āš›$c;  $x = atomic-fetch($c);
$c āš›= $x;     atomic-assign($c, $x);
$cāš›++;        atomic-fetch-inc($c);
$cāš›--;        atomic-fetch-dec($c);
++āš›$c;        atomic-inc-fetch($c);
--āš›$c;        atomic-dec-fetch($c);
$c āš›+= $x;    atomic-fetch-add($c,$x);

say $x; # 1
say $c; # 3

šŸ¦‹64. What does gist do in Perl 6?

When you print an object, say, asĀ say $x, Perl 6 calls theĀ gistĀ method. This method is defined for all built-in types: for some of them, it calls theĀ StrĀ method, for some theĀ perlĀ method, for some types it makes the string representation somehow differently.

Let us see how you can use the method to create your own variant:

class X {
    has $.value;

    method gist {
        '[' ~ $!value ~ ']'

my $x = X.new(value => 42);

say $x; # [42]
$x.say; # [42]

When you callĀ say, the program prints a number in square brackets:Ā [42].

Please notice that the interpolation inside double-quoted strings is using Str, notĀ gist. You can see it here:

say $x.Str; # X<140586830040512>
say "$x";   # X<140586830040512>

If you need a custom interpolation, redefine theĀ StrĀ method:

class X {
    has $.value;

    method gist {
        '[' ~ $!value ~ ']'
    method Str {
        '"' ~ $!value ~ '"'

my $x = X.new(value => 42);

say $x;     # [42]
$x.say;     # [42]

say $x.Str; # "42"
say "$x";   # "42"

Now, we got the desired behaviour.

šŸ¦‹63. More on the proto keyword in Perl 6

Before digging into the details of theĀ EVAL routine, we have to reveal some more information about protos and multiple dispatch. Examine the following program:

proto sub f($x) {
    say "proto f($x)";

multi sub f($x) {
    say "f($x)"

multi sub f(Int $x) {
    say "f(Int $x)"

multi sub f(Str $x) {
    say "f(Str $x)"


Here, there are three multi-candidates of the function plus a function declared with theĀ proto keyword. Earlier, we only saw such proto-functions with empty body, such as:

proto sub f($x) {*}

But this is not a necessity. The function can carry a regular load, as we see in the example:

proto sub f($x) {
    say "proto f($x)";

Run the program:

proto f(2)
proto f(2)
proto f(3)
proto f(3)

All the calls were caught by the proto-candidate. Now, update it and return the {*} block for some dedicated values;

proto sub f($x) {
    if $x.Str eq '3' {
        return {*}
    say "proto f($x)";

The if check triggers its block for the last two function calls:


In these cases, the proto-function returnsĀ {*}, which makes Perl 6 trying other candidates. As we have enough candidates for both integer and string arguments, the compiler can easily choose one of them:

proto f(2)
proto f(2)
f(Int 3)
f(Str 3)

šŸ¦‹62. The EVAL routine in Perl 6, part 1

The EVAL routine in Perl 6 compiles and executes the code that it gets as an argument.Ā  Today, we will see some potential use cases that you may try in your practice. Tomorrow, we will dig into Rakudo sources to see how it works and why it breaks sometimes.


Let us start with evaluating a simple program:

EVAL('say 123');

This program printsĀ 123, thereā€™s no surprise here.


There are, though, more complicated cases. What, do you think, does the following program print?

EVAL('say {456}');

I guess it prints not what you expected:

-> ;; $_? is raw { #`(Block|140570649867712) ... }

It parses the content between the curly braces as a pointy block.


What if you try double quotes?

EVAL("say {789}");

Now it even refuses to compile:

===SORRY!=== Error while compiling eval.pl
EVAL is a very dangerous function!!! (use the MONKEY-SEE-NO-EVAL pragma to override this error,
but only if you're VERY sure your data contains no injection attacks)
at eval.pl:6
------> EVAL("say {789}")ā;


We can fix the code by adding a few magic words:


EVAL("say {789}");

This time, it printsĀ 789.


The code is executed (we donā€™t know yet when exactly, that is the topic of tomorrowā€™s post), so you can make some calculations, for example:


EVAL("say {7 / 8 + 9}"); # 9.875


Finally, if you try passing a code block directly, you also cannot achieve the goal, even with a blind monkey:


EVAL {say 123};

The error happens at runtime:

Constraint type check failed in binding to parameter '$code';
expected anonymous constraint to be met but got 
-> ;; $_? is raw { #`...
  in block <unit> at eval.pl line 10

This message looks cryptic, but at least we see once again that we got an anonymous pointy block passed to the function.


And before we wrap up for today, an attempt to use Perl 5 syntax:

eval('say 42');

There is no such function in Perl 6, and we get a standard error message:

===SORRY!=== Error while compiling eval2.pl
Undeclared routine:
  eval used at line 5. Did you mean 'EVAL', 'val'?

It looks OK but it can be better.

Stay tuned, tomorrow we will try to understand how all these examples work in Rakudo.

šŸ¦‹17. Parameterised roles in Perl 6

Today, a small excursus into the syntax. Did you know that roles in Perl 6 can have a parameter that makes them similar to generic templates in, say, C++? Hereā€™s a small example:

role R {
    has $.value;
    method add($b) {
        $.value + $b.value

    method div($b) {
        $.value / $b.value

The R role defines an interface that has a value and two methods for arithmetical operations: add and div.

Now, create a class using the role, initialise two variables and use the methods to get the results:

class C does R {}

my C $x = C.new(value => 10);
my C $y = C.new(value => 3);

say $x.add($y); # 13
say $x.div($y); # 3.333333

Although the values here were integers, Perl did a good job and returned a rational number for the division. You can easily see it by calling theĀ WHAT method:

say $x.add($y).WHAT; # (Int)
say $x.div($y).WHAT; # (Rat)

If you have two integers, the result of their division is always of the Rat type. The actual operator, which is triggered in this case, is the one from src/core/Rat.pm:

multi sub infix:</>(Int \a, Int \b) {
Ā  Ā  DIVIDE_NUMBERS a, b, a, b

The DIVIDE_NUMBERS sub returns a Rat value.

Defining a role

How to modify theĀ C class so that it performs integer division? One of the options is to use a parameterised role:

role R[::T] {
    has T $.value;
    method add($b) {
        T.new($.value + $b.value)

    method div($b) {   
        T.new($.value / $b.value)

TheĀ parameter in square brackets after the role name restricts both the type of theĀ $.value attribute and the return type of the methods, which return a new object of the typeĀ T. Here, in the template of the role,Ā T is just a name, which should later be specified when the role is used.

Using the role

So, letā€™s make it integer:

class N does R[Int] {}

Now the parts of the role that employ theĀ T name replace it with Int, so the class is equivalent to the following definition:

class C {
    has Int $.value;
    method add($b) {
        Int.new($.value + $b.value)

    method div($b) {   
        Int.new($.value / $b.value)

The new class operates with integers, and the result of the division is an exact 3:

class N does R[Int] {}

my N $i = N.new(value => 10);
my N $j = N.new(value => 3);

say $i.add($j); # 13
say $i.div($j); # 3

It is also possible to force floating-point values by instructing the role accordingly:

class F does R[Num] {}

my F $x = F.new(value => 10e0);
my F $y = F.new(value => 3e0);

say $x.add($y); # 13
say $x.div($y); # 3.33333333333333

Notice that both values, including 13, are of the Num type now, not Int or Rat as it was before:

say $x.add($y).WHAT; # (Num)
say $x.div($y).WHAT; # (Num)

šŸ¦‹11. Compiler stages and targets in Perl 6

Welcome to the new year! Today, let us switch for a while from the discussion about obsolete messages to something different.


If you followed the exercises in the previous posts, you might have noticed that some statistics was printed in the consoleĀ when compiling Rakudo:

Stage start      :   0.000
Stage parse      :  44.914
Stage syntaxcheck:   0.000
Stage ast        :   0.000
Stage optimize   :   4.245
Stage mast       :   9.476
Stage mbc        :   0.200

You could have also noticed that the bigger the file you changed, the slower it is compiled, up to dozens of seconds when you modify Grammar.pm.

It is also possible to see the statistics for your own programs. The --stagestats command-line option does the job:

$ ./perl6 --stagestats -e'say 42'
Stage start      :   0.000
Stage parse      :   0.065
Stage syntaxcheck:   0.000
Stage ast        :   0.000
Stage optimize   :   0.001
Stage mast       :   0.003
Stage mbc        :   0.000
Stage moar       :   0.000

So, letā€™s look at these stages. Roughly, half of them is about Perl 6, and half is about MoarVM. In the case Rakudo is configured to work with the JVM backend, the output will differ in the second half.

The Perl 6 part is clearly visible in the src/main.nqp file:

# Create and configure compiler object.
my $comp := Perl6::Compiler.new();
$comp.addstage('syntaxcheck', :before);
$comp.addstage('optimize', :after);
nqp::bindhllsym('perl6', '$COMPILER_CONFIG', $comp.config);

Look at the selected lines. If you have played with Perl 6 Grammars, you know that big grammars are usually split into two parts: the grammar itself and the actions. The Perl 6 compiler does exactly the same thing for the Perl 6 grammar. There are two files: src/Perl6/Grammar.nqp and src/Perl6/Actions.nqp.

When looking at src/main.nqp, it is not quite clear that there are eight stages. Add the following line to the file:

for ($comp.stages()) { nqp::say($_) }

Now, recompile Rakudo and run any program:

$ ./perl6 -e'say 42'

Here they are.

The names of the first three stagesā€”start, parse, and syntaxcheckā€”are quite self-explanatory. The ast stage is the stage of building an abstract syntax tree, which is then optimized in the optimize stage.

At this point, your Perl 6 program has been transformed into the abstract syntax tree and is about to be passed to the backend, MoarVM virtual machine in our case. The stages names start with m. The mastĀ stage is the stage of the MoarVM assembly (not abstract) syntax tree, mbc stands for MoarVM bytecode and moar is when the VM executes the code.


Now that we know the stages of the Perl 6 program workflow, letā€™s make use of them. The --target option lets the compiler to stop at the given stage and display the result of it. This option supports the following values: parse, syntaxcheck, ast, optimize, and mast. With those options, Rakudo prints the output as a tree, and you can see how the program changes at different stages.

Even for small programs, the output, especially with the abstract syntax tree or an assembly tree of the VM is quite verbose. Letā€™s look at the parse tree of the ā€˜Hello, World!ā€™ program, for example:

$ ./perl6 --target=parse -e'say "Hello, World!"'
- statementlist: say "Hello, World!"
  - statement: 1 matches
    - EXPR: say "Hello, World!"
      - args:  "Hello, World!"
        - arglist: "Hello, World!"
          - EXPR: "Hello, World!"
            - value: "Hello, World!"
              - quote: "Hello, World!"
                - nibble: Hello, World!
      - longname: say
        - name: say
          - identifier: say
          - morename:  isa NQPArray
        - colonpair:  isa NQPArray

All the names here correspond to rules, tokens, or methods of the Grammar. You can find them in src/Perl6/Grammar.nqp. As an exercise, try predicting if the name is a method, or a rule, or a token. Say, a value should be a token, as it is supposed to be a compact string, while a statementlist is a rule.