Hi,

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:

<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>
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:

#!/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; } }

In reply to Trying to disable/edit stdout output with XML::Twig by bryank

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.