use strict; use XML::Twig; my @nodes = qw(title toc bar); my %found; my $t = XML::Twig->new( twig_handlers => { map { $_ => sub { $found{$_->name()}++ }; } @nodes } ); $t->parsefile(shift()); # read file specified at command line $t->flush; # update: you may need this for (@nodes) { die "$_ not found" unless $found{$_}; }