In the beginning, there were only scalars, hashes, and arrays. Actually, that was Perl4, but when Perl5 was being designed, absolute backwards compatibility was a key point. This means that $hash{entry} = @array
would have to keep assigning the array size to that hash entry rather than putting the array into the hash. So collections can only contain scalars.
This is one of the main reasons why references were needed – hash references and array references allow us to refer to a collection as a single scalar thing. The syntax for hash references needs to be different from that of hashes. The dereference arrow ->
(borrowed from C) can be used for a hashref access: $hashref->{entry}
. Because hashes can never contain a collection directly, the dereference arrow between subscripts is unnecessary for disambiguation between hashes and hashrefs, so $ref->{a}->{b}
and $ref->{a}{b}
do the same thing. However, hashref accesses cannot use $ref{entry}
because that syntax is already in use for hash accesses, as both $name
and %name
can exist at the same time as separate variables.
So in short, the syntax is ugly, but for good backwards compatible reasons.
Currently, for my %hash (@hash_references)
or for my @array (@hash_references)
aren't allowed as far as I'm aware. I personally don't see any issues with this syntax, but implicit dereferencing is admittedly rather confusing. Having to use %$ref
rather than %hash
isn't really that bad.
By the way, Perl6 got rid of references, because this difference between hashes and hashrefs is quite unnecessary aside from backwards compatibility. Collection variables behave more like scalars, although there are new contexts like an “item context” to help with the lack of references.