in reply to loops & sql results storage

Not related to your original question, but using flock may not be necessary in this situation. Under Unix, appends are atomic when the file is opened in append mode. This is at least true at the level of the write system call (i.e. syswrite in perl.)

So, if you open the file in append mode and:

  1. you use buffered I/O but only make one small write (small = smaller than the file buffer size), or
  2. you turn on autoflush, or
  3. you use syswrite
then you don't need to use a mutex to prevent interleaving of output with other processes.

Unfortunately this only applies to Unix systems (and then only to local file systems - appends are not guaranteed to be atomic under NFS.) Under Win32 I've read that appending writes are not guaranteed to be atomic.