How we can create a Unique Id in Perl

99封情书 提交于 2020-01-03 07:23:12

问题


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.




回答7:


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



来源:https://stackoverflow.com/questions/18628244/how-we-can-create-a-unique-id-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!