#! /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',
'corp' => {
'dom' => 'dominion power',
'vap' => 'virginia power',
},
},
},
},
'one' => 'Unicycle',
'two' => {
'pedals' => 'bicycle',
'motorized' => 'motorcycle',
},
'three' => 'tricycle',
};
##---------------------------
## DEEP REFERENCE
##---------------------------
fetch2($hash, 'wheels');
fetch2($hash, 'wheels', 'four', 'truck', 'utility', 'blah');
fetch2($hash, 'wheels', 'four', 'truck', 'utility', 'corp', 'dom');
## Using your suggested technique
sub fetch2 {
my ($hash, @keys) = @_;
print "==> Fetching [" . join(', ', @keys) . ']' . "\n";
print Dumper $hash->{shift @keys}{shift @keys};
}
####
==> Fetching [wheels]
$VAR1 = undef;
==> Fetching [wheels, four, truck, utility, blah]
$VAR1 = {
'car' => {
'plain' => {
'sporty' => 'honda',
'retro' => 'volkswagon'
},
'fancy' => {
'silver' => 'maserati',
'red' => 'ferrari'
}
},
'truck' => {
'light' => {
'chevy' => 'pickup'
},
'utility' => {
'city' => 'trash',
'muni' => 'power',
'us' => 'mail',
'corp' => {
'dom' => 'dominion power',
'vap' => 'virginia power'
}
}
}
};
==> Fetching [wheels, four, truck, utility, corp, dom]
$VAR1 = {
'car' => {
'plain' => {
'sporty' => 'honda',
'retro' => 'volkswagon'
},
'fancy' => {
'silver' => 'maserati',
'red' => 'ferrari'
}
},
'truck' => {
'light' => {
'chevy' => 'pickup'
},
'utility' => {
'city' => 'trash',
'muni' => 'power',
'us' => 'mail',
'corp' => {
'dom' => 'dominion power',
'vap' => 'virginia power'
}
}
}
};
##
##
==> Fetching [wheels]
$VAR1 = {
'three' => 'tricycle',
'one' => 'Unicycle',
'two' => {
'pedals' => 'bicycle',
'motorized' => 'motorcycle'
},
'four' => {
'car' => {
'plain' => {
'sporty' => 'honda',
'retro' => 'volkswagon'
},
'fancy' => {
'silver' => 'maserati',
'red' => 'ferrari'
}
},
'truck' => {
'light' => {
'chevy' => 'pickup'
},
'utility' => {
'city' => 'trash',
'muni' => 'power',
'us' => 'mail',
'corp' => {
'dom' => 'dominion power',
'vap' => 'virginia power'
}
}
}
}
};
==> Fetching [wheels, four, truck, utility, blah]
(nothing, because it doesn't exist)
==> Fetching [wheels, four, truck, utility, corp, dom]
$VAR1 = 'dominion power';