package UUID; require 5.005063; # see MIN_PERL_VERSION in Makefile.PL use strict; use warnings; use Carp 'croak'; require Exporter; require DynaLoader; use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK $VERSION); @ISA = qw(DynaLoader); $VERSION = '0.39'; %EXPORT_TAGS = ( 'all' => [qw( &clear &compare © &generate &generate_random &generate_time &generate_v0 &generate_v1 &generate_v3 &generate_v4 &generate_v5 &generate_v6 &generate_v7 &is_null &parse &time &type &unparse &unparse_lower &unparse_upper &uuid &uuid0 &uuid1 &uuid3 &uuid4 &uuid5 &uuid6 &uuid7 &variant &version )], ); @EXPORT_OK = @{$EXPORT_TAGS{'all'}}; bootstrap UUID $VERSION; sub import { for (my $i=scalar(@_)-1 ; $i>0 ; --$i) { my $v = $_[$i]; chomp $v; # :persist=FOO if (length($v) > 8 and substr($v,0,8) eq ':persist') { my $arg = substr $v, 8; if (length($arg) < 2 or substr($arg, 0, 1) ne '=') { croak "Usage: :persist=FILE"; } my $file = substr $arg, 1; _persist($file); splice @_, $i, 1; next; } # :mac=random if (length($v) == 11 and $v eq ':mac=random') { _hide_mac(); splice @_, $i, 1; next; } # :mac=unique if (length($v) == 11 and $v eq ':mac=unique') { _hide_always(); splice @_, $i, 1; next; } # :defer[=N] if (length($v) >= 6 and substr($v,0,6) eq ':defer') { my $arg = substr $v, 6; my $len = length $arg; if ($len == 0) { $arg = '=0.001'; } elsif ($len == 1 or substr($arg, 0, 1) ne '=') { croak "Usage: :defer[=N]"; } my $val = substr $arg, 1; _defer($val); splice @_, $i, 1; next; } } goto &Exporter::import; } # Preloaded methods go here. 1; __END__ =head1 NAME UUID - Universally Unique Identifier library for Perl =head1 SYNOPSIS use UUID qw(uuid); # see EXPORTS my $str = uuid(); # generate version 4 UUID string $str = uuid1(); # new version 1 UUID string $str = uuid4(); # new version 4 UUID string $str = uuid6(); # new version 6 UUID string $str = uuid7(); # new version 7 UUID string UUID::generate_v1($bin); # new version 1 binary UUID UUID::generate_v4($bin); # new version 4 binary UUID UUID::generate_v6($bin); # new version 6 binary UUID UUID::generate_v7($bin); # new version 7 binary UUID UUID::parse($str, $bin); # map string to binary UUID UUID::unparse($bin, $str); # stringify $bin; prefer lowercase UUID::compare($bin1, $bin2); # compare binary UUIDs UUID::copy($dst, $src); # copy binary UUID from $src to $dst UUID::clear($bin); # set binary UUID to NULL UUID::is_null($bin); # compare binary UUID to NULL UUID::time($bin); # return UUID time UUID::type($bin); # return UUID type UUID::variant($bin); # return UUID variant UUID::version($bin); # return UUID version UUID::generate($bin); # alias for generate_v1() UUID::generate_time($bin); # alias for generate_v1() UUID::generate_random($bin); # alias for generate_v4() UUID::unparse_lower($bin, $str); # force lowercase stringify UUID::unparse_upper($bin, $str); # force uppercase stringify # NAMESPACE is 'dns', 'url', 'oid', or 'x500'; case-insensitive. $str = uuid3(dns => 'www.example.com'); $str = uuid5(url => 'https://www.example.com/foo.html'); UUID::generate_v3($bin, dns => 'www.example.com'); UUID::generate_v5($bin, url => 'https://www.example.com/foo.txt'); =head1 DESCRIPTION The UUID library is used to generate unique identifiers for objects that may be accessible beyond the local system. For instance, they could be used to generate unique HTTP cookies across multiple web servers without communication between the servers, and without fear of a name clash. The generated UUIDs can be reasonably expected to be unique within a system, and unique across all systems, and are compatible with those created by the Open Software Foundation (OSF) Distributed Computing Environment (DCE). All generated UUIDs are either version 1, 3, 4, 5, 6, or version 7. And all are variant 1, meaning compliant with the OSF DCE standard as described in RFC4122. Versions 6 and 7 are not yet standardized. They are presented here as proposed in RFC4122bis, version 14, and may change in the future. RFC4122bis is noted to replace RFC4122, if approved. =head1 FUNCTIONS Most of the UUID functions expose the historically underlying I C interface rather directly. That is, many return their values in their parameters and nothing else. Not very Perlish, but it's been like that for a long time so not likely to change any time soon. All take or return UUIDs in either binary or string format. The string format resembles the following: 21b081a3-de83-4480-a14f-e89a1dcf8f0f Or, in terms of printf(3) format: "%08x-%04x-%04x-%04x-%012x" The binary form is simply a packed 16 byte binary value. =head2 B I<$uuid> B<)> Sets binary I<$uuid> equal to the value of the NULL UUID. =head2 B I<$uuid1>B<,> I<$uuid2> B<)> Compares two binary UUIDs. Returns an integer less than, equal to, or greater than zero if I<$uuid1> is less than, equal to, or greater than I<$uuid2>. If one is defined and the other not, the defined value is deemed the larger. If either operand is not a binary UUID, falls back to a simple string comparison returning similar values. =head2 B I<$dst>B<,> I<$src> B<)> Copies the binary I<$src> UUID to I<$dst>. If I<$src> isn't a UUID, I<$dst> is set to the NULL UUID. =head2 B I<$uuid> B<)> Alias for B. Prior to version 0.33, this function provided either a binary version 4 UUID or fell back to version 1 in some cases. This is no longer the case. The fallback feature was removed with the addition of an onboard crypto-strength number generator. =head2 B I<$uuid> B<)> Alias for B. =head2 B I<$uuid> B<)> Alias for B. =head2 B I<$uuid> B<)> Generates a new version 1 binary UUID using the current time and the local ethernet MAC address, if available. If the MAC address is not available at startup, or a randomized address is requested (see B<:mac> in B), a random address is used. The multicast bit of this address is set to avoid conflict with addresses returned from network cards. =head2 B I<$uuid>, I =E I B<)> Generate a new version 3 binary UUID using the given namespace and name hashed through the MD5 algorithm. Namespace is one of "dns", "url", "oid", or "x500", and case-insensitive. It is used to select the namespace UUID to hash with the name. Name should be an entity from the given namespace, but can really be any text. =head2 B I<$uuid> B<)> Generates a new version 4 binary UUID using mostly random data. There are 6 bits used for the UUID format, leaving 122 bits for randomness. =head2 B I<$uuid>, I =E I B<)> Generate a new version 5 binary UUID using the given namespace and name hashed through the SHA1 algorithm. Namespace is one of "dns", "url", "oid", or "x500", and case-insensitive. It is used to select the namespace UUID to hash with the name. Name should be an entity from the given namespace, but can really be any text. =head2 B I<$uuid> B<)> Generates a new version 6 binary UUID using the current time and the local ethernet MAC address, if available. If the MAC address is not available at startup, or a randomized address is requested (see B<:mac> in B), a random address is used. The multicast bit of this address is set to avoid conflict with addresses returned from network cards. Version 6 is the same as version 1, with reversed time fields to make it more database friendly. =head2 B I<$uuid> B<)> Generates a new version 7 binary UUID using the current time and random data. There are 6 bits used for the UUID format and 48 bits for timestamp, leaving 74 bits for randomness. Version 7 is the same as version 6, in that it uses reversed timestamp fields, but also uses a Unix epoch time base instead of Gregorian. =head2 B I<$uuid> B<)> Compares the value of I<$uuid> to the NULL UUID. Returns 1 if NULL, and 0 otherwise. =head2 B I<$string>B<,> I<$uuid> B<)> Converts the string format UUID in I<$string> to binary and returns in I<$uuid>. The previous content of I<$uuid>, if any, is lost. Returns 0 on success and -1 on failure. Additionally on failure, the content of I<$uuid> is unchanged. =head2 B I<$uuid> B<)> Returns the time element of a binary UUID in seconds since the epoch, the same as I's B