I want to create a UniqueId. Is there a function I can call, such that every time when I use that it will give me a new Id, always with a different value?
-
like when ever i will call that subroutine it will give me a Unique Id – Sep 05 '13 at 05:16
-
cryptographic hash? Random number generator basically – Karthik T Sep 05 '13 at 05:16
-
what is that cryptographic hash ?? – Sep 05 '13 at 05:17
-
1Perhaps it would help to know what you need this for? – Karthik T Sep 05 '13 at 05:17
-
2@RedCricket he is not up to you , dont yell , but answer ..LOL – backtrack Sep 05 '13 at 05:19
9 Answers
Use Data::UUID to generate unique IDs:
use Data::UUID;
$ug = Data::UUID->new;
$uuid1 = $ug->create(); # Or create_str()

- 65,762
- 16
- 114
- 184

- 272,464
- 47
- 358
- 399
-
-
But please check https://github.com/rjbs/Data-UUID/issues/5 and other issues... and possibly decide for another UUID-generating module on CPAN. – Slaven Rezic Sep 04 '20 at 12:02
The better way you can use this also
use UUID::Generator::PurePerl;
sub create_search_id {
my $this =shift;
my $args=shift;
my $ug = UUID::Generator::PurePerl->new();
my $uuid1 = $ug->generate_v1();
return $uuid1;
}
From here you can learn about UUID::Generator::PurePerl

- 129,424
- 31
- 207
- 592

- 6,292
- 19
- 55
- 115
-
-
1
-
1What is the purpose of `$this` and `$args` in this code? If these variables are not used then no point to _manifest_ them. – Polar Bear May 13 '20 at 16:42
If you have some restriction on the modules you can use and you are running your script on Linux, you can use this workaround:
my $uuid = `cat /proc/sys/kernel/random/uuid`;
You don't need to install a new package on your system to use it.

- 1,942
- 3
- 23
- 43
-
2Slightly more typing, but it's definitively faster to open & read the file with perl functions than using an external cat, e.g. `$uuid = do { open my $fh, "/proc/sys/kernel/random/uuid" or die $!; scalar <$fh> };` – Slaven Rezic Sep 04 '20 at 12:13
Another alternative using Data::GUID
use Data::GUID;
my $guid = Data::GUID->new;
my $uniqueIdString = guid->as_string;
or
use Data::GUID;
my $uniqueIdString = Data::GUID->new->as_string;

- 1,244
- 14
- 19
Probably worth noting that on Windows machines, you could also use Win32:
use Win32;
my $guid = Win32::GuidGen();

- 2,198
- 1
- 23
- 26
I used Data::Uniqid , this module has 3 methods:
use Data::Uniqid qw ( suniqid uniqid luniqid );
$id = suniqid; #genrates a very short id valid only for the localhost and with a liftime of 1 day
$id = uniqid; #generates a short id valid on the local host
$id = luniqid; #generates a long id valid everywhere and ever

- 2,441
- 1
- 17
- 30
I also really liked the idea to use the Linux OS tool uuidgen
in this answer:
Version 5 UUID in Perl
On my Debian Linux system I have it at /usr/bin/uuidgen

- 137
- 1
- 4
Mix into set
array the characters you want be in your UUID
use warnings;
use feature 'say';
say "Generate passwords\n";
say genPass(16) for (0..10);
say '';
say "Generate UUIDs\n";
say genUUID() for (0..10);
sub genPass {
my $len = shift;
my @set = ('A'..'Z','a'..'z',0..9,split('','~!@#$%^&*()_+{}[]-=;:<>?"\''));
my $num = $#set;
my $uuid;
$uuid .= $set[rand($num)] while $len--;
return $uuid;
}
sub genUUID {
my $uuid;
my @set = ('a'..'z',0..9);
my $num = $#set;
$uuid .= $set[rand($num)] for 1..8;
$uuid .= '-';
for (1..3) {
$uuid .= $set[rand($num)] for 1..4;
$uuid .= '-';
}
$uuid .= $set[rand($num)] for 1..12;
return $uuid;
}
Sample of output
Generate passwords
yWXjV{{z0=6T-NVt
<ETH0(9IETcb[c!=
]cONV~*6PoTp1L~<
F8Ve>^u<Hymq]ZFd
<fdV(Z[~EUE?_Ufe
9A9y*iJ3Wta:jg*Y
7r2pP<1voI@x=cFe
Z7bZdha+U;:7aAeT
c2?a7S"mEpt%q43V
uC^q5]iPg~8E4CwJ
gwK+CBM?bnvgS@hg
Generate UUIDs
arrle2tc-x414-o8qi-i82f-obmwrbumlows
xmeg8ekk-ka2m-k4vo-l6xn-lor2s3z1lgn4
35d36bfr-jngc-6qiv-ubq8-nlysa2oaja12
m6yi4joo-1lpv-8obg-zjed-yesyqnq6rjxh
d3edwfbf-znwo-8s2o-4ld3-vntd20ps1fe5
twim8wr7-z0jj-0a20-uypt-sk80yubnaj8h
b4fkoodo-sp16-sa0h-2nlm-kicej06wlwn6
1r1rjy50-2wjo-620t-5ffn-5quw5qjztdfo
tr005kl0-k6yt-syz2-nrhq-k83ghybxjzm1
opxm1mjz-mvu8-feyf-if5o-411zayail60w
380v388j-acct-3zap-0nty-688b04eozrkx

- 6,762
- 1
- 5
- 12
Try this:
Firebase-style push id guid in Perl
It generates a guid in alphabetical order of the time it was generated. Useful if you want to sort record guids in the time order it was generated.

- 1,182
- 1
- 8
- 4