use IO::File; .... $IPcheck = 0; { my $file = new IO::File "$temp", "r"; while (<$file>) { chomp; # operates on the $_ variable directly if ($_ eq $ipaddress) { $IPcheck = 1; last; } } } # note that you do not have to close the $file handle, falling # out of the scope and perl will automatically close the file.