📘 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

The not operator does the same but has lower precedence.

say not False; # True

📘 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 the unary plus in Chapter 6: +$/. That construction converts an object of the Match class that contains information about the matched part of the regular expression into a number.

📘 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 A {
    method m($x) {"A::m($x)"}
class B is A {
    method m($x) {"B::m($x)"}

my $o = B.new;
my @a = $o.+m(7);
say @a; # Prints [B::m(7) A::m(7)]

Here, the $o object has the m method in both its own class B and in its parent class A. The $o.+m(7) calls both of the methods and puts their results in a list.

If the method is not defined, an exception will be thrown.