"Perl for Perl Newbies" - Part 2ReferencesThe Arrow Operators

10.5. The Arrow Operators

An arrow (->) followed by a square or curly brackets can be used to directly access the elements of an array or a hash referenced by a certain hash. For instance: $array_ref->[5] will retrieve the 5th element of the array pointed to by $array_ref.

As an example let's print a tree of the contents of the part of the lecture that was presented in the previous slide:

use strict;
use warnings;

do "lol.pl";         # Load the other file

my $cont = get_contents();

print $cont->{'title'}, "\n";
print $cont->{'subs'}->[0]->{'url'}, "\n";
print $cont->{'subs'}->[0]->{'subs'}->[1]->{'title'}, "\n";

Note that the arrows following the first arrow are optional as perl sees that the programmer wishes to access the subseqeunt sub-items. However, the first one is mandatory because the expression $array_ref{'elem'} looks for the hash %array_ref.

Written by Shlomi Fish