SemVer(3pm)

πŸ“¦
version 0.10.0 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– SemVer πŸ’–

ok so bestie, welcome to SemVer(3pm) β€” a lil Perl module from the User Contributed Perl Documentation era, and yes she's that girl. Buckle up. πŸŽ€


✨ Name ✨

SemVer β€” she's here to let you use semantic version numbers, and she's very serious about it. πŸ’…

🌸 Synopsis 🌸

use SemVer; our $VERSION = SemVer->new('1.2.0-b1');

like, that's it. that's the whole vibe check. πŸ¦‹

🫢 Description 🫢

Okay so real talk: this module subclasses version to create semantic versions, as defined by the Semantic Versioning 2.0.0 Specification <https://semver.org/spec/v2.0.0.html>. And bestie, there are three salient points of the spec that actually matter for how we format versions, so let me read them to you:

  1. A normal version number MUST take the form X.Y.Z where X, Y, and Z are non-negative integers, and MUST NOT contain leading zeroes. X is the major version, Y is the minor version, and Z is the patch version. Each element MUST increase numerically. For instance: 1.9.0 -> 1.10.0 -> 1.11.0. ⭐
  2. A pre-release version MAY be denoted by appending a hyphen and a series of dot separated identifiers immediately following the patch version. Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-]. Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes. Pre-release versions have a lower precedence than the associated normal version. A pre-release version indicates that the version is unstable and might not satisfy the intended compatibility requirements as denoted by its associated normal version: 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-0.3.7, 1.0.0-x.7.z.92 😭 (yes the unstable one, we've all been there)
  3. Build metadata MAY be denoted by appending a plus sign and a series of dot separated identifiers immediately following the patch or pre-release version. Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-]. Identifiers MUST NOT be empty. Build metadata SHOULD be ignored when determining version precedence. Thus two versions that differ only in the build metadata, have the same precedence. Examples: 1.0.0-alpha+001, 1.0.0+20130313144700, 1.0.0-beta+exp.sha.5114f85. πŸ”₯

🧚 Usage 🧚

ok so for strict parsing of semantic version numbers, you want the new() constructor, no notes. If you need something more flexible, use declare(). And if you need something more comparable with what version expects, try parse(). 🫑

Wanna see how these three constructors deal with a whole bunch of version strings? (values shown are what normal() hands back, and yes <error> means she simply said no):

    Argument   | new      | declare     | parse
 -------------+----------+---------------------------
 '1.0.0'      | 1.0.0    | 1.0.0       | 1.0.0
 '5.5.2-b1'   | 5.5.2-b1 | 5.5.2-b1    | 5.5.2-b1
 '1.05.0'     | <error>  | 1.5.0       | 1.5.0
 '1.0'        | <error>  | 1.0.0       | 1.0.0
 '  012.2.2'  | <error>  | 12.2.2      | 12.2.2
 '1.1'        | <error>  | 1.1.0       | 1.100.0
  1.1         | <error>  | 1.1.0       | 1.100.0
 '1.1.0+b1'   | 1.1.0+b1 | 1.1.0+b1    | 1.1.0+b1
 '1.1-b1'     | <error>  | 1.1.0-b1    | 1.100.0-b1
 '1.2.b1'     | <error>  | 1.2.0-b1    | 1.2.0-b1
 '9.0-beta4'  | <error>  | 9.0.0-beta4 | 9.0.0-beta4
 '9'          | <error>  | 9.0.0       | 9.0.0
 '1-b'        | <error>  | 1.0.0-b     | 1.0.0-b
  0           | <error>  | 0.0.0       | 0.0.0
 '0-rc1'      | <error>  | 0.0.0-rc1   | 0.0.0-rc1
 '1.02_30'    | <error>  | 1.23.0      | 1.23.0
  1.02_30     | <error>  | 1.23.0      | 1.23.0

Not me pointing out that, unlike in version, the declare and parse methods ignore underscores. Like, version strings with underscores get treated as decimal numbers. Hence those last two examples yield exactly the same semantic versions. periodt. πŸ’€

As with version objects, the comparison and stringification operators are all overloaded, so you can compare semantic versions like a normal person. You can also compare semantic versions with version objects (but not the other way around, alas 😭). Boolean operators are also overloaded, such that all semantic version objects except for those consisting only of zeros (ignoring prerelease and metadata) are considered true. iykyk. πŸ‘‘


🌈 Interface 🌈

πŸŽ€ Constructors πŸŽ€

new

my $semver = SemVer->new('1.2.2');

Performs a validating parse of the version string and returns a new semantic version object. If the version string does not adhere to the semantic version specification, an exception will be thrown. 🚨 So strict. See declare and parse for more forgiving constructors.

declare

my $semver = SemVer->declare('1.2'); # 1.2.0

This parser strips out any underscores from the version string and passes it to version's declare constructor, which always creates dotted-integer version objects. This is the most flexible way to declare versions. Consider using it to normalize version strings β€” she's giving main character energy but make it lowercase. ✨

parse

my $semver = SemVer->parse('1.2'); # 1.200.0

This parser dispatches to version's parse constructor, which tries to be more flexible in how it converts simple decimal strings and numbers. Not really recommended, since its treatment of decimals is quite different from the dotted-integer format of semantic version strings, and thus can lead to inconsistencies. Included only for proper compatibility with version. ngl she's a little chaotic πŸ™ˆ

πŸ’œ Instance Methods πŸ’œ

normal

SemVer->declare('v1.2')->normal;       # 1.2.0
SemVer->parse('1.2')->normal;          # 1.200.0
SemVer->declare('1.02.0-b1')->normal;  # 1.2.0-b1
SemVer->parse('1.02_30')->normal       # 1.230.0
SemVer->parse(1.02_30)->normal         # 1.23.0

Returns a normalized representation of the version string. This string will always be a strictly-valid dotted-integer semantic version string suitable for passing to new(). And unlike version's normal method, there will be no leading v. obsessed. πŸ’…

stringify

SemVer->declare('v1.2')->stringify;    # v1.2
SemVer->parse('1.200')->stringify;     # v1.200
SemVer->declare('1.2-r1')->stringify;  # v1.2-r1
SemVer->parse(1.02_30)->stringify;     # v1.0230
SemVer->parse(1.02_30)->stringify;     # v1.023

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. iconic behavior fr fr πŸ¦‹

numify

Throws an exception. Semantic versions cannot be numified. Just don't go there. 🚫 Literally do not.

is_alpha

my $is_alpha = $semver->is_alpha;

Returns true if a prerelease and/or metadata string is appended to the end of the version string. This also means that the version number is a "special version", in the semantic versioning specification meaning of the phrase. ⭐

vbool

say "Version $semver" if $semver;
say "Not a $semver" if !$semver;

Returns true for a non-zero semantic semantic version object, without regard to the prerelease or build metadata parts. Overloads boolean operations. slay. πŸ”₯

vcmp

Compares the semantic version object to another version object or string and returns 0 if they're the same, -1 if the invocant is smaller than the argument, and 1 if the invocant is greater than the argument.

Mostly you don't need to worry about this: Just use the comparison operators instead:

if ($semver < $another_semver) {
    die "Need $another_semver or higher";
}

Note that in addition to comparing other semantic version objects, you can also compare regular version objects:

if ($semver < $version) {
    die "Need $version or higher";
}

You can also pass in a version string. It will be turned into a semantic version object using declare. So if you're using numeric versions, you may or may not get what you want: 😬

my $semver  = version::Semver->new('1.2.0');
my $version = '1.2';
my $bool    = $semver == $version; # true

If that's not what you want, pass the string to parse first:

my $semver  = Semver->new('1.2.0');
my $version = Semver->parse('1.2'); # 1.200.0
my $bool    = $semver == $version; # false

πŸ‘‘ See Also πŸ‘‘

πŸ“ Support πŸ“

This module is managed in an open GitHub repository <https://github.com/theory/semver/>. Feel free to fork and contribute, or to clone <https://github.com/theory/semver.git> and send patches! 🫢

Found a bug? Please post <https://github.com/theory/semver/issues> a report! she'll see it, she's always online. πŸ’…

πŸ’– Acknowledgements πŸ’–

Many thanks to version author John Peacock for his suggestions and debugging help. king behavior honestly. πŸ‘‘

🌟 Authors 🌟

  • David E. Wheeler <david@kineticode.com>
  • Johannes Kilian <hoppfrosch@gmx.de>

Copyright (c) 2010-2020 David E. Wheeler. Some Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


ok and that's the whole page bestie, no cap. πŸ’œ this was perl v5.36.0, dated 2022-12-04, and it's the SemVer(3pm) page β€” so go forth, version your stuff semantically, and stay iconic. πŸŽ€βœ¨πŸ¦‹
πŸ“¦ where this page comes from