Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the `->` syntax mean in Perl?

Tags:

perl

chdir($g_var->{g_loc});

I found this line in some perl code I am working with and I could not figure out what the -> means. I mean I cant find the meaning of the syntax. By the way, g_loc is the name of a folder. What am i missing here ?

P.S. i am only 4 days into perl.

like image 765
Chani Avatar asked Dec 10 '22 04:12

Chani


2 Answers

-> is dereferencing a reference. $g_var contains a reference to a %hash (elements of which you'd access using $hash{key}).

You can find more information about references in the perlreftut and perlref documentation. There's also perllol about lists-of-lists (nested references).

You can open the documentation using perldoc perlreftut, etc.

like image 198
Martijn Avatar answered Jan 02 '23 08:01

Martijn


This is what you get if you search for perlop (perl operators) on http://perldoc.perl.org. Perldoc, the on-version of it has undergone major improvements and frankly from all reference doc I like this the best.

"->" 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 30
andrei Avatar answered Jan 02 '23 08:01

andrei