Perl 6 defines the so-called submethods for classes. These are the methods which are not propagating to the subclassβs definition. The submethods may be either private or public, but they will not be inherited by the children. class A { Β Β Β submethod submeth { Β Β Β Β Β Β Β say “A.submeth” Β Β Β } } class B is A { … Continue reading “π Submethods in Perl 6 classes”
Author: Andrew Shitov
π The closed method in Perl 6 channels
The Channel class also defines a method that checks on whether the channel is closed. This method is called closed. my $c = Channel.new; say “open” if !$c.closed; # is openΒ $c.close; say “closed” if $c.closed; # closed Despite the simplicity of using the method, it in fact returns not a simple Boolean value but … Continue reading “π The closed method in Perl 6 channels”
π Named arguments in Perl 6 subs
Apart from the positional parameters (those that have to go in the same order both in the sub definition and in the sub call), Perl 6 allows named variables, somewhat similar to how you pass a hash to a Perl 5 subroutine. To declare a named parameter, a colon is used: sub power(:$base, :$exponent) { … Continue reading “π Named arguments in Perl 6 subs”
π Nested subs in Perl 6
Nested subs are allowed in Perl 6. sub cube($x) { Β Β Β sub square($x) { Β Β Β Β Β Β Β return $x * $x; Β Β Β } Β Β Β return $x * square($x); } say cube(3); # 27 The name of the inner sub square is only visible within the body of the outer sub cube.
π Lexical variables in Perl 6
Lexical variables in Perl 6 are those declared with the my keyword. These variables are only visible within the block where they were declared. If you tried accessing them outside the scope, youβd get the error: Variable ‘$x’ is not declared. { Β Β Β Β my $x = 42; Β Β Β Β say $x; # This is fine } … Continue reading “π Lexical variables in Perl 6”
π 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”
π 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”