And while we're at it, there is no need to read in the entire file into memory if you're just going to iterate over the lines anyway. Read it in line by line:
open (DATA, "<.txt") or die "Can't open file $!\n";
while ($rec = <DATA>) {
# Loop stuff here
}
close(DATA)