Curly braces and hashes
⇐ Back to Data: Lists, Scalars, and Hashes oh my!
Questions, comments, or corrections? Send me a message.
Curly braces are used to create and access hash references. To create a normal, pre-populated hash, you might do this:
%myhash = ( key1 => 'foo', key2 => 'bar' );And you have $myhash{key1} == 'foo', etc. The reference is acquired the same way as other references:
$hash_ref = \%myhash;Hash refs use the {} along with the -> indirection operator for access like this:
$hash_ref -> { key1 } eq $myhash{ key1 }; $hash_ref -> { key2 } eq $myhash{ key2 };Like with parenthesis and brackets, the curly braces can also be used to create a new hash reference without first creating the hash:
$hash_ref = { key1 => 'foo', key2 => 'bar' };Gives us the same hash as the first example.