mp has asked for the wisdom of the Perl Monks concerning the following question:
I have a nested data structure (hash at the top level, then mixture of hashes and arrays at lower levels, arbitrarily deep) that is read from a config file (Config::General).
I need a way to recursively search this data structure for particular hash keys, so that I can alter the data associated with them. For example, certain entries should always have a reference to an anonymous array, so I need to recursively search for a particular key, and wherever found, wrap it's value in an anonymous array if it is not already an array.
So, for example, given:
I need something that can find all occurrences of 'b' in this data structure, so that I can examine them and change them all to array references like this:{ a => { b => 3, } b => 123, c => { b => [ 123, 456, 789, ], }, d => [ b => 456, ], }
{ a => { b => [ 3, ] } b => [ 123, ], c => { b => [ 123, 456, 789, ], }, d => [ b => [ 456, ], ], }
Similar functionality to what I need exists in File::Find, File::Find::Rule, HTML::TreeBuilder (especially it's look_down method), but these all target either filesystem or file traversal rather than perl data structure traversal.
In the interest of avoiding reinventing a wheel that I'm sure exists somewhere, but I've been unable to find, has anyone found a CPAN module to do this sort of thing?