Method Invocation Using Indirect Objects
The second style of method invocation looks like this:
METHODINVOCANT(LIST)METHODINVOCANTLISTMETHODINVOCANT
The parentheses around LIST are
optional; if omitted, the method acts as a list operator. So you can
have statements like the following, all of which use this style of
method call. Notice the lack of a comma after the class name or
instance:
no feature "switch"; # for given forgiveness (see below) $mage = summon Wizard "Gandalf"; $nemesis = summon Balrog home => "Moria", weapon => "whip"; move $nemesis "bridge"; speak $mage "You cannot pass"; break $staff; # safer to use: break $staff ();
The list operator syntax should be familiar to you; it’s the same
style used for passing filehandles to print or printf:
print STDERR "help!!!\n";
It’s also similar to English sentences like “Give Gollum the
Preciousss”, so we call it the indirect object form. The invocant
is expected in the indirect
object slot. When you read about passing a built-in function
like system or exec something in its “indirect object slot”,
this means that you’re supplying this extra, comma-less argument in the
same place you would when you invoke a method using the indirect object
syntax.
The indirect object form even permits you to specify the
INVOCANT as a
BLOCK that evaluates to an object (reference)
or class (package). This lets you combine those two invocations into one
statement this way:
speak { summon Wizard "Gandalf" } "friend";Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access