The scope of dynamic variables is calculated at the moment when a variable is accessed. Thus, two or more calls of the same code may produce different results. Dynamic variables are marked with the * twigil (a character clearly referencing a wildcard). In the following example, the echo() function prints a dynamic variable $*var, which … Continue reading “π Dynamic variables in Perl 6”
Category: Perl 6 at a Glance
π Placeholders in Perl 6
When an anonymous code block is created, declaring a list of arguments is not mandatory even when a block takes an argument. To let this happen, Perl 6 uses special variable containers, which come with the ^ twigil. This is similar to the predefined variables $a and $b in Perl 5. In the case of … Continue reading “π Placeholders in Perl 6”
π Sub overloading with subtypes in Perl 6
Multi subs can be made even more specific by using subtypes. In Perl 6, subtypes are created with the subset keyword. A subtype definition takes one of the existing types and adds a restriction to select the values to be included in the subtype range. The following lines give a clear view of how subtypes … Continue reading “π Sub overloading with subtypes in Perl 6”
π Exporting a sub from a module in Perl 6
The my and our variables, as well as subs, which are defined in the module, are not visible outside of its scope by default. To export a name, the is export trait is required. unit module X;Β sub x() is export { Β Β Β say “X::x()”; } This is all you need to do to be … Continue reading “π Exporting a sub from a module in Perl 6”
π Modules import summary in Perl 6
Here is a concise list of the keywords for working with modules. use loads and imports a module at compile time need loads a module at compile time but does not import anything from it import imports the names from the loaded module at compile time require loads a module at runtime without importing the … Continue reading “π Modules import summary in Perl 6”
Multiple inheritance in Perl 6
When more than one class is mentioned in the list of base classes, we have multiple inheritance. class A { Β Β Β method a { Β Β Β Β Β Β Β say “A.a” Β Β Β } } class B { Β Β Β method b { Β Β Β Β Β Β Β say “B.b”; Β Β Β } } class C is A is B { } my $c = C.new; … Continue reading “Multiple inheritance in Perl 6”
π Chapter 5. New Concepts in Perl 6
π Promises in Perl 6
Promises are objects aimed to help synchronize parallel processes. The simplest use case involving them is to notify if the earlier given promise is kept or broken or if its status is not yet known.
π Grammars in Perl 6
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.
π 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.