use vars qw($change $alter); $alter = 0; sub readfile { while() { if ( $_ =~ /^\[ARTEC\]$/ ) { $alter = 1; exit print "found it exiting no need to change something"; }else { $change = 1; print "n "; } } return; } } if ($alter == 0 and $change == 1) { #do something ... print "changed\n"; }