http://www.perlmonks.org?node_id=857020

rpike has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to update a file - so read it, insert data into appropriate places within the contents retrieved, and then write the file back. I also need to make sure no other user can open the file and read and/or write to it while the process is taking place for another user running it. What are the exact steps needed to take in order to accomplish this? For simplicity sake let's say I'm reading a file of persons' names and I want to enter a name and insert it in the proper alphabetic location within the file (a name per line for instance). I don't need to know what code is needed to insert the name, jsut what steps can I take to read and write back the contents with a lock from read up to write (if possible). Thanks for any help in advance. AggressiveFish