Perl: Generate random UUID
I needed simple and portable way to generate a random v4 UUID in Perl, so I ripped out various pieces of UUID::Tiny
and came up with this.
sub gen_uuid {
my $uuid = '';
for ( 1 .. 4 ) {
$uuid .= pack 'I', int(rand(2 ** 32));
}
substr $uuid, 6, 1, chr( ord( substr( $uuid, 6, 1 ) ) & 0x0f | 0x40 );
return join '-',
map { unpack 'H*', $_ }
map { substr $uuid, 0, $_, '' }
( 4, 2, 2, 2, 6 );
}