File Coverage

blib/lib/Data/Uniqid.pm
Criterion Covered Total %
statement 49 49 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod 0 4 0.0
total 60 64 93.7


line stmt bran cond sub pod time code
1             package Data::Uniqid;
2              
3 1     1   19866 use 5.006;
  1         3  
  1         41  
4 1     1   6 use strict;
  1         1  
  1         237  
5 1     1   7 use warnings;
  1         6  
  1         57  
6              
7             require Exporter;
8 1     1   1063 use AutoLoader qw(AUTOLOAD);
  1         2027  
  1         7  
9              
10             our @ISA = qw(Exporter);
11              
12             our %EXPORT_TAGS = ( 'all' => [ qw(
13             suniqid
14             uniqid
15             luniqid
16             ) ] );
17              
18             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
19              
20             our @EXPORT = qw(
21            
22             );
23             our $VERSION = '0.12';
24              
25 1     1   29979 use Math::BigInt;
  1         53926  
  1         7  
26 1     1   26709 use Sys::Hostname;
  1         1567  
  1         74  
27 1     1   1032 use Time::HiRes qw( gettimeofday usleep );
  1         2168  
  1         8  
28              
29             sub base62() { ################################################### Base62 #####
30 306     306 0 523 my($s)=@_;
31 306         4673 my(@c)=('0'..'9','a'..'z','A'..'Z');
32 306         440 my(@p,$u,$v,$i,$n);
33 306         459 my($m)=20;
34 306         536 $p[0]=1;
35 306         673 for $i (1..$m) {
36 6120         673016 $p[$i]=Math::BigInt->new($p[$i-1]);
37 6120         141448 $p[$i]=$p[$i]->bmul(62);
38             }
39              
40 306         32781 $v=Math::BigInt->new($s);
41 306         12899 for ($i=$m;$i>=0;$i--) {
42 6426         113114 $v=Math::BigInt->new($v);
43 6426         147905 ($n,$v)=$v->bdiv($p[$i]);
44 6426         953892 $u.=$c[$n];
45             }
46 306         5537 $u=~s/^0+//;
47            
48 306         6399 return($u);
49             }
50              
51             sub suniqid { ########################################### get unique id #####
52 102     102 0 1154 my($s,$us)=gettimeofday();usleep(1);
  102         61508  
53 102         1250 my($v)=sprintf("%06d%05d%06d",$us,substr($s,-5),$$);
54 102         360 return(&base62($v));
55             }
56              
57             sub uniqid { ########################################### get unique id #####
58 102     102 0 1252 my($s,$us)=gettimeofday();usleep(1);
  102         13415  
59 102         833 my($v)=sprintf("%06d%010d%06d",$us,$s,$$);
60 102         293 return(&base62($v));
61             }
62              
63             sub luniqid { ############################################ get unique id #####
64 102     102 0 1135 my($s,$us)=gettimeofday();usleep(1);
  102         11078  
65 102         489 my($ia,$ib,$ic,$id)=unpack("C4", (gethostbyname(hostname()))[4]);
66 102         13714 my($v)=sprintf("%06d%10d%06d%03d%03d%03d%03d",$us,$s,$$,$ia,$ib,$ic,$id);
67 102         263 return(&base62($v));
68             }
69              
70             1;
71             __END__
72              
73             =head1 NAME
74              
75             Data::Uniqid - Perl extension for simple genrating of unique id's
76              
77             =head1 SYNOPSIS
78              
79             use Data::Uniqid qw ( suniqid uniqid luniqid );
80            
81             $id = suniqid;
82             $id = uniqid;
83             $id = luniqid;
84              
85             =head1 DESCRIPTION
86              
87             Data::Uniqid provides three simple routines for generating unique ids.
88             These ids are coded with a Base62 systen to make them short and handy
89             (e.g. to use it as part of a URL).
90              
91             suinqid
92             genrates a very short id valid only for the localhost and with a
93             liftime of 1 day
94            
95             uniqid
96             generates a short id valid on the local host
97              
98             luniqid
99             generates a long id valid everywhere and ever
100              
101              
102             =head1 AUTHOR
103              
104             Mike Wesemann, <mwx@gmx.de>
105              
106             =head1 SEE ALSO
107              
108             L<perl>.
109              
110             =cut