This is exactly what I was looking for. In just one pass ... This is impressive. I tried it out in my code and this works flawlessly against a whole router config.
I think I'd like to add a logic to capture parents without a child on the first level and push them in a separate array or in an arrayref inside the structure, but all I can say is WOW.
Could you please explain the regex in the while loop ? Otherwhise, I get that you're using the ternary operator to dig deeper ... this solves my challenge in a single line. Respect !