πŸ“˜ The anyof and allof methods in Perl 6 promises

Another pair of factory methods, Promise.anyof and Promise.allof, creates new promises, which will be only kept when at least one of the promises (in the case of anyof) is kept or, in the case of allof, all of the promises listed at the moment of creation are kept. One of the useful examples found in … Continue reading “πŸ“˜ The anyof and allof methods in Perl 6 promises”

πŸ“˜ An example of using promises: Sleep sort in Perl 6

Finally, a funny example of how promises can be used for implementing the sleep sort algorithm. In sleep sort, every integer number, consumed from the input, creates a delay proportional to its value. As the sleep is over, the number is printed out. Promises are exactly the things that will execute the code and tell … Continue reading “πŸ“˜ An example of using promises: Sleep sort in Perl 6”

πŸ“˜ Chapter 6. Regexes and Grammars

Grammars in Perl 6 are the β€œnext level” of the well-known regular expressions. Grammars let you create much more sophisticated text parsers. A new domain-specific language (DSL), language translator, or interpreter can be created without any external help, using only the facilities that Perl 6 offers with grammars.

πŸ“˜ Regexes (regular expressions) in Perl 6

In fact, Perl 6 just calls regular expressions regexes. The basic syntax is a bit different from Perl 5, but most elements (such as quantifiers * or +) still look familiar. The regex keyword is used to build a regex. Let us create a regex for the short names of weekdays. my regex weekday Β Β Β  … Continue reading “πŸ“˜ Regexes (regular expressions) in Perl 6”

πŸ“˜ The match $/ object in Perl 6

As we have just seen, the smartmatch operator comparing a string with a regex returns an object of the Match type. This object is stored in the $/ variable. It also contains all the matching substrings. To keep (catch) the substring a pair of parentheses is used. The first match is indexed as 0, and … Continue reading “πŸ“˜ The match $/ object in Perl 6”

πŸ“˜ A simple parser in Perl 6 grammars

The first example of the grammar application is on grammar for tiny language that defines an assignment operation and contains the printing instruction. Here is an example of a programme in this language. x = 42; y = x; print x; print y; print 7; Let’s start writing the grammar for the language. First, we … Continue reading “πŸ“˜ A simple parser in Perl 6 grammars”

πŸ“˜ 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”

πŸ”¬72. Superscripts in Perl 6

In Perl 6, you can use superscript indices to calculate powers of numbers, for example: > 2⁡ 32 > 7Β³ 343 It also works with more than one digit in the superscript: > 10ΒΉΒ² 1000000000000 You can guess that the above cases are equivalent to the following: > 2**5 32 > 7**3 343 > 10**12 … Continue reading “πŸ”¬72. Superscripts 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”