You have accidentally created an anonymous hash, which is a hash that has no identifier and is accessed only by reference.
The array @arr
is set to a single element which is a reference to that hash. Properly written it would look like this
use strict;
use warnings;
my @arr = (
{
a => "b",
c => undef,
}
);
print "$_\n" foreach @arr;
which is why you got the output
HASH(0x3fd36c)
(or something similar) because that is how Perl will represent a hash reference as a string.
If you want to experiment, then you can print the value of the first hash element by using $arr[0]
as a hash reference (the array has only a single element at index zero) and accessing the value of the element with key a
with print $arr[0]->{a}, "\n"
.
Note that, because hashes have to have a multiple of two values (a set of key/value pairs) the hash in your own code is implicitly expanded to four values by adding an undef
to the end.
It is vital that you add use strict
and use warnings
to the top of every Perl program you write. In this case the latter would have raised the warning
Odd number of elements in anonymous hash