Your code opens up a new output file for every line in all of the input files. Is this what you're intending?
A couple of general comments:
If something doesn't work as expected, let the code print out messages which indicate what's going on. In your case, put print statements into the foreach and while loops showing files and data processed.
Use use strict; use warnings to enable perl to help you track down typos.