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 { print '_default_ for '.$_->name." [[["; $_[0]->flush($tee); #default filehandle = tee say "]]]"; 1; }, }, pretty_print => 'none', empty_tags => 'normal', ); $twig->parse( *DATA ); sub magic { my ($thing, $element) = @_; print "magic for ". $element->{att}{type}." [[["; for ($element->{att}{type}) { if (/fruit/) { $thing->flush($frufile); } elsif (/vegetable/) { $thing->flush($vegfile); } else { $thing->purge; } } say "]]]"; 1; } __DATA__
1 2 3
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