in reply to reading from and writing to a file
In various places in your code you open a filehandle with ">>", i.e. you open the file for appending, then in the next line you try to read from the file. Which won't work, as the filehandle is positioned at the end of the file (naturally, since you wanted to append).
If you want to read from the file and then append, open it first for reading, read then close it and open it again for appending
|
|---|