Method Invocation Using the Arrow Operator
We mentioned that there are two styles of method invocation. The first style for invoking a method looks like this:
INVOCANT–>METHOD(LIST)INVOCANT–>METHOD
For obvious reasons, this style is usually called the arrow form of invocation. (Do not
confuse –> with =>, the
“double-barrelled” arrow used as a fancy comma.) Parentheses are
required if there are any arguments. When executed, the invocation first
locates the subroutine determined jointly by the class of the
INVOCANT and the
METHOD name, and then calls that subroutine,
passing INVOCANT as its first
argument.
When INVOCANT is a reference, we say
that METHOD is invoked as an instance method;
when INVOCANT is a package name, we say that
METHOD is invoked as a class method. There
really is no difference between the two, other than that the package
name is more obviously associated with the class itself than with the
objects of the class. You’ll have to take our word for it that the
objects also know their class. We’ll tell you in a bit how to associate
an object with a class name, but you can use objects without knowing
that.
For example, to construct an object using the class method
summon and then invoke the instance
method speak on the resulting object,
you might say this:
$mage = Wizard–>summon("Gandalf"); # class method
$mage–>speak("friend"); # instance methodThe summon and speak methods are defined by the Wizard class—or one of the classes from which it inherits. But you shouldn’t worry ...
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