Method postfix operators
In Chapter 4, Working with Operators, we did not cover the set of special postfix operators, which are related to object-oriented programming. Now it is time to fill that gap. The operators described in this section are the syntactic constructions but they may all be considered postfix operators.
To call a method on an object, the dot operator is used. We have been using it many times in this chapter:
class A { method m() { return 42; } } my $o = A.new; # calling the 'new' method say $o.m(); # calling the 'm' method
If the method does not exist, say, if you call $o.n()
, then the call fails:
No such method 'n' for invocant of type 'A'
To prevent the raise of an exception, the .?
form of the method call operator can help:
say $o.?m(); # 42 say $o.?n(); # Nil
An existing method is called as usual, while the call of the non-existing method returns Nil
and the program continues.
The .+
and .*
operators are used to call all the methods with the given name...