Grammars are the development of regular expressions. Syntactically, the grammar is defined similar to a class but using the keyword grammar. Inside, it contains tokens and rules. In the next section, we will be exploring the grammar in the examples.
Category: Raku
π Creating a module in Perl 6
The keyword module declares a module. The name of the module is given after the keyword. There are two methods of scoping the module. Either it can be a bare directive in the beginning of a file, or the whole module can be scoped in the code block within the pair of braces. In the … Continue reading “π Creating a module in Perl 6”
π Using a module in Perl 6
To use a module in your code, use the keyword use. An example. Let us first create the module Greet and save it in the file named Greet.pm. unit module Greet;Β sub hey($name) is export { Β Β Β say “Hey, $name!”; } Then, let us use this module in our programme by saying use Greet. use … Continue reading “π Using a module in Perl 6”
π Importing a module in Perl 6
The use keyword automatically imports the names from modules. When a module is defined in the current file in the lexical scope (please note that the module can be declared as local with my module), no import will be done by default. In this case, importing the names should be done explicitly with the import … Continue reading “π Importing a module in Perl 6”
π The require keyword to use a module in Perl 6
The require keyword loads a module at a runtime unlike the use, which loads it at the compile-time. For example, here is a module with a single sub, which returns the sum of its arguments. unit module Math;Β our sub sum(*@a) { Β Β Β return [+] @a; } (The star in *@a is required to tell … Continue reading “π The require keyword to use a module in Perl 6”
π Chapter 4. Classes
We have already seen elements of the object-oriented programming in Perl 6. Methods may be called on those variables, which do not look like real objects from the first view. Even more, methods may be called on constants. The types that were used earlier (like Int or Str) are container types. Variables of a container … Continue reading “π Chapter 4. Classes”
π 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.
π Factory methods in Perl 6 promises
There are a few factory methods defined in the Promise class.
π 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”