Category: Text Processing
Author/Contact Info /msg jeffa
Description: From time to time i have found myself wanting to capture the output from XML::Writer to a scalar instead of a file handle. It is simple enough, but i thought i would share here. Since XML::Writer expects an IO::Handle module for it's OUTPUT parameter, pass it an IO::Scalar. At the end, we can get a hold of that handle via XML::Writer::getOutput().
use strict;
use warnings;

use IO::Scalar;
use XML::Writer;

my $writer = XML::Writer->new(
    OUTPUT => IO::Scalar->new()
);

$writer->startTag("greeting", "class" => "simple");
$writer->characters("Hello, world!");
$writer->endTag("greeting");
$writer->end();

my $output = $writer->getOutput();

# optionally run $output through a filter

print $output;