As an aside, it can be useful to rig-up a “data walker” object that will iterate through a data-structure ... search http://search.cpan.org for “walker” ... and then perhaps maintain a push-down stack of such objects. Once, I solved a similar problem by devising a walker that would call a specified sub (coderef ...) when it found a matching node. Each subroutine in turn made the decisions that needed to be made at that level, and spawned another walker-instance to proceed further. Thus, the walkers handled the task of navigation while the subroutines made the case-specific decisions. (It was a lot easier to do than to describe.) I did it this way because, while I could not predict what the shape and depth of the structure would be, I could describe the logic as a small number of reusable, node-specific cases.