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