Are you suggesting that with a few modifications that I can use your code snippet to achieve my goal? Because I don't see how I can push the children onto their parents using your code (which doesn't produce the output I need).
Yes, this kind of question has been discussed before. You may also be interested in nodes referenced in that thread. Two modules, that were mentioned in the discussion and that could come useful to make "structured text" into a complex data structure are Data::Hierarchy and Data::Diver.