简体   繁体   English

我们如何在 Perl 中创建唯一 ID

[英]How we can create a Unique Id in Perl

I want to create a UniqueId .我想创建一个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?有没有我可以调用的函数,这样每次使用时它都会给我一个新的 Id,总是具有不同的值?

Use Data::UUID to generate unique IDs:使用Data::UUID生成唯一 ID:

use Data::UUID;

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

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从这里你可以了解UUID::Generator::PurePerl

Another alternative using Data::GUID使用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;

Probably worth noting that on Windows machines, you could also use Win32:可能值得注意的是,在 Windows 机器上,您也可以使用 Win32:

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

If you have some restriction on the modules you can use and you are running your script on Linux, you can use this workaround:如果您对可以使用的模块有一些限制,并且您在 Linux 上运行脚本,则可以使用以下解决方法:

my $uuid = `cat /proc/sys/kernel/random/uuid`;

You don't need to install a new package on your system to use it.您无需在系统上安装新软件包即可使用它。

I used Data::Uniqid , this module has 3 methods:我使用了Data::Uniqid ,这个模块有 3 个方法:

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

I also really liked the idea to use the Linux OS tool uuidgen in this answer: Version 5 UUID in Perl我也非常喜欢在这个答案中使用 Linux OS 工具uuidgen的想法: Perl 中的 Version 5 UUID

On my Debian Linux system I have it at /usr/bin/uuidgen在我的 Debian Linux 系统上,它位于/usr/bin/uuidgen

Try this:试试这个:

Firebase-style push id guid in Perl Perl 中的 Firebase 式推送 ID guid

It generates a guid in alphabetical order of the time it was generated.它按生成时间的字母顺序生成一个 guid。 Useful if you want to sort record guids in the time order it was generated.如果您想按照生成的时间顺序对记录 guid 进行排序,则很有用。

Mix into set array the characters you want be in your UUID将您想要在UUID的字符混合到set数组中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM