🦋 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”

🔬68. The smartness of the sequence operator in Perl 6, part 1

In Perl 6, you can ask the sequence operator to build a desired sequence for you. It can be arithmetic or geometric progression. All you need is to show the beginning of the sequence to Perl, for example: .say for 3, 5 … 11; This prints numbers 3, 5, 7, 9, and 11. Or: .say … Continue reading “🔬68. The smartness of the sequence operator in Perl 6, part 1”

🔬67. Redeclaration of a symbol in Perl 6

Today, we will see how Perl 6 helps to keep our programs better. Redeclaration of a variable Examine the following program: my $x = 1; my $x = 2; say $x; You can immediately see that this program is not entirely correct. Either we meant to assign a new value to $x or to create … Continue reading “🔬67. Redeclaration of a symbol 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”

🔬65. The EVAL routine in Perl 6, part 2

Welcome back! As you might notice, there was a small gap in the daily post flow. Before we are back to the Rakudo internals, a couple of words about some changes here. First of all, every post is now marked with either 🦋 or 🔬 (or with indistinguishable rectangles □ if your browser cannot display an emoji :-). These … Continue reading “🔬65. The EVAL routine in Perl 6, part 2”

🦋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”

🔬61. Declared in BOOTSTRAP

First of all, a new release of the Rakudo Perl 6 compiler was announced today: 2018.02. There are many fixes and speed improvements there, including one proposed by me. Let me not go through the changes, as most of them require quite in-depth knowledge of the Rakudo internals. Instead, let us take a low-hanging fruit and … Continue reading “🔬61. Declared in BOOTSTRAP”

🔬60. Examining the Real role of Perl 6, part 3

As promised yesterday, let us take a look at the two methods of the Real role: polymod and base. polymod I already devoted a post to the Int.polymod method, but the method also exists in the Real role. Let us see if it is different. method polymod(Real:D: +@mods) { my $more = self; my $lazy = … Continue reading “🔬60. Examining the Real role of Perl 6, part 3”

🔬59. Examining the Real role of Perl 6, part 2

Today, we continue our initial exploration of the Real role, that was started a couple of days ago. Together with its methods, the role contains a number of subroutines (placed outside the role) that define the infix operators with the objects of the Real type. The list is not that long, so let me copy … Continue reading “🔬59. Examining the Real role of Perl 6, part 2”

🔬58. A word on polymod in Perl 6

Before moving to the second part of the Real role, let us stop on the polymod method of the Int class. The method takes a number and a list of arbitrary numbers (units) and returns the corresponding multipliers. So that you can easily say that 550 seconds, for example, is 9 minutes and 10 seconds: … Continue reading “🔬58. A word on polymod in Perl 6”

🔬57. Examining the Real role of Perl 6, part 1

During the last few days, we talked a lot about the Real role. Lets us then look at it more precisely. The code is located in the src/core/Real.pm file. It contains the role itself and a few subroutines implementing different infixes. The Real role in its turn implements the Numeric role: my role Real does Numeric { … Continue reading “🔬57. Examining the Real role of Perl 6, part 1”

🔬56. A bit more on Rat vs FatRat in Perl 6

Yesterday, we were digging into Rakudo Perl 6 to understand when a Rat value becomes a Num value. It turned out that if the value becomes too small, which means its denominator gets bigger and bigger, Rakudo starts using a Num value instead of Rat. We found the place where it happened. Today, let us … Continue reading “🔬56. A bit more on Rat vs FatRat in Perl 6”

🔬55. FatRat vs Rat in Perl 6

Yesterday, Solomon Foster posted an example in the Perl 6 group on Facebook: my @x = FatRat.new(1, 1), -> $x { $x – ($x ** 2 – $N) / (2 * $x) } … * This code implements Newton’s method of finding an approximate value of a square root of $N. The important thing is … Continue reading “🔬55. FatRat vs Rat in Perl 6”

Perl 6 Grammars, Part 1

Originally published on perl.com The Perl 6 language has builtin support of grammars. You may consider grammars as a combination of the well known regular expressions and utilities such as yacc or bison, or more sophisticated grammar tools such as ANTLR. All that—both lexer, parser, and also semantic processing—which are often separate parts of compilers, is built-in and available … Continue reading “Perl 6 Grammars, Part 1”

🔬54. Going over the Bridge, part 2. Let’s get rid of it

Today, we continue working with the Bridge method in Rakudo Perl 6. Yesterday, we saw the definitions of the methods in a few pre-defined data types. It is time to see how the method is used. What’s inside? The major use of the method is inside the Real role, which contains the following set of methods: … Continue reading “🔬54. Going over the Bridge, part 2. Let’s get rid of it”

🔬53. Going over the Bridge, part 1

In the classes that handle numbers in Perl 6, we saw the Bridge method, which is used polymorphically. Let us spend some time and try to understand 1) how it works and 2) is it necessary. Classes and Roles Our first step is to look where the method is defined. Here is the list of … Continue reading “🔬53. Going over the Bridge, part 1”

🔬52. An attempt to understand how [*] works in Perl 6

Reduction operators are one of the many attractive features of Perl 6. A classical example is calculating factorial: say [*] 1..5; # 120 It is remarkable that in the AST output (generated with the –target=ast command-line option) you do not see any cycles. There is the METAOP_REDUCE_LEFT call, and obviously, the rest is hidden on the deeper … Continue reading “🔬52. An attempt to understand how [*] works in Perl 6”

🔬51. Colonpair in Perl 6’s Grammar, part 2

Today, we continue examining the colonpair syntax in Perl 6 and will give an addition to the third branch of the token. Here’s the branch we are looking at today: # branch 3 | <identifier> { $*key := $<identifier>.Str; } [ || <.unsp>? :dba(‘pair value’) <coloncircumfix($*key)> { $*value := $<coloncircumfix>; } || { $*value := 1; … Continue reading “🔬51. Colonpair in Perl 6’s Grammar, part 2”