skjoshi skjoshi - 4 years ago 116
Perl Question

Understanding context in perl

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?

Answer Source

{} 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download