File: Uniqid.pm

package info (click to toggle)
libdata-uniqid-perl 0.12-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 80 kB
  • sloc: perl: 108; makefile: 2
file content (110 lines) | stat: -rw-r--r-- 2,225 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package Data::Uniqid;

use 5.006;
use strict;
use warnings;

require Exporter;
use AutoLoader qw(AUTOLOAD);

our @ISA = qw(Exporter);

our %EXPORT_TAGS = ( 'all' => [ qw(
  suniqid  
  uniqid  
  luniqid  
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
  
);
our $VERSION = '0.12';

use Math::BigInt;
use Sys::Hostname;
use Time::HiRes qw( gettimeofday usleep );

sub base62() { ################################################### Base62 #####
  my($s)=@_;
  my(@c)=('0'..'9','a'..'z','A'..'Z');
  my(@p,$u,$v,$i,$n);
  my($m)=20;
  $p[0]=1;  
  for $i (1..$m) {
    $p[$i]=Math::BigInt->new($p[$i-1]);
    $p[$i]=$p[$i]->bmul(62);
  }

  $v=Math::BigInt->new($s);
  for ($i=$m;$i>=0;$i--) {
    $v=Math::BigInt->new($v);
    ($n,$v)=$v->bdiv($p[$i]);
    $u.=$c[$n];
  }
  $u=~s/^0+//;
  
  return($u);
}

sub suniqid { ########################################### get unique id #####
  my($s,$us)=gettimeofday();usleep(1);
  my($v)=sprintf("%06d%05d%06d",$us,substr($s,-5),$$);
  return(&base62($v));
}

sub uniqid { ########################################### get unique id #####
  my($s,$us)=gettimeofday();usleep(1);
  my($v)=sprintf("%06d%010d%06d",$us,$s,$$);
  return(&base62($v));
}

sub luniqid { ############################################ get unique id #####
  my($s,$us)=gettimeofday();usleep(1);
  my($ia,$ib,$ic,$id)=unpack("C4", (gethostbyname(hostname()))[4]);
  my($v)=sprintf("%06d%10d%06d%03d%03d%03d%03d",$us,$s,$$,$ia,$ib,$ic,$id);
	return(&base62($v));
}

1;
__END__

=head1 NAME

Data::Uniqid - Perl extension for simple genrating of unique id's

=head1 SYNOPSIS

  use Data::Uniqid qw ( suniqid uniqid luniqid );
  
  $id = suniqid;
  $id = uniqid;
  $id = luniqid;

=head1 DESCRIPTION

Data::Uniqid provides three simple routines for generating unique ids.
These ids are coded with a Base62 systen to make them short and handy
(e.g. to use it as part of a URL).

  suinqid
    genrates a very short id valid only for the localhost and with a 
    liftime of 1 day
  
  uniqid
    generates a short id valid on the local host 

  luniqid 
    generates a long id valid everywhere and ever


=head1 AUTHOR

Mike Wesemann, <mwx@gmx.de>

=head1 SEE ALSO

L<perl>.

=cut