Generate a UUID in Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language.
First developed in 1987 by Larry Wall, the latest version is Perl 5, which runs on 100s of platforms and is used in a wide range of contexts including web development, system administration, internet duct-tape and more. It was termed the "first postmodern computer language" by Mr. Wall in a talk in 1999.
How to Generate a UUID in Perl
Although the Perl programming language itself does not have built-in support for generating a UUID or GUID, there are quality 3rd party, open-source packages that you can use instead.
We recommend the UUID package.
It can generate version 1, 3, 4, 5, 6, or 7 UUIDs.
All are variant 1, meaning compliant with the OSF DCE standard as described in
RFC4122.
Installing the UUID package from CPAN
Perl packages are typically installed from the Comprehensive Perl Archive Network
(CPAN). It is common to use the
cpan minus
installer. You can install the UUID package with this command:
% cpanm UUID
Generating UUIDs
With the UUID package installed, you can now use it in your Perl code.
use strict;use warnings;use UUID qw(uuid4);my $uuid = uuid4();print "Your v4 UUID is: $uuid\n";
Explanation
- Lines #1 and #2 are good practice, ensuring common errors are caught and useful warnings are emitted.
- Line #3 includes the
UUIDmodule and imports theuuid4function into the current package. - Line #5 generates a Version 4 UUID and saves it in the variable,
$uuid. Theuuid4()function returns a new, random, Version 4 UUID. - The output from line #7 will be something like:
Your v4 UUID is: 374fe2ea-2034-420d-8d69-c49122fe6bed
The UUID package has a number of other functions, such as those for generating other UUID versions and
for parsing UUID strings. You can read more about the UUID
package on its MetaCPAN page.
Special thanks to Adam Clarke for the information on generating UUIDs in Perl.
How can we improve this page? Let us know!