+, -, *, 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.
Category: Perl 6 at a Glance
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”
Typed variables in Perl 6
This is how you declare a typed variable: my Int $x; Here, a scalar container $x may only hold an integer value. Attempts to assign it a value that is not an integer leads to an error: my Int $x; $x = “abc”; # Error: Type check failed in assignment to ‘$x’; # expected … Continue reading “ Typed variables 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.
Numeric operators to compare numbers in Perl 6
<, >, <=, and >= are the operands to compare Numeric values. <=> is the operator to compare numbers. It returns the value of the Order type, which can be Order::Less, Order::More, or Order::Same.
String concatenation operator ~ in Perl 6
~ does the string concatenation. The dot in Perl 6 is now used for dealing with methods; thus, a new operator for the string concatenation was required. The tilde was a good candidate because it is also used in other string-related operators in Perl 6. say “a” ~ “b”; # ab If necessary, the operator … Continue reading “ String concatenation operator ~ in Perl 6”
String comparison operators eq and ne in Perl 6
eq and ne compare strings for equality or non-equality, respectively.
String comparison operator leg in Perl 6
leg tells if is the two strings are equal or the left operand is less or greater than the second one. Its behaviour is similar to what <=> does for numbers or what the cmp built-in operator does in Perl 5. Like the cmp in Perl 6, the leg operator returns a value of the … Continue reading “ String comparison operator leg in Perl 6”
Universal comparison operators before and after in Perl 6
before and after are the comparison operators, which work with operands of different types. It returns a Boolean value of either True or False depending on which operand was considered to be ordered earlier or later. Type coercion is similar to how it happens with the cmp operator. Remember that depending on the data types … Continue reading “ Universal comparison operators before and after in Perl 6”
Smartmatch operator ~~ in Perl 6
~~ is the smartmatch operator. It compares the objects and tries to work correctly with the operands of any type (that is why the operator is called smart). say 42 ~~ 42.0; # True say 42 ~~ “42”; # True The result of the smartmatching depends on the operand order. Consider the following: say “42.0” … Continue reading “ Smartmatch operator ~~ in Perl 6”
Junction operators |, &, and ^ in Perl 6
|, &, and ^ create the so-called junctions (formerly known in Perl 6 as quantum superpositions). These objects can be used where a scalar is used but behave differently; unlike the scalars, the junctions have multiple values at the same moment in time. The |, &, and ^ operators create, respectively, the junctions of the … Continue reading “ Junction operators |, &, and ^ in Perl 6”
Invocant call operator : in Perl 6
: marks the left side of it as an invocant to call a method on, when a method of an object is used. It is easier to understand how it works in the following example. class C { method meth($x) { say “meth($x)”; } } my $o = C.new; meth($o: 42); # … Continue reading “ Invocant call operator : in Perl 6”