my $tree = { top => { inetUser => {}, person => { organizationalPerson => { managedPerson => { personnel => {}, }, }, }, application => { managedApplication => {}, }, }, }; sub explore { my $node = shift; my $target = shift; my $stack = shift; if( $target eq $stack->[$#stack]) { show( $stack ); exit 0; } my $kids = scalar keys %$node; if ( $kids > 0 ) { for my $key ( keys %$node ) { push @$stack, $key; explore( $node->{$key}, $target, $stack, $OK ); pop @$stack; } } } sub show { print "$_ " for @{$_[0]}; print "\n" } my $stack; explore( $tree, 'personnel', []);