## Concatenates the texts retrieved in a node's (possibly nested) descendants # Note: preserve recursion with process_text_in($node) sub get_all_text_in { my $node = shift; # Concatenate the texts extracted from each child node my $text = reduce { $a .= process_text_in($b) } $node->content_list; } ## Get the text in a node, processing it if needed # Note: preserve recursion with get_all_text_in($node) sub process_text_in { my $node = shift; # Just text => get it return $text unless ref $node; # Not a special tag => get its children texts my $tag = $node->tag; return get_all_text_in($node) unless $action{$node->tag}; # Special tag => process it accordingly return $action{$tag}->($node); }