NAME
Version::Semantic - Immutable SemVer scheme based version class
SYNOPSIS
use Version::Semantic ();
my $v_unstable = Version::Semantic->parse( '1.0.1-alpha' );
my $v_testing = Version::Semantic->parse( '1.0.1-beta' );
my $v_stable = Version::Semantic->parse( 'v1.0.1' );
if ( $v_unstable < $v_testing and $v_testing < $v_stable ) {
...
}
DESCRIPTION
This class defines immutable version objects that support the Semantic Versioning (SemVer) Scheme.
The class has a parse() factory method (a class method) to create objects from their semantic version strings. The method accepts semantic version strings that have a v prefix (Is "v1.2.3" a semantic version?).
The semver_re() class method provides the regular expression that is used by the parse() method. The parse() method adds a \A anchor at the beginning and a \z anchor at the end of the regular expression pattern.
The class has a usual new() constructor method (class method) to create objects from their named attributes.
Each object has the required attributes major, minor, and patch. Optional object attributes are prefix, pre_release and build. has_prefix(), has_pre_release() and has_build() predicate methods exist too.
The version_core() object method returns the semantic version string of an object with the v prefix (if originally set) but without the pre-release and the build extensions.
The to_string() object method returns the semantic version string of an object including the v prefix (if originally set). The method is used to overload the stringification "" operator.
The compare_to() object method implements the SemVer precedence rules. This method is used to overload the numeric comparison <=> operator. This allows you to compare version objects.
ABOUT PRE-RELEASES
Perl recognizes two version styles: decimal versions and dotted decimal versions. The dotted decimal version style is the newer one. It matches the SemVer scheme but doesn't allow pre-release and build extensions.
If the archive file of a Perl distribution has a name ending in TRIAL[0-9]* (before the file extension), it indicates that the distribution is a pre-release. TRIAL[0-9]* is a valid SemVer pre-release extension. For obvious reasons such kind of pre-releases are also named "trial releases". If ExtUtils::MakeMaker (EUMM) is your installer, the DISTVNAME EUMM attribute can be used to mark a distribution as a trial release (example value "Foo-Bar-v1.0.1-TRIAL5").
INCREMENT
The increment() object method increments a version object and returns the new one. The method has two optional parameters. The first one specifies the incrementation strategy. The following strategies are implemented: patch (the default), minor, major, and trial. The second parameter allows you to set or overwrite the pre_release attribute for non-trial strategies. Example:
# Leads to "v1.0.1-TRIAL5"
my $v_trial = Version::Semantic->new(
prefix => 'v',
major => 1,
minor => 0,
patch => 0
)->increment( 'patch', 'TRIAL5' );
The trial strategy can be applied to trial releases only. It increments the trial release number ignoring any kind of formatting (leading zeros for example). This strategy should be considered experimental.
Other Perl CPAN distributions that provide version object incrementation implementations are Version::Next and Perl::Version.
SEE ALSO
AUTHOR
Sven Willenbuecher <sven.willenbuecher@gmx.de>
COPYRIGHT AND LICENSE
This software is copyright (c) 2026 by Sven Willenbuecher.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.