http://qs321.pair.com?node_id=318738

IOrdy has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to search a nested structure and return an array with references to all occurrences that match something like:
->{foo}->{bar} or ->{foo}->[0]->{bar}
My recursive function (below) works fine for single instances like ->{bar} but somehow creates an endless loop with multiple parts (i.e ->{foo}->{bar}).
I think it has something to do with the eval corrupting $data but I dont understand why or how to fix it.

Any help would be appreciated.

The function:
sub rec_data { my $path = shift; # ->{foo}->{bar} my $data = shift; # nested perl structure my @results; # nothing to search return unless ref $data; # stop the evals complaining no warnings 'all'; if (ref $data eq 'HASH') { foreach my $k (keys %$data) { my $ref = qq|\$data->{$k}$path|; if (my $val = eval($ref)) { push @results, ref($val) ? $val : eval "\\$ref"; } if (ref $data->{$k}) { push @results, rec_data($path, $data->{$k}); } } } elsif (ref $data eq 'ARRAY') { foreach my $k (@$data) { my $ref = qq|\$k$path|; if (my $val = eval($ref)) { push @results, ref($val) ? $val : eval "\\$ref"; } if (ref $k) { push @results, rec_data($path, $k); } } } return @results; } # test data my $data = { test => { foo => { bar => 'bar', baz => 'baz' }, bar => { baz => 'baz' }, } } # search my @results = rec_data('->{foo}->{bar}', $data);