# small example my $tree = { top => { inetUser => {}, person => { organizationalPerson => { managedPerson => { personnel => {}, }, }, }, application => { managedApplication => {}, }, }, }; # I need to get the nodes that lead to the one I am looking for sub explore { my $node = shift; my $target = shift; my $stack = shift; # we found it, so we may leave... return $stack if $target eq $stack->[-1]; # we keep looking... my $kids = scalar keys %$node; if ( $kids > 0 ) { for my $key ( keys %$node ) { push @$stack, $key; show( $stack ); explore( $node->{$key}, $target, $stack, $OK ); pop @$stack; } } else { # leaf... no-op } } sub show { print "$_ " for @{$_[0]}; print "\n" } # test my $stack; eval { explore( $tree, 'personnel', [] ); # expected result: "top person organizationalPerson managedPerson personnel "; }; $stack = $@; show( $stack ); __END__