in reply to Replacing things in XML files

Yes, XML is 'human readable' (ie, plain old text), but I don't think it's a very robust solution to make changes to an XML document simply with a s/// operation skimming its way through the file. You have to weigh the risks of messing it up by under-engineering the solution against the difficulty and value of developing a properly engineered solution.

The good news is that there are some excellent XML parsing tools that virtually eliminate the "difficulty" of implementing a well-engineered solution. XML::Simple is one, but I prefer XML::Twig, because it handles some peculiar XML cases that make 'Simple choke, and because it still provides a very simple interface, similar to that of XML::Simple. With XML::Twig you can, in very few easy lines of code, grab the entire XML document (or portions thereof) and have it automagically parsed into a Perl datastructure. Then after modifying the specific elements within that datastructure that need to be changed, one or two more lines of code will dump that datastructure back into a properly formed XML document.

Because the XML::Twig approach is so easy to use, and because it represents a "properly engineered" solution, there is simply virtually no reason not to favor it over a less robust "regexp" solution.

The only problem I see is with regard to keeping indents and line breaks from the original file in the same exact places. XML::Twig will output clean XML, but it might not follow the existing formatting 100%. ...nor should it matter; XML, while human readable, is ultimately there for the machines to use. You can guarantee that an XML document output by XML::Twig will retain just as much human readability as the original, but you cannot guarantee that it will look 100% identical.


Dave