#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; use XML::Twig; my $doc = ' Some text here which may be any length and contain a number of child tags.'; my $exp = ' Some text here which may be any length and contain a quantity of child tags.'; # I got a little fancy here to allow several keywords to replace # the keywords are grouped in a regexp, sorted by inverse length so the alternation works properly my $replace = { number => 'quantity' }; my $keywords= join( '|', map { "\Q$_\E" } sort { length$b <=> length $a } keys %$replace); my $t=XML::Twig->new( twig_roots => { paragraph => \&subs_word })->parse( $doc); is( $t->sprint, $exp, 'one change') ; exit; sub subs_word { my( $t, $para)= @_; foreach my $text_elt ($para->children( '#TEXT')) { my $text= $text_elt->text; if( $text_elt->text=~ m{\b($keywords)\b}) { $text=~ s{\b($keywords)\b}{$replace->{$1}}g; $text_elt->set_text( $text); } }