use strict; use warnings; use Data::Dumper; my %hash = ( one => { test => 3 }, two => { Ovid => { is => { a => 'fool' } } } ); if ( hash_branch_exists( \%hash, qw/ one test / ) ) { print "Good\n"; } else { print "Bad\n"; } if ( hash_branch_exists( \%hash, qw/ Ovid is a fool / ) ) { print "Good\n"; } else { print "Bad\n"; } if ( hash_branch_exists( \%hash, qw/ two Ovid Is a / ) ) { print "Good\n"; } else { print "Bad\n"; } sub hash_branch_exists { my ( $hash_ref, @fields ) = @_; foreach ( @fields ) { return 0 if ref $hash_ref ne 'HASH'; return 0 if ! exists $hash_ref->{ $_ }; $hash_ref = $hash_ref->{ $_ }; } return 1; }