# specify twig_handlers (see the XML::Twig documentation) twig_handlers => { # create a new hash-ref map { # transform a list into a new list # create a name / subroutine pair $_ => sub { # when the subroutine is called # get the element name from $_->name() # and increase it's count entry in %found # # note: $_ is set by XML::Twig when the sub is called $found{$_->name()}++ }; } @nodes # for each element in @nodes }