📘 state variables in Perl 6

State variables (declared with the keyword state) appeared in Perl 5.10 and work in Perl 6. Such variables are initialized during the first call and keep their values in subsequent sub calls. It is important to keep in mind that a single instance of the variable is created. Let us return to the example with … Continue reading “📘 state variables in Perl 6”

📘 Function overloading in Perl 6

The multi keyword allows defining more than one function (or subroutine, or simply sub) with the same name. The only restriction is that those functions should have different signatures. In Perl 6, the signature of the sub is defined together with its name, and the arguments may be typed. In the case of multi subs, … Continue reading “📘 Function overloading in Perl 6”

📘 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”

📘 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”