in reply to file editing/writing

I think you write (the array) to the file and read it in (with undef $/) essentially to make a String from an Array. Use join instead.