Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the -> arrow do in Perl?

Tags:

perl

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?

like image 498
fang yu Avatar asked Feb 06 '11 12:02

fang yu


People also ask

Can you use -> in C?

It is used with a pointer variable pointing to a structure or union. The arrow operator is formed by using a minus sign, followed by the greater than symbol as shown below. Operation: The -> operator in C or C++ gives the value held by variable_name to structure or union variable pointer_name.

What is $_ in Perl?

The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }

How do I increment in Perl?

The ++ and -- operators are used with a variable to increment or decrement that variable by 1 (that is, to add or subtract 1). And as with C, both operators can be used either in prefix fashion (before the variable, ++$x) or in postfix (after the variable, $x++).


1 Answers

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.

like image 177
Eugene Yarmash Avatar answered Sep 23 '22 19:09

Eugene Yarmash