File Coverage

blib/lib/Acme/CPANModules/VersionNumber/Perl.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package Acme::CPANModules::VersionNumber::Perl;
2              
3 1     1   268306 use strict;
  1         1  
  1         268  
4              
5             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
6             our $DATE = '2023-10-31'; # DATE
7             our $DIST = 'Acme-CPANModules-VersionNumber-Perl'; # DIST
8             our $VERSION = '0.002'; # VERSION
9              
10             our $LIST = {
11             summary => 'List of libraries for working with Perl version numbers (or version strings)',
12             description => <<'_',
13              
14             The core module (a.k.a. version.pm) should be your first go-to
15             module when dealing with Perl version numbers. Other modules can also help in
16             some aspects. Modules mentioned here include: ,
17             .
18              
19             ## Version numbers in Perl
20              
21             There are two styles of version numbers used in the Perl world (i.e. for the
22             versioning of perl interpreter itself and for versioning Perl modules): decimal
23             (x.y) or dotted decimals (x.y.z or even more parts; the "v" prefix forces dotted
24             decimal to avoid ambiguity when there is only a single dot, e.g. v1.2).
25              
26             The former variant offers simplicity since version number can mostly be
27             represented by a floating point number (quoting as string is still recommended
28             to retain all precision and trailing zeros) and comparing versions can be done
29             numerically. However they are often very limited so in those cases a dotted
30             decimal variant can be used. For example the perl interpreter itself uses x.y.z
31             convention.
32              
33             Dotted decimal can be converted to decimal ("numified") form using this
34             convention: minor and lesser parts are given (at least) three decimal digits
35             each. For example, 1.2.3 becomes 1.002003. 1.20.3 becomes 1.020003. This can
36             give some surprise which has bitten Perl programmers, novice and expert alike.
37             In fact, it is the major gotcha when dealing with version numbers in Perl. For
38             example '0.02' (a decimal form) numifies to 0.02, but 'v0.02' (a dotted decimal
39             form) numifies to 0.002. Hence, v0.02 is less than 0.02 or even 0.01 when
40             compared using version->parse(). Another gotcha is when a module author decides
41             to go from 0.02 to 0.2.1 or 0.02.1. 0.02 (a decimal form) numifies to 0.02 while
42             0.2.1 or 0.02.1 (dotted decimal) numifies to 0.002001. Hence, going from 0.02 to
43             0.02.1 will actually *decrease* your version number. I recommend using x.yyy if
44             you use decimal form, i.e. start from 0.001 and not 0.01. It will support you
45             going smoothly to dotted decimal if you decide to do it one day.
46              
47             The numification is also problematic when a number part is > 999, e.g. 1.2.1234.
48             This breaks version comparison when comparison is done with version->parse().
49              
50             Aside from the abovementioned two styles, there is another: CPAN
51             distributions/modules can add an underscore in the last part of the version
52             number to signify alpha/dev/trial release, e.g. 1.2.3_01. PAUSE will not index
53             such releases, so testers will need to specify an explicit version number to
54             install, e.g. `cpanm Foo@1.2.3_01`. In some cases you need to pay attention when
55             comparing this kind of version numbers.
56              
57             ## Checking if a string is a valid version number
58              
59             To check if a string is a valid Perl version number, you can do:
60              
61             version->parse($str)
62              
63             which will die if C<$str> contains an invalid version string. version.pm can
64             handle the "v" prefix, (e.g. "v1.2"), dotted-decimal (e.g. "1.2.3" but also
65             "1.2.3.4.5"), as well as alpha/dev/trial part (e.g. "v1.1.1_001").
66              
67             ## Parsing a version number
68              
69             version->parse, obviously enough, is used to parse a version number string into
70             a structure:
71              
72             use Data::Dump;
73             dd( version->parse("1.2.3") );
74              
75             which prints:
76              
77             bless({ original => "1.2.3", qv => 1, version => [1, 2, 3] }, "version")
78              
79             However:
80              
81             dd( version->parse("1.2.3_01") );
82              
83             prints:
84              
85             bless({ alpha => 1, original => "1.2.3_01", qv => 1, version => [1, 2, 301] }, "version")
86              
87             ## Comparing version numbers
88              
89             You can compare two version numbers again using version->parse():
90              
91             version->parse($str1) <=> version->parse($str2)
92              
93             For example:
94              
95             version->parse("1.2.3") <=> version->parse("v1.3.0"); # => -1
96              
97             Be careful when dealing with alpha/dev/trial version:
98              
99             version->parse("1.2.3_01") <=> version->parse("v1.2.4") ; # => 1
100             version->parse("1.2.3_01") <=> version->parse("v1.2.301"); # => 0
101             version->parse("1.2.3_01") <=> version->parse("v1.2.400"); # => -1
102              
103             ## Normalizing a version number
104              
105             To normalize a version number:
106              
107             version->parse($str)->normal
108              
109             This will add a "v" prefix, force a dotted decimal form, and remove insignifcant
110             zeros. Examples:
111              
112             version->parse(1.2) ->normal; # => "v1.200.0"
113             version->parse("1.2.3") ->normal; # => "v1.2.3"
114             version->parse("1.2.30") ->normal; # => "v1.2.30"
115             version->parse("1.2.030")->normal; # => "v1.2.30"
116              
117             ## Incrementing a version number
118              
119             Some modules like and can help
120             increase version numbers (or whichever part of the number). The last one can
121             also decrement parts.
122              
123             _
124             entries => [
125             {module=>'version'},
126             {module=>'Perl::Version'},
127             {module=>'Versioning::Scheme::Perl'},
128             ],
129             };
130              
131             1;
132             # ABSTRACT: List of libraries for working with Perl version numbers (or version strings)
133              
134             __END__