.^method calls a method on the objectβs metaobject. A metaobject is an instance of the HOW class and contains some additional information about the object. The following two operations, applied to the $i variable, are equivalent and print the list of the methods available for the Int variables. my Int $i; say $i.^methods(); say $i.HOW.methods($i);
Category: Chapter 2. Operators
π Infix operators in Perl 6
Infix operators are placed in a programme between two operands. The majority of the infix operators are binary, and there is a single ternary operator, which expects three operands. The simplest example of the binary operator is an addition operator +. On the right and left sides it expects two values, for example, two variables: … Continue reading “π Infix operators in Perl 6”
π Numeric operator %% in Perl 6
%% is the so-called divisibility operator: it tells if the integer division with no remainder is possible for the given pair of operands. say 10 %% 3; # False say 12 %% 3; # True
π Numeric operators >, < in Perl 6
<, >, <=, and >= are the operands to compare Numeric values.
π Universal comparison operator === in Perl 6
=== returns a True value if both operands are the same value. Otherwise, it returns False. This operator is also known as the value identity operator. class I { }Β # Three different instances my $i = I.new; my $ii = I.new; my $iii = I.new;Β my @a = ($i, $ii, $iii); for @a -> … Continue reading “π Universal comparison operator === in Perl 6”
π Ternary operator ?? !! in Perl 6
?? !! is the ternary operator. It works as its counterpart ? : in Perl 5. The characters are doubled to avoid the mixture with infix operators ? and !, which change the context of their operands. say rand < 0.5 ?? ‘Yes’ !! ‘No’;
π Method postfix operator .= in Perl 6
.=method is a mutating call of the method on an object. The call $x.=method does the same as the more verbose assignment $x = $x.method. In the following example, the $o container initially holds an object of the C class, but after $o.=m(), the value will be replaced with an instance of the D class. … Continue reading “π Method postfix operator .= in Perl 6”
π Method postfix operator .? in Perl 6
.?method calls a method if it is defined. If the object does not have a method with the given name, Nil is returned. class C { Β Β Β method m() {‘m’} }Β my $c = C.new(); say $c.?m(); # m say $c.?n(); # Nil
π Method postfix operator .* in Perl 6
.*method calls all the methods with the given method name and returns a parcel with the results. If the method is not defined, an empty list is returned. In the rest, it behaves like the .+ operator.
π Numeric operators +, -, *, and / in Perl 6
+, -, *, and / are the operators executing the corresponding arithmetical operations and do not require any comments. When working with Perl 6, keep in mind that before the operation is executed, the operands will be automatically converted to the Numeric type if it is necessary.
π Numeric operators div and mod in Perl 6
div is the integer division operator. If the floating point is truncated, the result is rounded to the preceding lower integer. say 10 div 3;Β # 3 say -10 div 3; # -4 mod is another form of the modulo: say 10 % 3;Β Β # 1 say 10 mod 3; # 1 Unlike the / … Continue reading “π Numeric operators div and mod in Perl 6”
π Numeric operators +&, +|, and +^ in Perl 6
+&, +|, and +^ are the bitwise operands for the multiplication, addition, and XOR operations. The plus character in the operators suggests that the operands will be converted to the integer type if necessary.
π Numeric operator gcd in Perl 6
gcd calculates the greatest common denominator of the two integer operands. say 50 gcd 15; # 5
π String repetition operator x in Perl 6
x repeats the string the given number of times. say “A” x 5; # AAAAA Non-string values will be converted to strings before the operation. say 0 x 5; # 00000 If the number of repetitions is negative or zero, an empty string is returned.
π Chapter 2. Operators in Perl 6
The meanings of the many of the operators in Perl 6 are quite obvious even for those who are not familiar with Perl 5. On the other hand, sometimes the behaviour of the operator contains some tiny details that you may not think of. In this chapter, we will list some operators, giving some comments … Continue reading “π Chapter 2. Operators in Perl 6”
π Zip operator Z in Perl 6
Z is the zip operator. It mixes the content of its two operands like a zipper does. The operator continues mixing while there are enough data in both operands. The code @c = @a Z @b; is equivalent to the following: @c = ((@a[0], @b[0]), (@a[1], @b[1]), …); Consider another example: my @a = ^5; … Continue reading “π Zip operator Z in Perl 6”
π Zip meta-operator Z in Perl 6
The zip meta-operator prefix, Z, combines the corresponding elements of its list operands like the zipper does. The record @a Z+ @b is equivalent to this (in Perl 6, the last element of an array is indexed as *-1, not just -1; see the details in Appendix): ((@a[0] + @b[0]), (@a[1] + @b[1]), . . … Continue reading “π Zip meta-operator Z in Perl 6”
π Numeric operators +> and +< in Perl 6
+< and +> are the left and right shift operators.
π Numeric operator lcm in Perl 6
lcm finds the least common multiple value for the given operands. say 1043 lcm 14; # 2086
π Numeric operators == and != in Perl 6
== and != compare the two Numeric operands. Typecast is executed first if needed.