Reputation: 67
I wanted to put a lock while one process is writing on the text file. so no other process can read or write.
Upvotes: 1
Views: 1231
Reputation: 118645
The flock
file locking mechanism in Perl is advisory. It can be used to exclude other processes from accessing a file if those other processes are also using flock. Even this mechanism will be flaky with some systems (I'm looking at you, NFS).
It may be more reliable to operate with an anonymous, temporary file that other processes will not know about, and to rename your file when you are done with it.
use File::Temp;
my ($fh, $obscure_filename) = tempfile();
print $fh "some data ...\n";
...
close $fh;
rename($obscure_filename, $the_real_name_of_the_file);
Upvotes: 4