Developer Lab · Perl
Generate UUID in Perl
UUID generation in Perl using the Data::UUID CPAN module - battle-tested and widely deployed.
Implementation Code
use strict;
use warnings;
use Data::UUID;
my $ug = Data::UUID->new;
# Generate a time-based UUID string
my $uuid = $ug->create_str;
print "$uuid\n";
# Generate multiple
for (1..5) {
print $ug->create_str, "\n";
}Explanation
Data::UUID is the standard CPAN module for UUID generation in Perl.
create_str() returns a UUID as a formatted string (e.g. 550e8400-e29b-41d4-a716-446655440000).
Use create() for a binary representation.
Output Example
7f3c2a1b-9d4e-4f8a-b5c6-3e2d1f0a9b8c
Best Practices, Performance, and Security
Best practices
Reuse the Data::UUID object across calls rather than creating a new instance each time.
For web apps, consider UUID::Tiny as a pure-Perl alternative with no XS dependency.
Performance
Good. Data::UUID is XS-based (C extension) so it's fast. Suitable for high-volume ID generation in Perl services.
Security
Uses OS-level entropy sources. Suitable for non-security-critical identifiers. For session tokens, prefer a dedicated CSPRNG source.
Installation
Data::UUID (CPAN)
cpan Data::UUIDOr via system package manager: apt install libdata-uuid-perl
Frequently Asked Questions
How do I generate a UUID in Perl?
Use a CPAN module such as Data::UUID or UUID::Tiny. With UUID::Tiny, import create_uuid_as_string and call create_uuid_as_string(UUID_V4) for a random v4 UUID. Data::UUID is also widely deployed and battle-tested.
Is Data::UUID->create_str() cryptographically secure?
No. Data::UUID->create_str() produces a time-based (v1-style) UUID derived from the timestamp and host, not output from a CSPRNG. For security-sensitive identifiers such as session tokens or API keys, use UUID::Tiny's create_uuid_as_string(UUID_V4), which draws on the OS secure random source.
What is the difference between UUID v4 and v7 in Perl?
UUID v4 (via UUID::Tiny's UUID_V4) is fully random and not sortable. UUID v7 embeds a millisecond timestamp for chronological sorting (RFC 9562). Use v4 for general-purpose IDs; use v7 for database primary keys at scale.
Do I need to install a package for UUID generation in Perl?
A package or library install may be required. See the Installation section for the recommended approach in Perl.
How do I validate a UUID string in Perl?
Use the platform's UUID parse/validation function, or test against the RFC 4122 regex: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i. Always validate external input at API boundaries.
Should I use UUIDs as database primary keys in Perl applications?
UUIDs work well as primary keys for distributed systems. Prefer native UUID/BINARY(16) column types over VARCHAR(36). For very large tables, consider UUID v7 for better B-tree insert locality.
Can I generate UUIDs in Perl without a network connection?
Yes. UUID generation uses local OS entropy sources and does not require network access. Each call is independent and thread-safe on modern platforms.
What output formats are available in Perl?
The standard hyphenated lowercase string (36 chars) is the default. Most APIs also support 32-char hex (no hyphens) and 16-byte binary formats. Use string format for APIs and binary for database storage.
What RFC standards apply to Perl UUID generation?
Version 4 UUIDs follow RFC 4122. UUID v7 follows RFC 9562 (May 2024). Ensure your chosen method produces compliant version and variant bits.
When should I avoid UUID v1?
Avoid UUID v1 in security-sensitive contexts - it embeds MAC address and timestamp information. Prefer v4 (via UUID::Tiny's UUID_V4) unless you need legacy Cassandra timeuuid compatibility.
Key definitions
- UUID
- 128-bit universally unique identifier, usually shown as 36 hex characters with hyphens.
- CSPRNG
- Cryptographically secure pseudo-random number generator - the entropy source behind secure UUID generation.
- RFC 4122
- IETF standard defining UUID versions 1 through 5. Version 4 is random.
- RFC 9562
- IETF standard adding UUID versions 6, 7, and 8. Version 7 is time-ordered.