What does $x -> {_foo} -> {_bar} in Perl mean?

I'm trying to understand a Perl script and I dont understand one declaration line which is:

$pin =  $x -> {_foo} -> {_bar}

whereas _bar is an undef variable declared in another sub.

Upvotes: 1

Views: 61

Answers (1)

ikegami
ikegami

Reputation: 385655

The value of $x is expected to be a reference to a hash.

$x->{_foo} is the value of the element with key _foo of the hash referenced by $x.

That value is expected to be a reference to a hash.

$x->{_foo}->{_bar} is the value of the element with key _bar of the hash referenced by $x->{_foo}.

For example, it would return 123 for the following:

my $x = {
   _foo => {
      _bar => 123,
   }
};

Upvotes: 4

Related Questions