#! /bin/perl use Data::Dumper; my $hash = { 'wheels' => { 'four' => { 'car' => { 'plain' => { 'sporty' => 'honda', 'retro' => 'volkswagon', }, 'fancy' => { 'red' => 'ferrari', 'silver' => 'maserati', }, }, 'truck' => { 'light' => { 'chevy' => 'pickup', }, 'utility' => { 'us' => 'mail', 'city' => 'trash', 'muni' => 'power', }, }, }, 'one' => 'Unicycle', 'two' => { 'pedals' => 'bicycle', 'motorized' => 'motorcycle', }, 'three' => 'tricycle', }, }; ##--------------------------- ## DEEP REFERENCE ##--------------------------- fetch($hash, 'wheels'); fetch($hash, 'wheels', 'one'); fetch($hash, 'wheels', 'four', 'truck', 'utility'); fetch($hash, 'wheels', 'four', 'truck', 'utility', 'blah'); # doesn't exist sub fetch { my ($hash, @keys) = @_; print "==> Fetching [" . join(', ', @keys) . ']' . "\n"; my $results = deep_reference($hash, @keys); print Dumper($results), "\n\n"; } sub deep_reference { my ($hash, @keys) = @_; my $ref = undef; my $code = '$hash->' . join('->', map { "{$_}" } @keys); return eval $code; }