The Arrow Operator
Just as in C and C++, the binary
->
operator is an infix dereference operator. If
the right side is a […]
array subscript, a
{…}
hash subscript, or a (…)
subroutine argument list, the left side must be a reference (either
hard or symbolic) to an array, a hash, or a subroutine, respectively.
In an lvalue (assignable) context, if the left side is not a reference,
it must be a location capable of holding a hard reference, in
which case such a reference will be autovivified
for you. For more on this (and some warnings
about accidental autovivification) see Chapter 8.
$aref->[42] # an array dereference $href->{"corned beef"} # a hash dereference $sref->(1,2,3) # a subroutine dereference
Otherwise, it's a method call of some kind. The right side must be a method name (or a simple scalar variable containing the method name), and the left side must evaluate to either an object (a blessed reference) or a class name (that is, a package name):
$yogi = Bear->new("Yogi"); # a class method call $yogi->swipe($picnic); # an object method call
The method name may be qualified with a package name to indicate
in which class to start searching for the method, or with the special
package name, SUPER:
:, to indicate that the search
should start in the parent class. See Chapter 12.
Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.