Well, you're using $_ as the loop variable in two nested loops. That's bound to cause trouble. Use an explicit loop variable in at least one of the loops and the problem should go away.
Also, why the nested loop in the first place? Normally it makes no sense to read everything from DATA more than once.