Reputation: 481
I keep seeing ->
in code and I am not exactly sure what it means. For example:
@top_links = @{$m->links};
What does this line do? Also, where can I maybe read about it?
Upvotes: 46
Views: 83835
Reputation: 4347
Perl arrow operator has one other use:
Class−>method invokes subroutine method in package Class
though it's completely different than your code sample. Only including it for completeness for the question in the title.
Upvotes: 7
Reputation: 149756
See The Arrow Operator in perlop:
"
->
" is an infix dereference operator, just as it is in C and C++. If the right side is either a[...]
,{...}
, or a(...)
subscript, then the left side must be either a hard or symbolic reference to an array, a hash, or a subroutine respectively. (Or technically speaking, a location capable of holding a hard reference, if it's an array or hash reference being used for assignment.) See perlreftut and perlref.Otherwise, the right side is a method name or a simple scalar variable containing either the method name or a subroutine reference, and the left side must be either an object (a blessed reference) or a class name (that is, a package name). See perlobj.
Upvotes: 41
Reputation: 300825
The outer @{}
part is a dereference - this implies $m->links
is a reference to an array, so this operator deferences it to refer to the array directly, which is then copied to @top_links
Chapter 8 of the 'Camel Book' is a good place to learn about references, or the perlref section of the Perl documentation.
The ->
arrow operator is an object dereference, in this case fetching the links
member of the $m
object. Similarly, later chapters of the Camel Book cover Perl's object oriented support in detail. See the perlobj section of the Perl documentation also.
Upvotes: 7
Reputation: 39158
In general, ->
hints at dereference. This syntax comes from C.
In your example, it means method call: call method links
from the object instance in $m
. This is explained in the chapter Objects in Modern Perl. Pieces of perlsub, perlfaq7, perlobj, perltoot provide the details.
For references in general, search Stack Overflow; it is a recurring topic.
Upvotes: 15