Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Inplace edit using XML::Twig

by GrandFather (Sage)
on May 21, 2006 at 23:32 UTC ( #550838=perlquestion: print w/replies, xml ) Need Help??
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, '>', "$"; 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

Replies are listed 'Best First'.
Re: Inplace edit using XML::Twig
by mirod (Canon) on May 22, 2006 at 10:02 UTC
      Life would be so much harder without XML::Twig - nice one!
Re: Inplace edit using XML::Twig
by kvale (Monsignor) on May 22, 2006 at 03:40 UTC
    Why not just read in the file contents, truncate the file, and write processed file contents back out to the same file? No delete and rename needed.


      There are two reasons, firstly renaming is an atomic operation on all the systems that I am aware of so there is no risk of another application that can't be made to co-operate with this one getting a partially written file, secondly writing to a second file mitigates against the risk of the file being corrupted if the process is interrupted due to a program or OS crash or hardware failure for instance. For the most part I would omit the deletion stage to further reduce the risk of losing the file (although some OS might not support this).


      Tim Bray makes some suggestions why you might not want to do that here, the caveat being of course how important is the data in question.

      Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://550838]
Approved by polettix
Front-paged by monkfan
[Eily]: you could tie a variable into not having the same value each time, if you like to make people who try to debug your code facepalm
[Corion]: perl -wle 'package o; use overload q("") => sub {warn "str"; ""}, bool => sub{warn "bool"; 1}; package main; my $o={}; bless $o => o; print "Yay" if ($o && !length($o))'
[Corion]: But people writing such code should document the objects they construct and why it makes sense for an object to be invisible as string while being true in a boolean context
[hippo]: That's equal parts clever and horrendous.
[Eily]: the overload version wouldn't return true with "$x" && !length $x though, I guess
[hippo]: The more I look at this code, the more $x is a plain old scalar and the more this condition will never be true. I'm calling it a bug at this point.
[hippo]: Thanks for your input which has soothed my sanity (a little)
[Corion]: Eily: Sure - if you force both things into stringy things, then you break that magic. But that would also mean that you changed the expression, as now $x = 0.00 will be true instead of false as it were before
[Corion]: Ah no, at least in my feeble experiments that doesn't change the meaning
[Corion]: We sell sanity in small packages ;)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2017-07-27 13:42 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (413 votes). Check out past polls.