in reply to Automatically creating incremental file names

Don't use the PID for things like this. It's not as random as you think it is (OpenBSD is the only exception that I know of).

You could try loading all your data into a scalar, and then running an SHA1 hash on that data. The base64 value of the hash will be your filename. Just in case the impossible happens, check for the existance of that file and append some data to it each time through the loop, such as:

$filename .= '0' while(-e $filename);

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Automatically creating incremental file names
by graff (Chancellor) on May 28, 2003 at 02:30 UTC
    Actually, if the data for a current run produces the same SHA1 hash string (or the same MD5 signature, if one prefers) as some other file that already exists, wouldn't that mean that the existing file already contains exactly the same data as the current run? In that case, you have a built-in means to decide when a new file doesn't need to be written to disk (because the data in question are already there).

      . . . wouldn't that mean that the existing file already contains exactly the same data as the current run?

      For SHA1, there is a 1 in 2**160 chance that it isn't the same data, but produces the same hash. Actually, because of the Birthday Paradox (external link), it's more like 1 in 2**80.

      This chance is so tiny that you could ignore it and probably get away with it, even if your program was running until the sun burned out. Even so, I like to be certain if at all possible, which is why I had that while loop in my post above.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated