my $twig= XML::Twig->new( twig_roots => { Hit_num => \&hit_num } ); $twig->parsefile( $file); sub hit_num { my( $t, $hit_num)= @_; # do stuff with the hit_num # $t->purge will free the memory if you don't need # to keep the hit_num around }