I want to create many small files with unique names from many processes in a directory. Theoretically, the file names may be the same, so how can I explain this? Which module should I use for this purpose? Files must be saved.
Add a timestamp for each file, up to a millisecond:
#!/usr/local/bin/perl use Time::HiRes qw(gettimeofday); my $timestamp = int (gettimeofday * 1000); print STDOUT "timestamp = $timestamp\n"; exit;
Output:
timestamp = 1227593060768
You might want to use File::Temp .
File::Temp
($fh, $filename) = tempfile($template, DIR => $your_dir, UNLINK => 0);
The package to use will be File :: Temp .
$$ contains the process ID of the running perl program. Therefore, it can be used in file names to make them unique, at least between instances of a running process.
Without any modules, you can use something like this: time() . "_" . rand() time() . "_" . rand()
time() . "_" . rand()