in reply to flock problem

Get thee to a tutorial and please, please, please stop using
open FOO, ">file.txt";
when you intend to lock the file. This has just destroyed the contents of the file, regardless of you getting the lock. Always open the file for append or read/write, never just for write.

mikfire