🦋 108. Basic usage of NativeCall in Perl 6

NativeCall is both a module and a technology in Perl 6 that allows you to call C functions from your Perl 6 code. Today, let’s meet the most basic usage. Take the rand() function from the C standard library: #include <stdio.h> #include <stdlib.h> int main() { int r = rand(); printf(“%i\n”, r); return 0; } … Continue reading “🦋 108. Basic usage of NativeCall in Perl 6”

🦋 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 … Continue reading “🦋 69. Setting timeouts in Perl 6”

🦋 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 … Continue reading “🦋 66. Atomic operations in Perl 6”

🦋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 … Continue reading “🦋64. What does gist do in Perl 6?”

🦋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 … Continue reading “🦋63. More on the proto keyword in Perl 6”

🦋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. 1 Let us start with evaluating … Continue reading “🦋62. The EVAL routine in Perl 6, part 1”

🦋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 … Continue reading “🦋17. Parameterised roles in Perl 6”

🦋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. Stages 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: … Continue reading “🦋11. Compiler stages and targets in Perl 6”

🦋1. The proto keyword in Perl 6

Today, we are looking precisely at the proto keyword. It gives a hint for the compiler about your intention to create multi-subs. Example 1 Consider an example of the function that either flips a string or negates an integer. multi sub f(Int $x) {     return -$x; } multi sub f(Str $x) {   … Continue reading “🦋1. The proto keyword in Perl 6”