#!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Declare the hash reference with an empty hash my $hash = {}; # assign the value "bar" to the key foo $hash->{foo} = 'bar'; # This prints the values in the hash # $VAR1 = { # 'foo' => 'bar' # }; print Dumper $hash; # you can retrieve the value by specifying the hash key directly also # prints "bar" print $hash->{foo} . "\n"; # we can use variables to assign key names: foreach my $key (qw(abc def lmnop zaxy)) { my $value = reverse $key; $hash->{$key} = $value; } # since this is test data, I've just reversed the key names # to show the differentiation between $KEY and $VALUE # Now our hash looks something like this: # $VAR1 = { # 'zaxy' => 'yxaz', # 'lmnop' => 'ponml', # 'def' => 'fed', # 'abc' => 'cba', # 'foo' => 'bar' # }; print Dumper $hash;