http://www.perlmonks.org?node_id=550838

GrandFather has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to inplace edit using XML::Twig, or have I got to go through the pain of creating a temporary file and replacing the original? Is there a better way to do that than the following?

# Edit the manifest my $saveStdOut = select; my $manifestOut; open $manifestOut, '>', "$manifestPath.new"; select $manifestOut; $sub = bindArgs (\&insertFiles, $absDir, \@fileList); my $twig = XML::Twig->new ( discard_spaces => 1, pretty_print => 'indented', twig_roots => {'sourceFiles' => $sub,}, # Insert file list twig_print_outside_roots => 1, # Print the rest ); $twig->parsefile ($manifestPath); select $saveStdOut; close $manifestOut; unlink $manifestPath; rename $manifestPath . '.new', $manifestPath;

DWIM is Perl's answer to Gödel