my $orig = 'doc.xml'; use autodie 1.999; use POSIX 'strftime'; my $back = $orig . strftime( '-%Y-%m-%d', localtime ); rename $orig, $back; open my $newfh, '>', $orig; my $t = XML::Twig->new( twig_roots => { 'qpass:merchant' => sub { my ( $t, $price ) = @_; { my $ra = $price->text; $ra =~ s/_/ /g; $price->set_text($ra); } $price->print($newfh); }, }, twig_print_outside_roots => $newfh, ); $t->parsefile($back); $t->flush; #don't forget undef $t; # close $newfh;