Why do I get my string two times in the output?
#!/usr/bin/perl
use warnings;
use strict;
use XML::Twig;
my $string = '<cd_catalogue><title>Hello, World!</title></cd_catalogue>';
my $t= XML::Twig->new( twig_handlers => { cd_catalogue => \&cd_catalogue, },
pretty_print => 'indented',
);
$t->parse( $string );
sub cd_catalogue {
my( $t, $cd_catalogue ) = @_;
$cd_catalogue->flush;
}
# Output:
#<cd_catalogue>
# <title>Hello, World!</title>
#</cd_catalogue>
#<cd_catalogue>
# <title>Hello, World!</title>
#</cd_catalogue>
Changing your sub to use
printandpurgeinstead offlushgets around problem:The
flushis getting confused because of the simplicity of your example becausecd_catalogueis root node. If you change your data to something like this:or if you changed your twig_handler to look for
title:then you will find that
$cd_catalogue->flushnow works as expected with your$string./I3az/