Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a unique filename in perl

I want to create in a directory many small files with unique names from many processes. Theoretically filenames may be the same, so how do I account for that? What module I should use for this purpose? Files should be persist.

like image 470
Bdfy Avatar asked Oct 01 '11 20:10

Bdfy


4 Answers

You probably want to use File::Temp for that.

($fh, $filename) = tempfile($template, DIR => $your_dir, UNLINK => 0);
like image 195
cnicutar Avatar answered Nov 20 '22 22:11

cnicutar


The package to use would be File::Temp.

like image 39
Jens Avatar answered Nov 20 '22 23:11

Jens


Append a timestamp to each file, up to the millisecond:

#!/usr/local/bin/perl
use Time::HiRes qw(gettimeofday);

my $timestamp = int (gettimeofday * 1000);
print STDOUT "timestamp = $timestamp\n";
exit;

Output:

timestamp = 1227593060768
like image 5
rwyland Avatar answered Nov 20 '22 23:11

rwyland


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

like image 4
DavidEG Avatar answered Nov 21 '22 00:11

DavidEG