๐Ÿ“˜ The need keyword to use a module in Perl 6

To just load a module and do no exports, use the need keyword. Let us create a module named N, which contains the sub n(). This time, the sub is declared as our but with no is export. unit module N;ย  our sub n() { ย ย ย  say “N::n()”; } Then you need a module and … Continue reading “๐Ÿ“˜ The need keyword to use a module in Perl 6”

๐Ÿ“˜ Submethods in Perl 6 classes

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”

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

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

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