Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Using XML::Writer, how can I output to a file AND to the console?

by envinyater (Initiate)
on Dec 13, 2012 at 21:02 UTC ( #1008725=perlquestion: print w/ replies, xml ) Need Help??
envinyater has asked for the wisdom of the Perl Monks concerning the following question:

I am using XML::Writer since it doesn't have that many dependencies and I'm going to use fatpack to roll what I need into the script itself. Anyways, I am currently creating an XML file and the format is nice but I want to also display it to STDOUT. Here is how I declare my XML::Writer
my $output = IO::File->new(">$xmlFileName"); my $writer = new XML::Writer(OUTPUT => $output,DATA_MODE => 1, DATA_IN +DENT => 2 );
Now, if I remove the  OUTPUT => $output it prints everything to the console, formatted and nice -- which is what I want. As soon as I set the output, I lose that functionality. I can't see how I can get it back to the console. I've been googling for a while now but I would appreciate some help from this community, which has been AMAZING so far. Thanks :)

Comment on Using XML::Writer, how can I output to a file AND to the console?
Select or Download Code
Re: Using XML::Writer, how can I output to a file AND to the console?
by ww (Bishop) on Dec 13, 2012 at 21:44 UTC
Re: Using XML::Writer, how can I output to a file AND to the console?
by aitap (Chaplain) on Dec 13, 2012 at 22:37 UTC
Re: Using XML::Writer, how can I output to a file AND to the console?
by GrandFather (Cardinal) on Dec 13, 2012 at 22:38 UTC

    Open a file handle on a string:

    use strict; use warnings; use XML::Writer; my $outputStr; open my $output, '>', \$outputStr; my $writer = new XML::Writer(OUTPUT => $output); $writer->startTag("greeting", "class" => "simple"); $writer->characters("Hello, world!"); $writer->endTag("greeting"); $writer->end(); close $output; print $outputStr;

    Prints:

    <greeting class="simple">Hello, world!</greeting>

    Having captured the output to a string you can print where ever you want and as many times as you want.

    Update helps to copy and paste the right stuff!

    True laziness is hard work
      It works, Thank you :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008725]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-04-18 00:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls