in reply to More Eyes, Please

The original example uses:
open(DBA, "+< $datafile") ...
but the next 2 code examples use:
open(TMPA, "+> $tempfile") ...
Note the direction of the mode indicator - in the first example, you are opening the file for read/write access, or, to be specific, you are opening the file for input, and want read/write access. In the next examples, you are opening it for output (and clobbering the contents in the process), and want read/write access. The second is not very useful: if you have already opened it for writing, and just set it to zero length, there is nothing to read! If you are using flock, you want to use the first example, so that any changes made to the file (like, say, truncating it to 0 bytes) is made *after* you have sucessfully flocked the file. You could possibly use the second in cases where you do not care about what is in the file, but merely about whether it is locked or not (e.g. a semaphore).