Consider the following simple piece of code:
%hash = ('a'=>1,'b'=>2);
print $hash{'b'};
print "\n",(\%hash)->{'b'}; #used when hashes are passed by reference
#to subroutines
The output, as expected, is a pair of 2’s. But I was thinking whether $hash{key} is a shorthand for the referencing & dereferencing done as (\%hash)->{key}, or is it an entirely different route to reach the same result.
Please give some clarification.
They’re somewhat different because unlike many other languages, where all complex types are only available as references, Perl have actual plain hash type and separate reference type that can act as proxy to any other type. You can find gory details about this in perlguts.
In the end those two examples both pull data from same storage, of course, but the second invocation is a bit longer because it spends time dutifully creating a reference to plain HV and then dereferencing it back just as you’ve asked. You can study details on what going under the hood using
B::Concisemodule.Concise output: