24

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?

tripleee
  • 175,061
  • 34
  • 275
  • 318

9 Answers9

28

Use Data::UUID to generate unique IDs:

use Data::UUID;

$ug    = Data::UUID->new;
$uuid1 = $ug->create();   # Or create_str()
friedo
  • 65,762
  • 16
  • 114
  • 184
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
9

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

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Developer
  • 6,292
  • 19
  • 55
  • 115
7

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.

Pierre
  • 1,942
  • 3
  • 23
  • 43
  • 2
    Slightly 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
6

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;
krispy
  • 1,244
  • 14
  • 19
6

Probably worth noting that on Windows machines, you could also use Win32:

use Win32;    
my $guid = Win32::GuidGen();
ianbeks
  • 2,198
  • 1
  • 23
  • 26
1

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
Suic
  • 2,441
  • 1
  • 17
  • 30
1

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

0

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
Polar Bear
  • 6,762
  • 1
  • 5
  • 12
-1

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.

Rob de la Cruz
  • 1,182
  • 1
  • 8
  • 4