http://qs321.pair.com?node_id=208932


in reply to Proper use of flock

Start out with use Fcntl qw(:flock); Right after you open a file for reading, do flock FH, LOCK_SH or die "Failed acquiring shared lock on $filename: $!"; If you want to write to a file, do flock FH, LOCK_EX or die "Failed acquiring ex. lock on $filename: $!";

DO NOT unlock the file, ever. Just close it. If you want to read from a file and then also write to it, then open it for reading and writing once, and get a shared lock. Once you want to write to it, upgrade to an exclusive lock by calling flock on the file handle again - do not close and reopen it. Don't downgrade that lock to a shared one either.

See perldoc -f flock for help on the function.

Makeshifts last the longest.