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[59] to an array, a hash, or a subroutine, respectively:
$aref–>[42] # an array dereference
$href–>{"corned beef"} # a hash dereference
$sref–>(1,2,3) # a subroutine dereferenceIn 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.
$aref–>[42] = 'Huh!'; # autovivify an array in $aref
$href–>{"corned beef"} = 0; # autovivify a hash in $hrefIn either case, it also creates the new individual array or hash element with the assigned value. For more on this (and some warnings about accidental autovivification), see Chapter 8.
If the right side of the arrow is not one of those brackets, 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 or a method reference), and the left side must evaluate to either an object (a blessed reference) or a class name (that is, a package name):
my $yogi = Bear–>new("Yogi"); # a class method call
$yogi–>swipe('picnic basket'); # an object method callThe 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 ...
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