Perl: UUIDv7
Reddit had a mini challenge about implementing UUIDv7 in various languages. I whipped up a Perl implementation that turned out pretty well. I submitted it to the official GitHub repo and it was accepted.
sub uuidv7 {
# current timestamp in ms
my $ts_ms = int(time() * 1000);
my $uuid = substr(pack("Q>", $ts_ms), 2, 6);
# Append ten random bytes
$uuid .= pack("C10", map { int(rand(256)) } 1 .. 10);
# Version and variant
substr($uuid, 6, 1, chr((ord(substr($uuid, 6, 1)) & 0x0F) | 0x70));
substr($uuid, 8, 1, chr((ord(substr($uuid, 8, 1)) & 0x3F) | 0x80));
return join('-', unpack("H8 H4 H4 H4 H12", $uuid));
}
See also: UUIDv4 in Perl.



