vcmp#

Compare two versions, returning -1, 0, or 1 like <=>.

Synopsis#

my $ord = $v1 <=> $v2;
my $ord = $v1 cmp $v2;
my $ord = $v1->vcmp($v2);

This is the function the overloaded operators dispatch to. Both operands are normalised to dotted-integer parts before comparison, so mixing decimal, dotted-integer, and v-string forms on either side works as expected. ==, !=, <, <=, >, >=, eq, ne, and cmp all build on top of vcmp.

What you get back#

An integer: -1 if the left side sorts first, 0 if equal, 1 if the right side sorts first.

Examples#

Ordering different notations produces the intuitive result:

version->new("1.2.3") <=> version->new("1.002003");   # 0
version->new("1.2.4") <=> version->new("1.2.3");      # 1

The overloaded operators are the common entry point:

my @ordered = sort { $a <=> $b } map { version->parse($_) }
              qw(1.2 v1.3.0 1.2.5 1.19);

Alpha versions sort below the corresponding release:

version->new("1.02_03") < version->new("1.0203");     # true

Edge cases#

  • One side shorter than the other — the missing trailing components are treated as zero, so v1.2 equals v1.2.0.

  • A non-version scalar on either side — parsed with new first, so $v == "1.2.3" is well-defined.

Differences from upstream#

Fully compatible with upstream version 0.9929.

See also#

  • new — builds the objects on either side

  • numify — useful when you need a sortable key for non-version code

  • boolean — the overloaded truth test, not an ordering