J
John Smith
I want to ensure that only one person at a time can edit a file using my
perl script.
From what I've read, it appears that you can only flock a file after opening
it.
So I guess it would look like this:
- open the file for writing
- flock the file for exclusive (2)
- update the file
- unflock the file or flock (8)
- close the file
I'm suspecting that if the file is already open when you try to flock it,
the flock function will return an error code?
Now this would work great if you are trying to append to a file, as in a log
file, but what if you want to change info in the file? Maybe I'm not doing
this properly.
- My program opens (for read), reads the entire file, and closes the file.
- Then it open (for write), re-writes the complete file (with the updates),
and closes the file.
From what I understand, if I can't lock the file before opening it, then
this flock won't help me.
Now it turns out that my perl script will be the only program accessing
these files.
So I thought, that maybe I could have a dummy file that I would flock. If I
can flock this file, then I would allow my perl program to read, delete,
re-write or whatever to my other data files. Once I'm done, I unflock the
dummy file.
Another question... My perl script runs on a web server and when it is
called, it does a few quick things and terminates. When the perl script
terminates, will all files locked by the perl script be unlocked upon
termination (in the event that an error occured and it ended prematurely -
not that it would).
I think I was told that the web server is running perl 5.8???
This brings me to a final question.
Is there a function that can retrieve the version of Perl that is running?
This would be nice since the server is like 3 hours away (200 miles away!)
Thanks for all.
G.Doucet
perl script.
From what I've read, it appears that you can only flock a file after opening
it.
So I guess it would look like this:
- open the file for writing
- flock the file for exclusive (2)
- update the file
- unflock the file or flock (8)
- close the file
I'm suspecting that if the file is already open when you try to flock it,
the flock function will return an error code?
Now this would work great if you are trying to append to a file, as in a log
file, but what if you want to change info in the file? Maybe I'm not doing
this properly.
- My program opens (for read), reads the entire file, and closes the file.
- Then it open (for write), re-writes the complete file (with the updates),
and closes the file.
From what I understand, if I can't lock the file before opening it, then
this flock won't help me.
Now it turns out that my perl script will be the only program accessing
these files.
So I thought, that maybe I could have a dummy file that I would flock. If I
can flock this file, then I would allow my perl program to read, delete,
re-write or whatever to my other data files. Once I'm done, I unflock the
dummy file.
Another question... My perl script runs on a web server and when it is
called, it does a few quick things and terminates. When the perl script
terminates, will all files locked by the perl script be unlocked upon
termination (in the event that an error occured and it ended prematurely -
not that it would).
I think I was told that the web server is running perl 5.8???
This brings me to a final question.
Is there a function that can retrieve the version of Perl that is running?
This would be nice since the server is like 3 hours away (200 miles away!)
Thanks for all.
G.Doucet