📘 Reading directory content using Perl 6

Print the file names from the current directory.

Reading a directory in Perl 6 can be done using the dir routine defined in the IO::Path class.

say dir();

This tiny program does not do the task really satisfactory, as the dir routine returns a lazy sequence (an object of the Seq data type) of IO::Path objects.

To get the textual file names, take the path part of an IO::Path object using the path method:

.path.say for dir;

The code is equivalent to the more verbose fragment:

for dir() -> $file {
    say $file.path;

If you want to print full paths of the files in a directory, use the absolute method:

.absolute.say for dir;

The test named argument of the dir routine allows selecting filenames that match a certain regex, for example, listing all jpeg files:

for dir(test => /\.jpg$/) -> $file {
    say $file.path;

📘 The uniq utility written in Perl 6

Create the simple equivalent of the UNIX uniqutility, which only prints the lines from the STDIN input, which are not repeating the previous line.

The solution of this task can be built on the solution of Task 95, The catutility. This time, the entered lines have to be saved, so let’s introduce the $previous variable and make an explicit code block for the loop.

my $previous = '';
while (my $line = $*IN.get) {
    say $line unless $line eq $previous;
    $previous = $line;

On each iteration, the next line from the $*IN handle is read and saved in the $line variable. If the value is different from the previous line, which is saved in the $previous variable, then the current line has been printed.

At the moment, only duplicated lines are affected. If the two identical lines are separated by other lines, then everything is printed. Let us modify the program so that it only prints the unique lines per whole input stream.

my %seen;
while (my $line = $*IN.get) {
    next if %seen{$line};
    say $line;
    %seen{$line} = 1;

Here, the %seen hash is used as a storage of the lines printed. It’s also a good practice to use a Set object instead; see the example of using a set in Task 54, Exclusion of two arrays.

📘 The cat utility written in Perl 6

Create the equivalent of the UNIX catutility that copies its STDIN input directly to STDOUT.

Reading from the input and sending it to the output is a relatively easy task. The $*IN handle is by default connected to STDIN. Being an object of the IO::Handle type, it has the slurp method that returns the whole input text in one go. What is left to do, is just to print it to the output, which defaults to STDOUT.

Here’s the complete program:

say $*IN.slurp;

This works well, and we can use it via the command line:

$ perl6 cat.pl < file1.txt > file2.txt

However, in the interactive mode, the program does not replicate each entered line, but instead, it waits until the end of the output (say, until you press Ctrl+D). This behaviour is fully explainable because of the use of the slurpmethod.

Let us modify the program so that it prints each line as soon as it is entered. The IO::Handle class has another method, get, which reads one line from the handle and returns it. Create a loop and print the line after it is delivered by the get method:

.say while $_ = $*IN.get;

Here, the default variable $_ is used. This allows to omit creating the new variable and to make the whole program more compact. The .say call in it is a shortcut for $_.say.

📘 Parallel file processing in Perl 6

Process the files from the current directory in a few parallel threads.

We have to do something with each file in the directory, and it has to be done in such a way that files are processed independently with a few workers. It is not possible to predict how long the process will take for each individual file, that’s why we need a common queue, which supplies the filenames for the next available worker.

A good candidate for the queue is a channel.

my $channel = Channel.new();
$channel.send($_) for dir();

All the file names are sent to the channel, which we close afterward. (On how to read directories, see more details in Task 97, Reading directory contents.)

Channels are designed to work thread-safe. It means that it is possible to get data from the channel using several threads, and each value is processed only once. Perl 6 cannot predict which thread gets which name but it can guarantee that each data item is only read by the threads once.

my @workers;
for 1..4 {
    push @workers, start {
        while (my $file = $channel.poll) {

The code on the previous page creates four independent workers using the startkeyword. As they are executed independently not only from each other but also from the main program, it is important to wait until all of them are done:


The elements of the @workers array are promises (objects of the Promise data type). The await routine waits until all the promises are kept.

Another practical way of creating and waiting workers is shown in Task 92, Sleep Sort: instead of collecting them in an array, you can use the gather and take keywords.

Examine the main loop:

while (my $file = $channel.poll) {

On each iteration, a value from the channel is read. The poll method ensures that the reading stops after the channel is exhausted.

All four threads are doing similar work and are polling the same channel. This approach distributes the filenames that were sent to the channel between the workers. As a name has been read, it is removed from the channel, and the next read request returns the next name.

Finally, cook the do_something sub according to your needs. In the following simplest example, it only prints filenames:

sub do_something($file) {
    say $file.path;

📘 Atomic operations in Perl 6

Using the solution for Task 38, the Monte Carlo method, create a program that calculates the result using multiple threads.

Perl 6 has built-in support for parallel computing. In Task 92, Sleep Sort, we’ve seen how to use the keywords awaitgather, and take to spawn a few threads and wait for them to finish. 

When different threads want to modify the same variable, such as a counter, it is wise to introduce atomic operations to make sure the threads do not interfere with each other. Here is the modification of the Monte Carlo program calculating the area of a circle with four parallel threads.

my atomicint $inside = 0;

my $n = 5000;
my $p = 4;

await gather for 1..$p {
    take start {
        for 1..$n {
            my @point = map {2.rand - 1}, 1..2;
            $inside⚛++ if sqrt([+] map *², @point) <= 1;

say 4 * $inside / $p / $n;

Run the program a few times, changing the value of $n (the number of random points per thread) and $p (the number of threads). The program should print the value that is close to pi, such as 3.141524.

The new thing here is the atomic increment operation:


An atomic operation ensures that the variable is modified with no conflicts between the threads.

The variable itself should be a native integer of a special type—notice how it is declared:

my atomicint $inside;

As the atomic operation uses the Unicode character, there is an ASCII alternative:


Here’s a list of other atomic operations and their synonyms that can be used with parallel processes:

$var ⚛= $value       atomic-assign($var, $value)
my $a = ⚛$var         my $a = atomic-fetch($var)

$var⚛++               atomic-fetch-inc($var)
$var⚛--               atomic-fetch-dec($var)
++⚛$var               atomic-inc-fetch($var)
--⚛$var               atomic-dec-fetch($var)

$var ⚛+= $value       atomic-fetch-add($var, $value)
$var ⚛-= $value       atomic-fetch-dec($var, $value)

N. B. The code in this task works with the Rakudo Perl 6 compiler starting from version 2017.09. Earlier versions do not support atomic operators.

📘 Sleep Sort in Perl 6

Implement the Sleep Sort algorithm for a few small positive integer values.

The Sleep Sortis a funny implementation of the sorting algorithm. For each input value, an asynchronous thread starts, which waits for the number of seconds equals to the input number and then prints it. So, if all the threads are spawned simultaneously, the output of the program contains the sorted list.

Here is the solution in Perl 6. On the next page, we will go through the bits of it and explain all the important moments.

await gather for @*ARGS -> $value {
    take start {
        sleep $value/10;
        say $value;

Pass the values via the command line, and get them sorted.

$ perl6 sleep-sort.pl 9 10 2 8 5 7 6 4 1 3

The input values from the command line come to the @*ARGS array. The first step is to iterate over the array:

for @*ARGS -> $value {    
     . . .

For each $value, the startblock creates a promise with a code block that waits for the time that is proportional to the value and prints the value after that time.

start {
    sleep $value/10;
    say $value;

Dividing the value by ten speeds up the program. On the other hand, the delay should not be too small to avoid race conditions between different threads.

After a separate promise has been created for each input number, the program has to wait until all of them are kept. To achieve that, the gathertake construction is used. The take keyword adds another promise to a sequence, which is then returned as a whole by the gather keyword.

gather for @*ARGS -> $value {
    take start {
        . . .

Finally, the await routine ensures the program does not quit until all the promises are kept or, in other words, until all the numbers are printed.

await gather . . . {
    take start {
        . . .

📘 Setting timeouts in Perl 6

Do not wait for a slow code block if it takes too long.

In Perl 6, promises are the best way to create timeouts. In the following example, two code blocks are created; they are executed in parallel.

my $timeout = Promise.in(2).then({
    say 'Timeout after 2 seconds';

my $code = start {
    sleep 5;

    say 'Done after 5 seconds';

Both $timeout and $code are the promises, i. e. objects of the Promise type. Actually, the $timeout variable is a promise that is executed as a result of keeping the anonymous promise created by the Promise.in(2) call. The in method of the Promise class creates a promise that becomes kept after the given number of seconds.

The second promise, stored in the $code variable, is created by the start function. This is a long-running code block, which does not return within five seconds. The $code promise can be kept only after that time.

The $timeout promise is kept earlier than the $code one. To let the program continue earlier, create another promise with the help of the anyof method:

await Promise.anyof($timeout, $code);

say 'Alldone';

The flow of the whole program is the following: first, the $timeout code block is created and starts running in a separate thread. Then, without waiting, the $code block has been created and launched. Finally, the next line of the main thread is executed; it creates an anonymous thread and waits until it is kept. The await routine blocks the execution of the main program until at least one of its arguments is kept. This program prints ‘Alldone’ after two seconds, and exits:

$ perl6 timeout.pl 
Timeout after 2 seconds
All done

If the $code thread is completed first (say, if we changed the timeout value to 10 seconds), then the output is different, and the timeout is not triggered:

$ perl6 timeout.pl 
Done after 5 seconds
All done

Keep in mind that in our examples, the program finishes after printing ‘All done’. In case the program continues after that, the longest promise will still be running.

For example, add a simple delay like the one shown below:

await Promise.anyof($timeout, $code);
say 'All done';
sleep 20;

In this case, the program prints all three messages:

$ perl6 timeout.pl 
Timeout after 2 seconds
All done
Done after 5 seconds