πŸ“˜ Default values in Perl 6 subs

Perl 6 also allows specifying the default values of the sub’s arguments. Syntactically, this looks like an assignment. sub i-live-in(Str $city = “Moscow”) {Β Β  Β Β Β  say “I live in $city.”;Β Β Β Β Β Β Β Β Β Β Β Β  } i-live-in(‘Saint Petersburg’); i-live-in(); # The default city It is also possible to pass values that are not known at the compile phase. When the … Continue reading “πŸ“˜ Default values in Perl 6 subs”

πŸ“˜ Subroutines, or subs in Perl 6

For a sub, which takes no arguments, its definition and the call are very straightforward and easy. sub call-me { Β Β Β  say “I’m called” } call-me; The syntax for declaring a sub’s parameters is similar to what other languages (including Perl 5.20 and higher) provide. sub cube($x) { Β Β Β  return $x ** 3; } say … Continue reading “πŸ“˜ Subroutines, or subs in Perl 6”

πŸ“˜ Anonymous code blocks in Perl 6

Perl 6 introduces the concept of so-called pointy blocks (or pointy arrow blocks). These are anonymous closure blocks, which return a reference to the function and can take arguments. The syntax of defining pointy blocks is an arrow -> followed by the argument list and a block of code. my $cube = -> $x {$x … Continue reading “πŸ“˜ Anonymous code blocks in Perl 6”

πŸ“˜ Non-value argument passing in Perl 6 subs

By default, you pass the arguments by their values. Despite that, it is not possible to modify them inside the sub. To pass a variable by reference, add the is rw trait. (Note that formally this is not a reference but a mutable argument.) sub inc($x is rw) { Β Β Β  $x++; } my $value = … Continue reading “πŸ“˜ Non-value argument passing in Perl 6 subs”

πŸ“˜ Anonymous subs in Perl 6

Let’s look at the creation of anonymous subs. One of the options (there are more than one) is to use syntax similar to what you often see in JavaScript. say sub ($x, $y) {$x ~ ‘ ‘ ~ $y}(“Perl”, 6); The first pair of parentheses contains the list of formal arguments of the anonymous sub; … Continue reading “πŸ“˜ Anonymous subs in Perl 6”

πŸ“˜ Modules in Perl 6

Basically, the Perl 6 modules are the files on disk containing the Perl 6 code. Modules are kept in files with the .pm extension. The disk hierarchy reflects the namespace enclosure, which means that a module named X::Y corresponds to the file X/Y.pm, which will be searched for in one of the predefined catalogues or … Continue reading “πŸ“˜ Modules in Perl 6”

πŸ“˜ Typed arguments in Perl 6 subs

Similarly to the above-described typed variables, it is possible to indicate that the sub’s parameters are typed. To do so, add a type name before the name of the parameter. sub say-hi(Str $name) { Β Β Β  say “Hi, $name!”; } If the types of the expected and the actual parameters do not match, a compile-time error … Continue reading “πŸ“˜ Typed arguments in Perl 6 subs”

πŸ“˜ Optional parameters in Perl 6 subs

Optional parameters are marked with a question mark after their names. The defined built-in function helps to tell if the parameter was really passed: sub send-mail(Str $to, Str $bcc?) { Β Β Β  if defined $bcc { Β Β Β Β Β Β Β  # . . . Β Β Β Β Β Β Β  say “Sent to $to with a bcc to $bcc.”; Β Β Β  } Β Β Β  else { … Continue reading “πŸ“˜ Optional parameters in Perl 6 subs”

πŸ“˜ Slurpy parameters and flattening in Perl 6

Perl 6 allows passing scalars, arrays, hashes, or objects of any other type as the arguments to a sub. There are no restrictions regarding the combination and its order in a sub declaration. For example, the first argument may be an array, and the second one may be a scalar. Perl 6 will pass the … Continue reading “πŸ“˜ Slurpy parameters and flattening in Perl 6”

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