It looks like there is a problem with the module, either with the code or with the docs. For such problems with uncommon XML modules you should try asking on the perl-xml mailing list (Kip usually reads it). If you don't get an answer you can then try contacting the author and/or reporting the bug through rt.cpan.org.
In this case I would probably use a different module though. See below for a bunch of possibilities (there are many more, from XML::LibXML to XML::SAX::Writer):
#!/usr/bin/perl -w
use strict;
use Test::More qw(no_plan);
use XML::SemanticDiff;
use File::Slurp;
use Getopt::Std;
my %opt;
# -v is verbose, it displays the generated string for each test
getopts( 'v', \%opt);
# XML::SemanticDiff seems to only compare files, not strings
my $tmp_expected= "tmp.expected"; # temp file with expected result
my $tmp_result = "tmp.result"; # temp file with result
my %tests=( 'XML::Simple' => { sub => \&test_xml_simple, },
'XML::Writer' => { extra_modules => [ 'IO::Scalar' ],
sub => \&test_xml_writer,
},
'XML::Handler::YAWriter'
=> { sub => \&test_xml_handler_yawriter,
+ },
'XML::Twig' => { sub => \&test_xml_twig, },
);
write_file( $tmp_expected, <DATA>);
my $sem_diff= XML::SemanticDiff->new;
foreach my $module ( keys %tests)
{ my $test= $tests{$module};
my @modules= ($module);
push @modules, @{$test->{extra_modules}} if( $test->{extra_modules
+});
if( load_modules( @modules))
{ my $result= $test->{sub}->();
write_file( $tmp_result, $result);
my $diff= $sem_diff->compare( $tmp_expected, $tmp_result);
ok( ! $diff, $module) or diag( "got $result") ;
diag( "\n", $result, "\n\n") if( $opt{v});
}
else
{ SKIP: { skip( "$module not available", 1); }
}
}
unlink $tmp_expected, $tmp_result;
sub test_xml_simple
{ my $xml= { entry => { att1 => 'value1', att2 => 'value2', path =>
+{ content => 'path & text'} } };
return XMLout( $xml, keeproot => 1);
}
sub test_xml_writer
{ my $result;
my $output = IO::Scalar->new( \$result);
my $writer = XML::Writer->new(OUTPUT => $output);
$writer->startTag( entry => att1 => 'value1', att2 => 'value2');
$writer->dataElement( path => 'path & text');
$writer->endTag( 'entry');
$writer->end();
$output->close();
return $result;
}
sub test_xml_handler_yawriter
{ my $writer= XML::Handler::YAWriter->new( AsString => 1);
$writer->start_document;
$writer->start_element( { Name => 'entry', Attributes => { att1 =>
+ 'value1', att2 => 'value2'} } );
$writer->start_element( { Name => 'path' } );
$writer->characters( { Data => 'path & text' } );
$writer->end_element( { Name => 'path'} );
$writer->end_element( { Name => 'entry'} );
return $writer->end_document;
}
sub test_xml_twig
{ my $root= XML::Twig::Elt->new( entry => { att1 => 'value1', att2 =
+> 'value2' });
$root->insert_new_elt( path => "path & text");
return $root->sprint;
}
# a simple way of importing modules at run time
#sub load_modules
# { foreach (@_)
# { if(eval "require $_") { import $_; }
# else { return 0; }
# }
# return 1;
# }
# fun way
sub load_modules
{ ((eval "require $_" and import $_ or 1) or return 0) foreach (@_)
+; 1}
__DATA__
<entry att1="value1" att2="value2">
<!-- the & is here to test escaping -->
<path>path & text</path>
</entry>