How to get a unique file name in perl

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.

+4
source share
5 answers

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 
+5
source

You might want to use File::Temp .

 ($fh, $filename) = tempfile($template, DIR => $your_dir, UNLINK => 0); 
+13
source

The package to use will be File :: Temp .

+5
source

$$ 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.

+2
source

Without any modules, you can use something like this: time() . "_" . rand() time() . "_" . rand()

+2
source

All Articles