use warnings; use strict; use XML::Twig; my $children = 4697; my $found; my $xmlStr = ''.join("\n",@{['1' x $children]}).''; my $twig = XML::Twig->new( keep_encoding => 1, twig_handlers => {'lemma' => \&ProcessLemma} ); $twig->parse($xmlStr); print "Expected one, found $found\n"; sub ProcessLemma { my ($XmlTwig, $XmlLemma) = @_; ++$found; $XmlLemma->purge; return 1; }