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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s