devxlogo

Write to a Perl File Using Flock

Write to a Perl File Using Flock

In his tip “Locking a File With the Perl Flock Function,” Steve Renaker’s example opens a file without specifying read or write. While this will successfully open a file for reading, the context of this example (indeed the very reason for using flock in the first place) involves writing to a file:

open (FILE, ">data.txt");flock (FILE, 2);

Now, the file is available for writing. In the original example, the file was indeed locked so no other script could write to it, but the example code would not be able to write to it either.

devx-admin

Share the Post: