πŸ“˜ Class methods in Perl 6

The method keyword defines a method, similarly to how we define subroutines with sub. A method has access to all attributes of the class, both public and private. The method itself can be private. We will return to this later after talking about inheritance. In the following short example, two methods are created, and each … Continue reading “πŸ“˜ Class methods in Perl 6”

πŸ“˜ Inheritance in Perl 6

Inheritance is easy. Just say is Baseclass when declaring a class. Having said that, your class will be derived from the base class. class A { Β Β Β  method x { Β Β Β Β Β Β Β  say “A.x” Β Β Β  } Β Β Β  method y { Β Β Β Β Β Β Β  say “A.y” Β Β Β  } } class B is A { Β Β Β  method x { Β Β Β Β Β Β Β  … Continue reading “πŸ“˜ Inheritance in Perl 6”

πŸ“˜ Private (closed) methods in Perl 6 classes

Now, after we have discussed inheritance, let us return to the private (or closed) methods. These methods may only be used within the class itself. Thus, you cannot call them from the programme that uses an instance of the class. Nor are they accessible in the derived classes. An exclamation mark is used to denote … Continue reading “πŸ“˜ Private (closed) methods in Perl 6 classes”

πŸ“˜ Constructors in Perl 6 classes

You may have noticed in the previous examples that two different approaches to creating a typed variable were used. The first was via an explicit call of the new constructor. In this case, a new instance was created. my $a = A.new; In the second, a variable was declared as a typed variable. Here, a … Continue reading “πŸ“˜ Constructors in Perl 6 classes”

πŸ“˜ The list method in Perl 6 channels

The list method accompanies the previously seen methods and returns everything that is left unread in the channel. my $c = Channel.new;Β  $c.send(5); $c.send(6);Β  $c.close; say $c.list; # (5 6) The method blocks the programme until the channel is open, thus it is wise to close it before calling the list method.

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