longliveAJ has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,
I am trying to use XML::Writer. I would like the output to goto a string instead of a output file

As per cpan documentation

OUTPUT
An object blessed into IO::Handle or one of its subclasses (such as IO::File), or a reference to a string; if this parameter is not present, the module will write to standard output. If a string reference is passed, it will capture the generated XML (as a string; to get bytes use the Encode module).
my $writer = new XML::Writer(OUTPUT => $output);

However when I declare
$output = \"";

I get an error.
Anyone any idea.

2006-02-15 Retitled by Arunbear, as per Monastery guidelines
Original title: 'XML::Writer'

Replies are listed 'Best First'.
Re: Writing output to a scalar with XML::Writer
by holli (Abbot) on Feb 15, 2006 at 19:52 UTC
    you need
    my $writer = new XML::Writer(OUTPUT => \$output);
    which passes a reference to the constructor.
    $output = \"";
    creates a reference to a literal string. So that won't work. Btw, next time tell us the error message. Because the error message is god.


    holli, /regexed monk/
      Holli, that doesnt work. Still get the following error message
      Can't call method "print" on unblessed reference at /usr/opt/perl/5.8. +5/lib/XML/Writer.pm line 116. at /usr/opt/perl/5.8.5/lib/XML/Writer.pm line 116 XML::Writer::__ANON__[/usr/opt/perl/5.8.5/lib/XML/Writer.pm:12 +5] called at /usr/opt/perl/5.8.5/lib/XML/Writer.pm line 133 XML::Writer::__ANON__[/usr/opt/perl/5.8.5/lib/XML/Writer.pm:13 +5] called at /usr/opt/perl/5.8.5/lib/XML/Writer.pm line 476 XML::Writer::xmlDecl('XML::Writer=HASH(0xd6dbe0)') called at g +en_activity.pl line 150 main::gen_blob('HASH(0xd2edac)') called at gen_activity.pl lin +e 115

      Below is my code
      my $output; # XML Writers object 147 my $xmlObj = new XML::Writer(OUTPUT=> \$output, NEWLINES=>0) +; 148 149 # Start XML file with the line <xml version = "1.0 150 $xmlObj->xmlDecl();

        What does print $XML::Writer::VERSION print?
        What does print ref \$output print?