#!perl use warnings; use strict; sub safe_exists { my ($href, @list_of_descent) = @_; use List::Util 'first'; if (first { not (exists $href->{$_} and $href=$href->{$_}) } @list_of_descent) { return (); } else { return \$href; } } my %hash = ('a' => { 'value' => 1, 'foo' => 'bar', }, 'b' => { 'value' => 2, 'foo' => 'bar', }, 'c' => { 'value' => 3, 'foo' => 'bar', }, ); for (qw(a b c d)) { if (my $href = safe_exists(\%hash, $_, 'value')) { print "$_ value is $$href\n"; } else { print "Key $_ doesn't exist. Valid keys are: "; print join (", ", keys %hash), "\n"; } }