How we can create a Unique Id in Perl

2019-06-15 04:10发布

问题:

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?

回答1:

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 Link



回答2:

Use Data::UUID to generate unique IDs:

use Data::UUID;

$ug    = Data::UUID->new;
$uuid1 = $ug->create();   # Or create_str()


回答3:

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

use Win32;    
my $guid = Win32::GuidGen();


回答4:

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;


回答5:

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


回答6:

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.