Showing entries with tag "UUID".

Found 2 entries

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.

See also: UUIDv4 in Perl.

Leave A Reply

Perl: Generate UUIDv4

I needed simple and portable way to generate a version 4 UUID in Perl, so I hacked apart various pieces of UUID::Tiny and came up with this.

sub uuidv4 {
    my $uuid = '';

    # Four random bytes
    for (my $i = 0; $i < 4; $i++) {
        $uuid .= pack('I', int(rand(2 ** 32)));
    }

    # Replace the version of the UUID with 4 (0x40)
    substr($uuid, 6, 1, chr(ord(substr($uuid, 6, 1)) & 0x0f | 0x40 ));

    my @parts = map { substr $uuid, 0, $_, '' } ( 4, 2, 2, 2, 6 );
    my @hex   = map { unpack("H*", $_) } @parts;
    my $ret   = join('-', @hex);

    return $ret;
}
Leave A Reply