File Coverage

blib/lib/UUID/URandom.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 31 31 100.0


line stmt bran cond sub pod time code
1 1     1   73427 use 5.008;
  1         9  
2 1     1   6 use strict;
  1         2  
  1         29  
3 1     1   4 use warnings;
  1         2  
  1         63  
4              
5             package UUID::URandom;
6             # ABSTRACT: UUIDs based on /dev/urandom or the Windows Crypto API
7              
8             our $VERSION = '0.001';
9              
10 1     1   6 use Exporter 5.57 qw/import/;
  1         13  
  1         43  
11 1     1   449 use Crypt::URandom 0.36 ();
  1         5349  
  1         145  
12              
13             our @EXPORT_OK = qw(
14             create_uuid
15             create_uuid_hex
16             create_uuid_string
17             );
18              
19             #pod =func create_uuid
20             #pod
21             #pod my $uuid = create_uuid();
22             #pod
23             #pod # "\x95\x5a\xe4\x96\x8b\xb2\x45\x0b\x9c\x7e\x99\xf5\x01\xdf\x90\xfe"
24             #pod
25             #pod This returns a new UUID as a 16 byte 'binary' string.
26             #pod
27             #pod =cut
28              
29             sub create_uuid {
30 10003     10003 1 23953 my $uuid = Crypt::URandom::urandom(16);
31 10003         229311 vec( $uuid, 13, 4 ) = 0x4; # set UUID version
32 10003         16925 vec( $uuid, 35, 2 ) = 0x2; # set UUID variant
33 10003         27484 return $uuid;
34             }
35              
36             #pod =func create_uuid_hex
37             #pod
38             #pod my $uuid = create_uuid_hex();
39             #pod
40             #pod # "955ae4968bb2450b9c7e99f501df90fe"
41             #pod
42             #pod This returns a new UUID as a 32-byte hexadecimal string.
43             #pod
44             #pod =cut
45              
46             sub create_uuid_hex {
47 1     1 1 748 return unpack( "H*", create_uuid() );
48             }
49              
50             #pod =func create_uuid_string
51             #pod
52             #pod my $uuid = create_uuid_string();
53             #pod
54             #pod # "955ae496-8bb2-450b-9c7e-99f501df90fe"
55             #pod
56             #pod This returns a new UUID in the 36-byte RFC-4122 canonical string
57             #pod representation. (N.B. The canonical representation is lower-case.)
58             #pod
59             #pod =cut
60              
61             sub create_uuid_string {
62 1     1 1 1129 return join "-", unpack( "H8H4H4H4H12", create_uuid() );
63             }
64              
65             1;
66              
67              
68             # vim: ts=4 sts=4 sw=4 et tw=75:
69              
70             __END__