#!/usr/local/bin/perl use XML::Twig; use Net::Telnet(); $db = "hrdm2"; $comm = "stop-$db.sh"; $cachedel = "No"; $done = 0; my $s= new XML::Twig( TwigHandlers=> { Product => \&product}); $s->parsefile( 'testservers2.xml'); exit; sub product { my ($s, $product)= @_; return if $done; my %product; # In all your if/elsif statements add $done = 1; # Rest of your code }