version(3perl)
π version β Perl extension for Version Objects π
okay so bestie, this is version(3perl), straight out of the Perl Programmers Reference Guide, and it's literally the whole β¨ vibe β¨ of Perl version numbers: parsing them, declaring them, comparing them, and not crying about it. Let's go. π
β¨ SYNOPSIS β¨
# Parsing version strings (decimal or dotted-decimal)
use version 0.77; # get latest bug-fixes and API
$ver = version->parse($string)
# Declaring a dotted-decimal $VERSION (keep on one line!)
use version; our $VERSION = version->declare("v1.2.3"); # formal
use version; our $VERSION = qv("v1.2.3"); # deprecated
use version; our $VERSION = qv("v1.2_3"); # deprecated
# Declaring an old-style decimal $VERSION (use quotes!)
our $VERSION = "1.0203"; # recommended
use version; our $VERSION = version->parse("1.0203"); # formal
use version; our $VERSION = version->parse("1.02_03"); # alpha
# Comparing mixed version styles (decimals, dotted-decimals, objects)
if ( version->parse($v1) == version->parse($v2) ) {
# do stuff
}
# Sorting mixed version styles
@ordered = sort { version->parse($a) <=> version->parse($b) } @list;
πΈ DESCRIPTION πΈ
Version objects got added to Perl way back in 5.10, and this module is the icon that brings version objects to older Perls and hands the version object API to every version of Perl. Serving everyone, honestly. π
Every single release before 0.74 is deprecated and fr fr should not be used, because the API changes were incompatible β it's giving chaos. Version 0.77 is where the new parse and declare methods show up to standardize how you do things.
And bestie, you are strongly urged to set 0.77 as a minimum in your code, like:
use version 0.77; # even for Perl v.5.10.0
π¦ TYPES OF VERSION OBJECTS π¦
There are two different types of version objects, and they match up with the two different styles of versions in use. Two flavors, one icon. π
Decimal Versions
The classic floating-point number $VERSION. The literally amazing advantage here is that you don't need to do anything special β just type a number into your source file and you're done. Quoting is recommended though, because quoting is what keeps trailing zeroes ("1.50") preserved in any warnings or other output. Don't let your zeroes get eaten. π€
Dotted Decimal Versions
The more modern form of version assignment β 3 (or potentially more) integers separated by decimal points, e.g. v1.2.3. This is the form Perl itself has used ever since 5.6.0 dropped. The leading v is now strongly recommended for clarity, and will throw a warning in a future release if you omit it. Also important: a leading v character is required to pass the "is_strict()" test. So like... put the v. π
π DECLARING VERSIONS π
Okay so if you have a module using a decimal $VERSION (floating point) and you are never planning on changing that, then bestie, this module is not for you. There's nothing version.pm gains you over a simple $VERSION assignment:
our $VERSION = "1.02";
And since Perl v5.10.0 includes the version.pm comparison logic anyways, you don't need to do anything at all. Slay, do nothing. π«Ά
How to convert a module from decimal to dotted-decimal
If you've used a decimal $VERSION in the past and now you wanna switch to a dotted-decimal $VERSION, you need to do a one-time conversion to the new format.
Important Note: you must ensure that your new$VERSIONis numerically greater than your current decimal$VERSION. And no, this is not always obvious. Do not skip this step. π
First, convert your old decimal version (e.g. 1.02) to a normalized dotted-decimal form:
$ perl -Mversion -e 'print version->parse("1.02")->normal'
v1.20.0
Then increment any of the dotted-decimal components (v1.20.1 or v1.21.0). Baby steps, but make them numeric. β¨
How to declare() a dotted-decimal version
use version; our $VERSION = version->declare("v1.2.3");
The declare() method always creates dotted-decimal version objects. When you use it in a module, you have to put it on the same line as use version so that $VERSION gets read correctly by PAUSE and installer tools. You should also add 'version' to the 'configure_requires' section of your module metadata file β see the instructions in ExtUtils::MakeMaker or Module::Build for the details.
Important Note: even if you pass in what looks like a decimal number ("1.2"), a dotted-decimal will be created ("v1.200.0"). It's giving surprise makeover. To avoid confusion or unintentional errors on older Perls, follow these guidelines:
- Always use a dotted-decimal with (at least) three components
- Always use a leading-v
- Always quote the version
If you really insist on using version.pm with an ordinary decimal version, use parse() instead of declare. See the "PARSING AND COMPARING VERSIONS" for details.
Also see version::Internals for more on version number conversion, quoting, calculated version numbers, and declaring developer or "alpha" version numbers.
π PARSING AND COMPARING VERSIONS π
If you need to compare version numbers but you can't be sure whether they're expressed as numbers, strings, v-strings or version objects β bestie, that's the whole situation β then you should use version.pm to parse them all into objects for comparison. Vibe check everything. β¨
How to parse() a version
The parse() method takes in anything that might be a version and returns a corresponding version object, doing any necessary conversion along the way. It's very "come as you are." π
- Dotted-decimal: bare v-strings (
v1.2.3) and strings with more than one decimal point and a leading'v'("v1.2.3"); NOTE you can technically use a v-string or strings with a leading-vand only one decimal point (v1.2or"v1.2"), but you will confuse both yourself and others. Not worth it. π - Decimal: regular decimal numbers (literal or in a string)
Some examples:
$variable version->parse($variable)
--------- -------------------------
1.23 v1.230.0
"1.23" v1.230.0
v1.23 v1.23.0
"v1.23" v1.23.0
"1.2.3" v1.2.3
"v1.2.3" v1.2.3
See version::Internals for more on version number conversion.
How to check for a legal version string
If you don't actually want to create a full blown version object, but you'd still like to verify that a given string meets the criteria to be parsed as a version, there are two helper functions you can employ directly:
is_lax()
The lax criteria corresponds to what is currently allowed by the version parser. All of the following formats are acceptable for dotted-decimal formats strings:
v1.2
1.2345.6
v1.23_4
1.2345
1.2345_01
is_strict()
If you want to limit yourself to a much more narrow definition of what a version string constitutes, is_strict() is limited to version strings like the following list:
v1.234.5
2.3456
See version::Internals for details of the regular expressions that define the legal version string forms, as well as how to use those regular expressions in your own code if is_lax() and is_strict() are not sufficient for your needs. (They're the main characters, but not the only characters. π
)
How to compare version objects
Version objects overload the "cmp" and "<=>" operators. Perl automatically generates all of the other comparison operators based on those two, so all the normal logical comparisons will work. Periodt.
if ( version->parse($v1) == version->parse($v2) ) {
# do stuff
}
If a version object is compared against a non-version object, the non-object term will be converted to a version object using parse(). This may give surprising results:
$v1 = version->parse("v0.95.0");
$bool = $v1 < 0.94; # TRUE since 0.94 is v0.940.0
Always comparing to a version object will help avoid surprises:
$bool = $v1 < version->parse("v0.94.0"); # FALSE
Note that "alpha" version objects (where the version string contains a trailing underscore segment) compare as less than the equivalent version without an underscore:
$bool = version->parse("1.23_45") < version->parse("1.2345"); # TRUE
See version::Internals for more details on "alpha" versions.
π OBJECT METHODS π
is_alpha()
True if and only if the version object was created with a underscore, e.g.
version->parse('1.002_03')->is_alpha; # TRUE
version->declare('1.2.3_4')->is_alpha; # TRUE
is_qv()
True only if the version object is a dotted-decimal version, e.g.
version->parse('v1.2.0')->is_qv; # TRUE
version->declare('v1.2')->is_qv; # TRUE
qv('1.2')->is_qv; # TRUE
version->parse('1.2')->is_qv; # FALSE
normal()
Returns a string with a standard 'normalized' dotted-decimal form with a leading-v and at least 3 components.
version->declare('v1.2')->normal; # v1.2.0
version->parse('1.2')->normal; # v1.200.0
numify()
Returns a value representing the object in a pure decimal.
version->declare('v1.2')->numify; # 1.002000
version->parse('1.2')->numify; # 1.200
to_decimal
This returns a new version object for the numified version, much like "version->parse($v->numify)" would.
version->parse('v1.2')->to_decimal; # 1.002000
to_dotted_decimal
This returns a new version object for the normalized version, much like "version->parse($v->normal)" would.
version->parse('1.002')->to_dotted_decimal; # v1.2.0
tuple()
This turns the components of the version into a list. E.g.
version->parse('1.2.3')->tuple; # (1, 2, 3)
from_tuple(...)
This takes a list of components and creates a dotted decimal version out of it. E.g.
version->from_tuple(1, 2, 3) # v1.2.3
stringify()
Returns a string that is as close to the original representation as possible. If the original representation was a numeric literal, it will be returned the way perl would normally represent it in a string. This method is used whenever a version object is interpolated into a string. Very "be yourself." π¦
version->declare('v1.2')->stringify; # v1.2
version->parse('1.200')->stringify; # 1.2
version->parse(1.02_30)->stringify; # 1.023
tuple
Returns an array of non-negative integers that is used for comparison purposes with other version objects.
π EXPORTED FUNCTIONS π
qv()
This function is no longer recommended for use, but is maintained for compatibility with existing code. If you do not want to have it exported to your namespace, use this form:
use version 0.77 ();
is_lax()
(Not exported by default)
This function takes a scalar argument and returns a boolean value indicating whether the argument meets the "lax" rules for a version number. Leading and trailing spaces are not allowed. So don't pad it, bestie. π
is_strict()
(Not exported by default)
This function takes a scalar argument and returns a boolean value indicating whether the argument meets the "strict" rules for a version number. Leading and trailing spaces are not allowed.
π AUTHOR π
John Peacock <jpeacock@cpan.org> β the blueprint, the moment, the icon behind all of this. β¨
π SEE ALSO π
- version::Internals.
- perl.
and that's a wrap on this one bestie π this page is from perl v5.44.0, dated 2026-10-17, and it lives at version(3perl). go forth and version responsibly. ππ¦β¨
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms