πŸ“˜ The in and at methods in Perl 6 promises

The other two factory methods, Promise.in and Promise.at, create a promise, which will be kept after a given number of seconds or by a given time. For example: my $p = Promise.in(3); for 1..5 { Β Β Β  say $p.status; Β Β Β  sleep 1; } The programme prints the following lines. Planned Planned Planned Kept Kept That means … Continue reading “πŸ“˜ The in and at methods in Perl 6 promises”

πŸ“˜ Whatever (*) and WhateverCode in Perl 6

In Perl 6, the star character * can be associated with one of the predefined classes, Whatever and WhateverCode. We’ll start with an object of the Whatever class. say *.WHAT; # (Whatever) The construction like 1 .. * creates a Range object, where its upper limit is not fixed to any particular number. say (1 … Continue reading “πŸ“˜ Whatever (*) and WhateverCode in Perl 6”

πŸ“˜ Programming for the Internet in Perl 6

The simplest way to build a web server in Perl 6 is to use a PSGI server called Bailador. This is a module that you can find on the official page with the list of Perl 6 modules: modules.perl6.org. If you are using the Rakudo Star distribution, use the panda* command line utility to install … Continue reading “πŸ“˜ Programming for the Internet in Perl 6”

πŸ“˜ Roles in Perl 6

Apart from the bare classes, the Perl 6 language allows roles. These are what are sometimes called interfaces in other object-oriented languages. Both the methods and the data, which are defined in a role, are available for β€œaddition” (or mixing-in) to a new class with the help of the does keyword. A role looks like … Continue reading “πŸ“˜ Roles in Perl 6”

πŸ“˜ Read and write in Perl 6 channels

In Perl 6, there is a predefined class Channel, which includes, among the others, the send and the receive methods. Here is the simplest example, where an integer number first is being sent to the channel $c and is then immediately read from it. my $c = Channel.new; $c.send(42); say $c.receive; # 42 A channel … Continue reading “πŸ“˜ Read and write in Perl 6 channels”

πŸ“˜ Transfer non-scalar objects through Perl 6 channels

Channels may also transfer both arrays and hashes and do it as easily as they work with scalars. Unlike Perl 5, an array will not be unfolded to a list of scalars but will be passed as a single unit. Thus, you may write the following code. my $c = Channel.new; my @a = (2, … Continue reading “πŸ“˜ Transfer non-scalar objects through Perl 6 channels”

πŸ“˜ Channels in Perl 6

Perl 6 includes a number of solutions for parallel and concurrent calculations. The great thing is that this is already built-in into the language and no external libraries are required. The idea of the channels is simple. You create a channel through which you can read and write. It is a kind of a pipe … Continue reading “πŸ“˜ Channels in Perl 6”

πŸ“˜ Basics of promises in Perl 6

The Promise.new constructor builds a new promise. The status of it can be read using the status method. Before any other actions are done with the promise, its status remains to be Planned. my $p = Promise.new; say $p.status; # Planned When the promise is kept, call the keep method to update the status to … Continue reading “πŸ“˜ Basics of promises in Perl 6”

πŸ“˜ The start keyword in Perl 6 promises

The start method creates a promise containing a block of code. There is an alternative way to create a promise by calling Promise.start via the start keyword. my $p = start { Β Β Β  42 } (Note that in Perl 6, a semicolon is assumed after a closing brace at the end of a line.) The … Continue reading “πŸ“˜ The start keyword in Perl 6 promises”

πŸ“˜ The then method in Perl 6 promises

The then method, when called on an already existing promise, creates another promise, whose code will be called after the β€œparent” promise is either kept or broken. my $p = Promise.in(2); my $t = $p.then({say “OK”}); # Prints this in two seconds say “promised”; # Prints immediately sleep 3; say “done”; The code above produces … Continue reading “πŸ“˜ The then method in Perl 6 promises”

πŸ“˜ An interpreter with Perl 6 grammars

So far, the grammar sees the structure of the programme and can tell if it is grammatically correct, but it does not execute any instructions contained in the programme. In this section, we will extend the parser so that it can actually execute the programme. Our sample language uses variables and integer values. The values … Continue reading “πŸ“˜ An interpreter with Perl 6 grammars”

πŸ“˜ Unicode in Perl 6

The strings in Perl 6 are internally handled in the format called NFG (Normalization Form Grapheme). From a practical point of view, that means that, for any symbol, you can get its NFC, NFD, NFKC and KFKD forms. I will refer you to read about the details of these formats to the Unicode standard. In … Continue reading “πŸ“˜ Unicode in Perl 6”

πŸ“˜ Working with files and directories in Perl 6

To get the content of a file, use the slurp built-in function, which reads the whole file and returns a string. say slurp “file.txt”; The function that does the opposite is called spurt, it writes the string to a file. Let us implement the Unix’s cp command in Perl 6. my ($source, $dest) = @*ARGS;Β  … Continue reading “πŸ“˜ Working with files and directories in Perl 6”

πŸ“˜ Database access in Perl 6

Install the DBIish module to get a powerful tool for working with databases*: $ panda install DBIish You also will need the database driver; for example, libmysqlclient for working with MySQL. Check the documentation of the DBIish module on modules.perl6.org if you want to work with a different database engine. The module provides an interface … Continue reading “πŸ“˜ Database access in Perl 6”

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