๐Ÿ“˜ Invocant call operator : in Perl 6

: marks the left side of it as an invocant to call a method on, when a method of an object is used. It is easier to understand how it works in the following example.

class C {
ย ย ย  method meth($x) {
ย ย ย ย ย ย ย  say "meth($x)";
ย ย ย  }

my $o = C.new;
meth($o: 42); # The meth method of the $o object is called,
ย ย ย ย ย ย ย ย ย ย ย ย ย  # it prints โ€œmeth(42)โ€

The form meth($o: 42) is equivalent to the classical form $o.meth(42). Note that you cannot omit a space following the colon (otherwise, it will be interpreted as a named argument).

Another common Perl 6 idiom for the use of : is to prevent having parentheses with method calls. The following two lines of code are equivalent:

say "abcd".substr: 1, 2; # bc
say "abcd".substr(1, 2); # bc

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