Modules · Language · 1.00

Horus

UUIDs of every RFC 9562 version, in ten formats, with no C library to install.

Versions 1 to 8 and the NIL and MAX UUIDs, written in XS with its own MD5, SHA-1 and random pool, so there is nothing to install beside it. Each comes out in the format you ask for, from the hyphenated string to 16 raw bytes, and a thousand come out of one call.

use Horus qw(:all);

# random, and time-ordered: v7 sorts by when it was made
my $v4 = uuid_v4();
my $v7 = uuid_v7();
print "$v4\n$v7\n";
print uuid_version($v7), "\n";                      # 7

# the same UUID in other formats
print uuid_convert($v7, UUID_FMT_URN), "\n";        # urn:uuid:...
print uuid_convert($v7, UUID_FMT_BASE64), "\n";     # 22 characters
print uuid_convert($v7, UUID_FMT_CROCKFORD), "\n";  # 26, and still sorts

# the millisecond it was made
printf "%.3f\n", uuid_time($v7);

What you get

  • Every version

    v1 and v6 from the clock, v2 for DCE, v3 and v5 from a namespace and a name, v4 random, v7 from the Unix millisecond and sortable, v8 your own bytes, and NIL and MAX.

  • Ten formats

    Hyphenated in either case, plain hex in either case, braces, a URN, base64 in 22 characters, base32 or Crockford in 26, and the 16 raw bytes. uuid_convert moves one between them.

  • Namespaces

    UUID_NS_DNS, _URL, _OID and _X500 are built in, so uuid_v5(UUID_NS_DNS, 'lnation.org') is the same UUID every time, anywhere.

  • In bulk

    uuid_v4_bulk(1000) makes a thousand in one crossing into C, drawn from a buffered random pool.

  • And back again

    uuid_validate, uuid_version, uuid_variant, uuid_cmp, uuid_parse, and uuid_time for the moment a time-based UUID was made.

In use

use Horus qw(:all);

# the same name in the same namespace is always the same UUID
my $id = uuid_v5(UUID_NS_DNS, 'lnation.org');
print "$id\n";
print $id eq uuid_v5(UUID_NS_DNS, 'lnation.org') ? "stable\n" : "changed\n";

# a thousand at once, in one call into C
my @batch = uuid_v4_bulk(1000);
my %seen;
print scalar(grep { !$seen{$_}++ } @batch), " unique\n";   # 1000 unique

print uuid_validate('not-a-uuid') ? '' : "rejected\n";
print uuid_cmp(uuid_nil(), uuid_max()), "\n";               # -1

One UUID, ten ways

UUID_FMT_STR 550e8400-e29b-41d4-a716-446655440000 (the default), UUID_FMT_UPPER_STR the same in capitals, UUID_FMT_HEX and UUID_FMT_UPPER_HEX without the hyphens, UUID_FMT_BRACES {550e8400-...}, UUID_FMT_URN urn:uuid:550e8400-...

UUID_FMT_BASE64 22 characters with no padding, UUID_FMT_BASE32 26 characters by RFC 4648, UUID_FMT_CROCKFORD 26 characters that sort as the UUID does, UUID_FMT_BINARY the 16 bytes.

For C

horus_core.h is the whole generator as plain C with no Perl in it, installed with the module and found with Horus::include_dir(). Another XS module, or a program in C, includes it and makes UUIDs with no Perl call at all.

Get it

cpanm Horus