bryank has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to use a script that uses XML::Twig to modify specific tags in xml docs(I got the suggestion to use XML::Twig from another monk btw). The script does what I want, but in addition to updating the xml files, it also spits out a bunch of output as a single line:
I'm not sure why that is -- I thought the script was writing directly to the xml files I was editing? Anyway, if there are any tips on modifying my code to remove the stdout output, or tips on prettifying the data, I'd appreciate it. Here is my script:<merchant>BOB</merchant><merchant>Jane</merchant></testProduct><mercha +nt>BOB</merchant><merchant>Jane</merchant></testProduct><merchant>BOB +</merchant><merchant>Jane</merchant></testProduct><merchant>BOB</merc +hant><merchant>Jane</merchant></testProduct><merchant>BOB</merchant>< +merchant>Jane</merchant></testProduct><merchant>BOB</merchant><mercha +nt>Jane</merchant></testProduct><merchant>BOB</merchant><merchant>Jan +e</merchant></testProduct><merchant>BOB</merchant><merchant>Jane</mer +chant></testProduct>
#!/usr/bin/perl use strict; use warnings; use File::Find; use XML::Twig; @ARGV = ('.') unless @ARGV; my $dir = shift @ARGV; find(\&edits, $dir); sub edits() { my $seen = 0; my $file = $_; if ($file eq 'data.xml') { my $orig = 'data.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 => { 'merchant' => sub { my ( $t, $value ) = @_; { my $ra = $value->text; $ra =~ s/_/ /g; $value->set_text($ra); } $value->print($newfh); }, }, twig_print_outside_roots => $newfh, ); $t->parsefile($back); $t->flush; #don't forget undef $t; # close $newfh; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trying to disable/edit stdout output with XML::Twig
by kennethk (Abbot) on Jun 23, 2009 at 00:06 UTC | |
by bryank (Acolyte) on Jun 23, 2009 at 13:11 UTC |