sub formatText { my ($node, $target, $tag) = @_; my @children = $node->detach_content (); my @newChildren; for (@children) { if (ref) { formatText ($_, $target, $tag); push @newChildren, $_; next; } my @fragments = split /($target)/, $_; while (@fragments) { push @newChildren, shift @fragments; last if ! @fragments; my $element = HTML::Element->new ($tag); $element->push_content (shift @fragments); push @newChildren, $element; } } $node->push_content (@newChildren) if @newChildren; }