One thing that may be missing is that after writing to DATA you have to go back to the beginning of the file before you can start reading from it. Insert this in your second sub:
seek(DATA, 0, 0);
However, this may be overly complicated. Perhaps you could just store the data in an array and pass that around instead of writing to a file.