Rand Stats

Version::Semver

zef:lizmat

Actions Status Actions Status Actions Status

NAME

Version::Semver - Implement Semver Version logic

SYNOPSIS

use Version::Semver;

my $left  = Version::Semver.new("1.0.0");
my $right = Version::Semver.new("1.1.0");

# method interface
say $left.cmp($right);  # Less
say $left."<"($right);  # True

# infix interface
say $left cmp $right;  # Less
say $left < $right;    # True

DESCRIPTION

The Version::Semver distribution provides a Version::Semver class which encapsulates the logic for creating a Version-like object with semantics matching the Semantic Versioning 2.0.0 standard.

INSTANTIATION

use Version::Semver;

my $sv = Version::Semver.new("1.2.3-pre.release+build.data");

The basic instantion of a Version::Semver object is done with the new method, taking the version string as a positional argument.

ACCESSORS

major

my $sv = Version::Semver.new("1.2.3");
say $sv.major;  # 1

Returns the major version value.

minor

my $sv = Version::Semver.new("1.2.3");
say $sv.minor;  # 2

Returns the minor version value.

patch

my $sv = Version::Semver.new("1.2.3");
say $sv.patch;  # 3

Returns the patch value.

pre-release

my $sv = Version::Semver.new("1.2.3-foo.bar");
say $sv.pre-release;  # (foo bar)

Returns a List with the pre-release tokens.

build

my $sv = Version::Semver.new("1.2.3+build.data");
say $sv.build;  # (build data)

Returns a List with the build tokens.

OTHER METHODS

cmp

my $left  = Version::Semver.new("1.0.0");
my $right = Version::Semver.new("1.1.0");

say $left.cmp($left);   # Same
say $left.cmp($right);  # Less
say $right.cmp($left);  # More

The cmp method returns the Order of a comparison of the invocant and the positional argument, which is either Less, Same, or More. This method is the workhorse for comparisons.

eqv

my $left  = Version::Semver.new("1.0.0");
my $right = Version::Semver.new("1.0.0+build.data");

say $left.eqv($right);  # True

The eqv method returns whether the internal state of two Version::Semver objects is identical. Note that does not necessarily means that their stringification is the same, as any build data is ignored in these comparisons.

== != < <= > >=

my $left  = Version::Semver.new("1.2.3");
my $right = Version::Semver.new("1.2.4");

say $left."=="($left);  # True
say $left."<"($right);  # True

These oddly named methods provide the same functionality as their infix counterparts. Please note that you must use the "xx"() syntax, because otherwise the Raku compiler will assume you've made a syntax error.

EXPORTED INFIXES

The following infix candidates handling Version::Semver are exported:

AUTHOR

Elizabeth Mattijsen liz@raku.rocks

Source can be located at: https://github.com/lizmat/Version-Semver . Comments and Pull Requests are welcome.

If you like this module, or what I’m doing more generally, committing to a small sponsorship would mean a great deal to me!

COPYRIGHT AND LICENSE

Copyright 2025 Elizabeth Mattijsen

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.