πŸ“˜ List creation operator , in Perl 6

, creates a List object. Note that this operator, as a few mentioned above, can be chained to accept more than two operands. my $what = (1, 2, 3); say $what.WHAT; # (List) The comma is also used as a separator of parameters passed to the subroutine. To create an empty list, use a pair … Continue reading “πŸ“˜ List creation operator , in Perl 6”

πŸ“˜ Assignment meta-operator = in Perl 6

The design of the operators in Perl 6 is very consistent. For example, if you add a new operator to the language, Perl 6 will create a few more to keep the harmony. In this section, we will talk about the so-called meta-operators, the operators over other operators. The assignment meta-operators (=) use the other … Continue reading “πŸ“˜ Assignment meta-operator = in Perl 6”

πŸ“˜ Reduction meta-operator [ ] in Perl 6

For any infix operator op the reduction form [op] also exists. The reduction operator takes a list, enrols its values, and inserts the operator between them. Examine the example with the [*] reduction operator: [*] 1..5 The form above is equivalent to the following line: 1 * 2 * 3 * 4 * 5 Reduction … Continue reading “πŸ“˜ Reduction meta-operator [ ] 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”

πŸ“˜ Class attributes in Perl 6

Class data variables are called attributes. They are declared with the has keyword. An attribute’s scope is defined via its twigil. As usual, the first character of the twigil indicates the type of the container (thus, a scalar, an array, or a hash). The second character is either . if a variable is public or … Continue reading “πŸ“˜ Class attributes in Perl 6”

πŸ“˜ Reverse meta-operator R in Perl 6

The prefix R forms the reverse operator for the infix operators, such as / or cmp. The reverse operator does the same as the original but changes the order of the operands. If necessary, it also changes the operator’s associativity. This matters when you have more than two operands in a row. For example, in … Continue reading “πŸ“˜ Reverse meta-operator R in Perl 6”

πŸ“˜ Cross meta-operator X in Perl 6

The cross meta-operator prefix, X, applies an operation to all the possible combinations of the elements of the operands that are treated in list context. The result of the cross-operation is also a list. Here is an example that prints the coordinates for all the cells of a chess board: say ‘a’..’h’ X~ 1..8;

πŸ“˜ Hyper-operators in Perl 6

Hyper-operators modify regular operators in such a way that the operation is applied to all the element of a list operand. Both unary and binary operators may be written in the hyper-operator form. To create a hyper-operator, add a pair of >> and/or << to the operation sign. Let’s start with a simple unary operator … Continue reading “πŸ“˜ Hyper-operators 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”

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