use XML::Twig;
use IO::Tee;
use feature 'say';
open my $frufile, '>', 'fruit.xml' or die "fruit $!";
open my $vegfile, '>', 'veg.xml' or die "veg $!";
my $tee = IO::Tee->new($frufile, $vegfile);
select $tee;
my $twig=XML::Twig->new(
twig_handlers => {
thing => \&magic,
_default_ => sub {
say STDOUT '_default_ for '.$_->name;
$_[0]->flush($tee); #default filehandle = tee
1;
},
},
pretty_print => 'indented',
empty_tags => 'normal',
);
$twig->parse( *DATA );
sub magic {
my ($thing, $element) = @_;
say STDOUT "magic for ". $element->{att}{type};
for ($element->{att}{type}) {
if (/fruit/) {
$thing->flush($frufile);
} elsif (/vegetable/) {
$thing->flush($vegfile);
} else {
$thing->purge;
}
}
1;
}
__DATA__
Im an apple!
Toronto
Im a carrot!
Melrose
Im a potato!
Im a pear!
Im a pickle!
Patna
Im a banana!
Im an eggplant!
Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
A
B
####
Im an apple!
Im a pear!
Im a banana!
A
B
####
Im a carrot!
Im a potato!
Im a pickle!
Im an eggplant!
A
B