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 UUID v4 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 Practice
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.
Installation
cpan Data::UUID
Or via system package manager: apt install libdata-uuid-perl
Security
Uses OS-level entropy sources. Suitable for non-security-critical identifiers. For session tokens, prefer a dedicated CSPRNG source.