1. Perl
  2. Reference
  3. here

Hash reference

A data structure called hash reference frequently appears in Perl. This page details the hash reference from Reference.

What is a hash reference?

A hash reference is a reference to hash. A reference means to point. You can use the reference generator "\" on a hash to create a hash reference.

# Create a hash reference
my %score = (math => 68, english => 90);
my $score_ref = \%score;

Note that the hash reference is assigned to a scalar variable.

Let's write an image of the hash reference below.

# image
                      %score
$score_ref - -- -->(math => 68, english => 90)

A hash reference represents "what points to a hash" rather than the hash itself.

Hash reference is assigned and the contents are not copied

Let's see the difference between hashes and hash reference from here.

The hash is copied in its entirety by assignment, but the hash reference is not copied.

# Hash is the value copied
my %score1 = (math => 68, english => 90);
my %score2 = %score1;

# Hash reference does not copy value
my %score1 = (math => 68, english => 90);
my $score_ref1 = \%score1;
my $score_ref2 = $score_ref1;

Below is an image. In the case of a hash, the entire contents are copied, whereas in the hash reference, the reference itself is copied, not the contents.

# image
%score1%score2
(math => 68, english => 90) (math => 68, english => 90)

$score_ref1 - -- -- -- ->$score_ref2 - -- -- -- ->(math => 68, english => 90)

When passing to subroutine, it is more efficient to pass a hash reference because the contents are not copied.

# Pass the hash reference to the subroutine
my_func ($score_ref1);

Anonymous hash generator

Hash reference can be created using the reference generator "\", but this is a bit tricky. An operator called anonymous hash generator is defined to quickly create a hash reference.

my $score_ref = {math => 68, english => 90};

Note that the hash reference is generated using the anonymous hash generator "{}", whereas the normal hash is created with "()".

This notation appears frequently in Perl source code. It is important to read from the source code whether it is a hash or a hash reference.

# Hash
my %score = (math => 68, english => 90);

# Hash reference
my $score = {math => 68, english => 90};

Dereference

I used a reference generator to generate a hash reference. Now let's extract the hash from the hash reference, on the contrary.

To do this, perform an operation called dereference .

my $score_ref = {math => 68, english => 90};

# Dereference
my %score = %{$score_ref};

Dereference can be made using the notation "%{hash reference}".

If the hash reference is a scalar variable, you can omit the "{}".

my %score = %$score_ref;

Pass as an argument to a builtin functions

keys function, values function, each function take a hash as an argument. So be aware that if you want to pass a hash reference, you need to dereference it.

# Dereference and pass to builtin functions
keys %$hash;
values %$hash;
each %$hash;

Extract the hash value from the hash reference

You can use the arrow operator "->" to retrieve the hash value from the hash reference.

# Get the hash value from the hash reference
my $score_math = $score_ref->{math};

# Set the hash value from the hash reference
$score_ref->{math} = 67;

If the arrow operator is used, it is a hash reference. If not, it's a hash. Please be able to read this well in the source code.

# Get hash value
my $score_math = $score{math};

# Set hash value
$score{math} = 67;

It's important to look closely, as it depends on whether it's a hash or a hash reference, with or without the arrow operator.

Array reference

Array reference are as often used as hash reference.

my $nums_ref = [1, 3, 5];

Array reference are described in detail below.

Related Informatrion