use XML::Twig::XPath; my $t = XML::Twig::XPath->new( twig_roots => { 'BalancerStatus/WebServer/OverallConnectionPoolStatus' => \§ion, #'BalancerStatus/WebServer/BalancerStatus/Server' => \§ion }); $t->parsefile( 'localhost.xml'); # sub section { my ($t, $section) = @_; my $a = $section->get_xpath( q{//OverallConnectionPoolStatus/FlowRater/Name/text()} ); $t->purge; }