^ is a range-creating operator or the so-called upto operator. It creates a range (which is an object of the Range type) from 0 up to the given value (not including it). .print for ^5; # 01234 This code is equivalent to the following, where both ends of the range are explicitly specified: .print for … Continue reading “π Prefix operator ^ in Perl 6”
Month: October 2018
π Postfix operator ++ in Perl 6
Postfix operators are unary operators placed after their single operand. ++ is a postfix increment. The change of the value happens after the current value is used in the expression. my $x = 42; say $x++; # 42 say $x; Β Β # 43
π Method postfix operator . in Perl 6
There are a few syntactical elements in Perl 6, which start with a dot. These operators might look like a postfix operator, but they all are the forms of the calling a method on an object. Unlike Perl 5, the dot operator does not do any string concatenation. .method calls a method on a variable. … Continue reading “π Method postfix operator . in Perl 6”
π Numeric operator % in Perl 6
% is the modulo operator returning the remainder of the integer division. The operands are cast to integers first if necessary.
π String comparison operators lt, gt, le, and ge in Perl 6
lt, gt, le, and ge are the operators for comparing strings: less, more, less or equal, and more or equal. The operands are converted to the string values if necessary.
π Prefix operator ~ in Perl 6
~ casts an object to a string. Note that we are now talking about the prefix or a unary operator. If the tilde is used as an infix (see later in this chapter about what infixes are), it works as a string concatenating operator, but it still deals with strings. my Str $a = ~42; … Continue reading “π Prefix operator ~ in Perl 6”
π Prefix operator -β- in Perl 6
-β- is a prefix form of decrement. It works exactly like the ++ prefix but, of course, makes the operand smaller (whether it be a string or a number). my $x = 42; say –$x; # 41
π Prefix operator ?^ in Perl 6
?^ is a logical negation operator. Please note that this is not a bitwise negation. First, the argument is converted to a Boolean value, and then the result is negated. my $x = 10; my $y = ?^$x; say $y;Β Β Β Β Β Β # False say $y.WHAT;Β # (Bool)
π Prefix operator | in Perl 6
| flattens the compound objects into a list. For example, this operator should be used when you pass a list to a subroutine, which expects a list of scalars: sub sum($a, $b) { Β Β Β $a + $b }Β my @data = (10, 20); say sum(|@data); # 30 Without the | operator, the compiler will report … Continue reading “π Prefix operator | in Perl 6”
π Prefix operator let in Perl 6
let is a prefix operator, which is similar to temp, but works correctly with exceptions. The previous value of the variable will be restored if the scope was left because of the exception. my $var = ‘a’; try { Β Β Β let $var = ‘b’; Β Β Β die; } say $var; # a With a die, this … Continue reading “π Prefix operator let in Perl 6”
π Postfix operator -β- in Perl 6
-β- is a postfix decrement. Both postfix and prefix operators magically know how to deal with numbers in filenames. my $filename = ‘file01.txt’; for 1..10 { Β Β Β say $filename++; } This example prints the list of the filenames with incrementing numbers: file01.txt, file02.txt, … file10.txt.
π Method postfix operator .^ in Perl 6
.^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);
π 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.