open (my $fh_out, '>', $out_file) or die "unable to open '$out_file' for writing: $!"; my $t = XML::Twig->new( twig_roots => {'display-name' => sub { convert( @_, $fh_out) } }, twig_print_outside_roots => $fh_out, # as mentioned in a previous response pretty_print => 'nice' ); # including data for ease of post $t->parsefile( $file); sub convert { my ($t, $elt, $fh) = @_; ... # do what you have to do to $elt $elt->print( $fh); # this is where you have to print it }