in reply to lack an adequate understanding of (at least) the seek function
jeffa has a great solution, but just for a learning experience,
1. flock takes a file handle as its first argument, not a file name. You cannot lock before you open
2. When you open (TOFILE, ">>messageTest.txt");, you are opening with O_APPEND, which means, as the POSIX standard says, "the file offset shall be set to the end of the file prior to each write". So your seek will be in vain. Theoretically you can still read from other positions if you open it "+>>", although I just got a freaky result when I tried it.