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”
Category: Programming languages
π 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”
π 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”