πŸ“˜ Dynamic variables in Perl 6

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”

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

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