0

I am ramping up on Perl programming and understand some basics of Perl. As per my understanding, if an array is assigned to scalar, it will store number of element in scalar. However, I am not clear in terms of hashes. For example, I saw this line:

my $variable = {};

I am not able to understand, how this $variable is still working as hash? What am I missing here?

skjoshi
  • 2,493
  • 2
  • 27
  • 38

1 Answers1

3

{} creates a hashref, which is a scalar.

You need to dereference it to access the items inside it.

my $hashref = { foo => 1 };
say $hashref->{foo};

my %hash = ( foo => 1 );
say $hash{foo};

See perldoc perlref for more details.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335