πŸ“˜ Prefix operators ! and not in Perl 6

Prefix operators are those that come in front of their operands. Obviously, prefix operators require only one operand. In some cases, the symbol of the operation can be used as an infix operator when it stands between two operands. ! is the Boolean negation operator. say !True;Β Β Β Β  # False say !(1 == 2); # True … Continue reading “πŸ“˜ Prefix operators ! and not in Perl 6”

πŸ“˜ Prefix operator + in Perl 6

+ is the unary plus operator, which casts its operand to the numerical context. The action is equivalent to the call of the Numeric method. my Str $price = ‘4’ ~ ‘2’; my Int $amount = +$price; say $amount;Β Β Β Β Β Β  Β # 42 say $price.Numeric; # 42 We will see one of the important use cases of … Continue reading “πŸ“˜ Prefix operator + in Perl 6”

πŸ“˜ Method postfix operator .+ in Perl 6

.+method makes an attempt to call all the methods with the given name on an object. This may be used, for example, when an instance is a part of the hierarchy of objects and its parent also has a method with the same name. More on the classes and class hierarchy in Chapter 4. class … Continue reading “πŸ“˜ Method postfix operator .+ in Perl 6”

πŸ“˜ Prefix operator ++ in Perl 6

++ is a prefix operator of increment. First, an increment is done, and then a new value is returned. my $x = 41; say ++$x; # 42 The increment operation is not limited to working only with numbers. It can also handle strings. my $a = ‘a’; say ++$a; # b A practical example is … Continue reading “πŸ“˜ Prefix operator ++ in Perl 6”

πŸ“˜ Prefix operator ^ in Perl 6

^ 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”

πŸ“˜ 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”

πŸ“˜ 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

| 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.