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”
Month: October 2018
π Universal comparison operator cmp in Perl 6
There are a few operators, which can compare both strings and numbers, or even compound objects like pairs. cmp compares two objects and returns a value of the Order type, either Less, or Same, or More. say 2 cmp 2;Β Β # Same say 2 cmp 2.0; # Same say 1 cmp 2;Β Β # Less say … Continue reading “π Universal comparison operator cmp in Perl 6”
π Universal comparison operator eqv in Perl 6
eqv is an operator that tests the two operands for equivalence. It returns the True value if the operands are of the same type and contain the same values. my $x = 3; my $y = 3; say $x eqv $y; # True An example with a bit more complex data structures: my @a = … Continue reading “π Universal comparison operator eqv in Perl 6”
π Universal comparison operator =:= in Perl 6
=:= is the operator to check if the operands refer to the same object. A Boolean value is returned. The operator is called the container identity operator. my $x = 42; my $y := $x;Β say $x =:= $y; # True say $y =:= $x; # True
π List repetition operator xx in Perl 6
xx repeats the list the given number of times. say (1, -1) xx 2; # ((1 -1) (1 -1)) Like the string x operator, the xx operator returns an empty list if the number of repetitions is zero or negative.
π Cross-product operator X in Perl 6
X is the cross product operator, which converts the two given lists to a third one containing all the possible combinations of the elements from the original lists. @c = @a X @b; This is the same as the following sequence: @c = ((@a[0], @b[0]), (@a[0], @b[1]), (@a[0], @b[2]), … (@a[N], @b[0]), (@a[N], @b[1]), … … Continue reading “π Cross-product operator X in Perl 6”
π Sequence operator … in Perl 6
… creates a sequence and is called a sequence operator. my @list = 1 … 10; The operator can also count backwards: my @back = 10 … 1;
π Shortcut operator || in Perl 6
|| returns the first operand, which is True in a Boolean context. The remaining operands are not evaluated. say 10 || 0; # 10 say 0 || 10; # 10
π Shortcut operator // in Perl 6
// returns the first defined operand. The operator is called a defined-or operator. It is also is a shortcut operator. my $x; my $y = 42; my $z; say $x // $y // $z; # 42
π Pair creation operator => in Perl 6
=> creates an object of the Pair type. The Pair is a βkey; valueβ combination, like those used in hashes. In the code, it is not always necessary to quote the key values. my $pair = alpha => “one”;Β my %data = jan => 31, feb => 28, mar => 31;
π Negation meta-operator ! in Perl 6
The negating exclamation mark ! is used to create the next set of meta-operators. If it is prepended to the existing operator op, you get the operator that gives you the negated result !op. say “no” if “abcd” !~~ “e”;
π 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”
π Shortcut operator && in Perl 6
&& returns the first of the operands, which, after being converted to a Boolean value, becomes False. If none are False, then the last element is returned. Please note that the result is not a Boolean value but the value of one of the operands (unless they are Boolean already). say 10 && 0; # … Continue reading “π Shortcut operator && in Perl 6”
π Shortcut operator ^^ in Perl 6
^^ returns an operand that is True in a Boolean context and that is, at the same time, the only one in the given list. If nothing is found, Nil is returned. As soon as the operator sees the second True value, it stops evaluating the rest, because the result is already known. say 0 … Continue reading “π Shortcut operator ^^ in Perl 6”
π Infix operators min and max in Perl 6
min and max return, correspondently, the minimum and maximum value of their operands. say 20 min 10;Β Β Β Β Β Β Β Β # 10 say ‘three’ max ‘two’; # two (sorted alphabetically)
π Assign operator = in Perl 6
= assigns a value to a variable.
π 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”