... #### 1st node: Text of 1st node 2nd node: Text of 2nd node subnode A: Text of subnode A subnode B: Text of subnode B 3rd node: Text of 3rd node #### foreach my $node (@{$data->{node}}) { print $node->{name}.": ".$node->{text}."\n"; if ( ... ) { # detect the existence of a subnode here somehow foreach my $subnode (@{$data->{node}{subnode}}) { print "\t".$subnode->{name}.": ".$subnode->{text}."\n"; } } }