NAME
UUID::Tiny - Pure Perl UUID Support With Functional Interface
VERSION
Version 1.00
SYNOPSIS
Create version 1, 3, 4 and 5 UUIDs:
use UUID::Tiny;
my $v1-mc_UUID = create_UUID();
my $v3-md5_UUID = create_UUID(UUID_V3, $str);
my $v3-md5_UUID = create_UUID(UUID_V3, UUID_NS_DNS, 'caugustin.de');
my $v4-rand_UUID = create_UUID(UUID_V4);
my $v5-sha1_UUID = create_UUID(UUID_V5, $str);
my $v5_with_NS_UUID = create_UUID(UUID_V5, UUID_NS_DNS, 'caugustin.de');
my $v1-mc_UUID_string = create_UUID_as_string(UUID_V1);
my $v3-md5_UUID_string = UUID_to_string($v3-md5_UUID_string);
if ( version_of_UUID($v1-mc_UUID) == 1 ) { ... };
if ( version_of_UUID($v5-sha1_UUID) == 5 ) { ... };
if ( is_UUID_string($v1-mc_UUID_string) ) { ... };
if ( equal_UUIDs($uuid1, $uuid2) ) { ... };
my $uuid_time = time_of_UUID($v1-mc_UUID);
my $uuid_clk_seq = clk_seq_of_UUID($v1-mc_UUID);
DESCRIPTION
UUID::Tiny is a lightweight, dependency-free Pure Perl module for UUID creation and testing. This module provides the creation of version 1 time based UUIDs (using random multicast MAC addresses), version 3 MD5 based UUIDs, version 4 random UUIDs, and version 5 SHA-1 based UUIDs.
No fancy OO interface, no plethora of different UUID representation formats and transformations - just string and binary. Conversion, test and time functions equally accept UUIDs and UUID strings, so don't bother to convert UUIDs for them!
All constants and public functions are exported by default, because if you didn't need/want them, you wouldn't use this module ...
UUID::Tiny deliberately uses a minimal functional interface for UUID creation (and conversion/testing), because in this case OO looks like overkill to me and makes the creation and use of UUIDs unnecessarily complicated.
If you need raw performance for UUID creation, or the real MAC address in version 1 UUIDs, or an OO interface, and if you can afford module compilation and installation on the target system, then better look at other CPAN UUID modules like Data::UUID.
This module should be thread save, because the (necessary) global variables are locked in the functions that access them. (Not tested.)
DEPENDENCIES
This module should run from Perl 5.6 up and uses only standard modules for its job. No compilation or installation required.
CONSTANTS
NIL UUID
This module provides the NIL UUID (shown with its string representation):
UUID_NIL: '00000000-0000-0000-0000-000000000000'
Pre-defined Namespace UUIDs
This module provides the common pre-defined namespace UUIDs (shown with their string representation):
UUID_NS_DNS: '6ba7b810-9dad-11d1-80b4-00c04fd430c8'
UUID_NS_URL: '6ba7b811-9dad-11d1-80b4-00c04fd430c8'
UUID_NS_OID: '6ba7b812-9dad-11d1-80b4-00c04fd430c8'
UUID_NS_X500: '6ba7b814-9dad-11d1-80b4-00c04fd430c8'
FUNCTIONS
All public functions are exported by default (they should not collide with other functions).
create_UUID()
creates standard binary UUIDs in network byte order (MSB first), create_UUID_as_string()
creates the standard string represantion of UUIDs.
All query and test functions (except is_UUID_string
) accept both representations.
create_UUID()
my $v1-mc_UUID = create_UUID();
my $v1-mc_UUID = create_UUID(UUID_V1);
my $v3-md5_UUID = create_UUID(UUID_V3, $ns_uuid, $name_or_filehandle);
my $v3-md5_UUID = create_UUID(UUID_V3, $name_or_filehandle);
my $v4-rand_UUID = create_UUID(UUID_V4);
my $v5-sha1_UUID = create_UUID(UUID_V5, $ns_uuid $name_or_filehandle);
my $v5-sha1_UUID = create_UUID(UUID_V5, $name_or_filehandle);
Creates a binary UUID in network byte order (MSB first). For v3 and v5 UUIDs a SCALAR
(normally a string), GLOB
("classic" file handle) or IO
object (i.e. IO::File
) can be used; files have to be opened for reading.
I found no hint if and how UUIDs should be created from file content. It seems to be undefined, but it is useful - so I would suggest to use UUID_NIL as the namespace UUID, because no "real name" is used; UUID_NIL is used by default if a namespace UUID is missing (only 2 arguments are used).
create_UUID_as_string()
Similar to create_UUID
, but creates a UUID string.
is_UUID_string()
my $bool = is_UUID_string($str);
UUID_to_string()
my $uuid_str = UUID_to_string($uuid);
This function returns $uuid
unchanged if it is a UUID string already.
string_to_UUID()
my $uuid = string_to_UUID($uuid_str);
This function returns $uuid_str
unchanged if it is a UUID already.
In addition to the standard UUID string representation and its URN forms (starting with urn:uuid:
or uuid:
), this function accepts 32 digit hex strings, variants with different positions of -
and Base64 encoded UUIDs.
Throws an exception if string can't be interpreted as a UUID.
If you want to make shure to have a "pure" standard UUID representation, check with is_UUID_string
!
version_of_UUID()
my $version = version_of_UUID($uuid);
This function accepts binary and string UUIDs.
time_of_UUID()
my $uuid_time = time_of_UUID($uuid);
This function accepts UUIDs and UUID strings. Returns the time as a floating point value, so use int()
to get a time()
compatible value.
Returns undef
if the UUID is not version 1.
clk_seq_of_UUID()
my $uuid_clk_seq = clk_seq_of_UUID($uuid);
This function accepts UUIDs and UUID strings. Returns the clock sequence for a version 1 UUID. Returns undef
if UUID is not version 1.
equal_UUIDs
my $bool = equal_UUIDs($uuid1, $uuid2);
Returns true if the provided UUIDs are equal. Accepts UUIDs and UUID strings (can be mixed).
DISCUSSION
- Why version 1 only with random multi-cast MAC addresses?
-
The random multi-cast MAC address gives privacy, and getting the real MAC address with Perl is really dirty (and slow);
- Should version 3 or version 5 be used?
-
Using SHA-1 reduces the probabillity of collisions and provides a better "randomness" of the resulting UUID compared to MD5. Version 5 is recommended in RFC 4122 if backward compatibility is not an issue.
Using MD5 (version 3) has a better performance. This could be important with creating UUIDs from file content rather than names.
UUID DEFINITION
See RFC 4122 (http://www.ietf.org/rfc/rfc4122.txt) for technical details on UUIDs.
AUTHOR
Much of this code is borrowed from UUID::Generator by ITO Nobuaki <banb@cpan.org>. But that module is announced to be marked as "deprecated" in the future and it is much too complicated for my liking.
So I decided to reduce it to the necessary parts and to re-implement those parts with a functional interface ...
Christian Augustin, <mail at caugustin.de>
BUGS
Please report any bugs or feature requests to bug-uuid-tiny at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=UUID-Tiny. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc UUID::Tiny
You can also look for information at:
RT: CPAN's request tracker
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
Kudos to ITO Nobuaki <banb@cpan.org> for his UUID::Generator::PurePerl module! My work is based on his code, and without it I would've been lost with all those incomprehensible RFC texts and C codes ...
COPYRIGHT & LICENSE
Copyright 2009 Christian Augustin, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.