I have a bit of a problem and I'm wondering if there is a better solution. I have a Perl program (of course) which reads STDIN and breaks it into
separate files based on a "report separator". I can construct an
appropriate file name based on information in the "trailing" separator. I.e. I don't know the name of the report until the report is complete. What I am doing at present is using the routine 'tempfile' in the
File::Temp package to create a unique file in the output directory. Once
I get the report name from the "trailer" separator, I want to rename this
file to the correct name. However, it is possible that there will be
multiple, different reports coming in which have the same name. So, what I
want to do is put a "sequence number" as the last node in the file name. This means that I must generate a test file name, then see if that file
already exists. If it does, increment the sequence number, generate a new
test file name and loop. Oh, did I mention that it is possible for this
program to have several copies all running at the same time, possibly
producing different reports with identical names? So I must worry about
"race" conditions.
If your code is the only code accessing the file, the standard way to handle this is to create a lock file. Try to write a file to a specific place as a lock file; if it fails, another process has already created it, so you poll until you can create it. Then run your routine to get a filename, move the file, and delete the lock file so that the next process can run.
Randy.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>