📘 Creating a calculator with Perl 6 grammars

When considering language parsers, implementing a calculator is like writing a “Hello, World!” programme. In this section, we will create a grammar for the calculator that can handle the four arithmetical operations and parentheses. The hidden advantage of the calculator example is that you have to teach it to follow the operations priority and nested … Continue reading “📘 Creating a calculator with Perl 6 grammars”

📘 AST (abstract syntax tree) and attributes in Perl 6 grammars

Now, we are ready to simplify the grammar again after we split the assignment and printout rules into two alternatives each. The difficulty was that without the split, it was not possible to understand which branch had been triggered. You either needed to read the value from the value token or get the name of … Continue reading “📘 AST (abstract syntax tree) and attributes in Perl 6 grammars”

📘 Actions in Perl 6 grammars

The grammars in Perl 6 allow actions in response to the rule or token matching. Actions are code blocks that are executed when the corresponding rule or token is found in the parsed text. Actions receive an object $/, where you can see the details of the match. For example, the value of $<identifier> will … Continue reading “📘 Actions in Perl 6 grammars”

🔬 75. my $x = $x in Perl 6

What happens if you’ll try to create a new variable and immediately initialise it by itself, as shown in the following test code: my $x = $x; This does not work (which is expected), but Perl 6 is so kind to the user  that it gives an error message prepared especially for this case: ===SORRY!=== … Continue reading “🔬 75. my $x = $x in Perl 6”

🔬73. Keys, values, etc. of hashes in Perl 6

Today, we will take a look at a few methods of the Hash class that return all hash keys or values or both: > my %h = H => ‘Hydrogen’, He => ‘Helium’, Li => ‘Lithium’; {H => Hydrogen, He => Helium, Li => Lithium} > %h.keys; (H Li He) > %h.values; (Hydrogen Lithium Helium) … Continue reading “🔬73. Keys, values, etc. of hashes in Perl 6”

🔬71. Implementing Int.sleep() in Perl 6

Hello! Yesterday, I was giving my Perl 6 Intro course at the German Perl Workshop in Gummersbash. It was a great pleasure to prepare and run this one-day course, and, while it was difficult to cover everything, we touched all main aspects of the Perl 6 language: from variables to regexes and parallel computing. Of … Continue reading “🔬71. Implementing Int.sleep() in Perl 6”

🔬70. Examining the enum type in Perl 6

In Perl 6, you can create enumerations like this: enum colour <red orange yellow green blue violet>; Having this said, you can use the new name as a type name and create variables of that type: my colour $c; $c = green; say $c; # green say $c.Int; # 3 As you would rightly expect, … Continue reading “🔬70. Examining the enum type 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”

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