in reply to Trying to understand flock

Start by understanding http://en.wikipedia.org/wiki/File_locking.

(Consider rewriting the code to use three-argument open and variables instead of barewords as file handles.)

What really happens is that you voluntarily protect the code between flock S and close S. flock S will block (not return) until no other process has a lock on the semaphore file. The data file is not locked at all, and as long as all processes accessing the data file agree to lock the semaphore file instead, it does not have to be locked. Locking the semaphore file guarantees that only one process per machine can access the file.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)