Perl Basics   «Prev 

Anonymouns Perl Hashes

An anonymous hash can be created by using curly brackets in the same way you would use the square brackets for an anonymous array:

$hashref = { 'foo' => 'bar', 'baz' => 'boz' };

A hash can include other anonymous hashes. Notice in this example the use of barewords for the keys. It's traditional to use uppercase letters for the barewords, which can prevent some confusion for the humans who may read the code sometime in the future:
$hashref = { 
  FOO => 'bar', 
  BAZ => 'boz', 
  COUPLES => { 
     'fred' => 'wilma',
     'samson' => 'delilah',
    },
  OTHERS => {
     'calvin' => 'hobbes',
     'tom' => 'jerry',
    },
  };

Dereferencing the hashes of hashes is similar to the techniques used for arrays of arrays:
$hashref->{FOO}                # bar
$hashref->{COUPLES}{'samson'}  # delilah