in reply to Trying to read a filehandle inside a loop

The foreach statement causes <DATA> to be evaluated in list context, hence all data is read at once. Subsequent reads obviously fail by lack of data.

This is easy to see by replacing the foreach $line (<DATA>) { by while ($line = <DATA>) { which does what you expected.

hope this helps, -gjb-