1version(3) User Contributed Perl Documentation version(3)
2
3
4
6 version - Perl extension for Version Objects
7
9 # Parsing version strings (decimal or dotted-decimal)
10
11 use version 0.77; # get latest bug-fixes and API
12 $ver = version->parse($string)
13
14 # Declaring a dotted-decimal $VERSION (keep on one line!)
15
16 use version; our $VERSION = version->declare("v1.2.3"); # formal
17 use version; our $VERSION = qv("v1.2.3"); # deprecated
18 use version; our $VERSION = qv("v1.2_3"); # deprecated
19
20 # Declaring an old-style decimal $VERSION (use quotes!)
21
22 our $VERSION = "1.0203"; # recommended
23 use version; our $VERSION = version->parse("1.0203"); # formal
24 use version; our $VERSION = version->parse("1.02_03"); # alpha
25
26 # Comparing mixed version styles (decimals, dotted-decimals, objects)
27
28 if ( version->parse($v1) == version->parse($v2) ) {
29 # do stuff
30 }
31
32 # Sorting mixed version styles
33
34 @ordered = sort { version->parse($a) <=> version->parse($b) } @list;
35
37 Version objects were added to Perl in 5.10. This module implements
38 version objects for older version of Perl and provides the version
39 object API for all versions of Perl. All previous releases before 0.74
40 are deprecated and should not be used due to incompatible API changes.
41 Version 0.77 introduces the new 'parse' and 'declare' methods to
42 standardize usage. You are strongly urged to set 0.77 as a minimum in
43 your code, e.g.
44
45 use version 0.77; # even for Perl v.5.10.0
46
48 There are two different types of version objects, corresponding to the
49 two different styles of versions in use:
50
51 Decimal Versions
52 The classic floating-point number $VERSION. The advantage to this
53 style is that you don't need to do anything special, just type a
54 number into your source file. Quoting is recommended, as it ensures
55 that trailing zeroes ("1.50") are preserved in any warnings or other
56 output.
57
58 Dotted Decimal Versions
59 The more modern form of version assignment, with 3 (or potentially
60 more) integers separated by decimal points (e.g. v1.2.3). This is
61 the form that Perl itself has used since 5.6.0 was released. The
62 leading 'v' is now strongly recommended for clarity, and will throw a
63 warning in a future release if omitted. A leading 'v' character is
64 required to pass the "is_strict()" test.
65
67 If you have a module that uses a decimal $VERSION (floating point), and
68 you do not intend to ever change that, this module is not for you.
69 There is nothing that version.pm gains you over a simple $VERSION
70 assignment:
71
72 our $VERSION = "1.02";
73
74 Since Perl v5.10.0 includes the version.pm comparison logic anyways,
75 you don't need to do anything at all.
76
77 How to convert a module from decimal to dotted-decimal
78 If you have used a decimal $VERSION in the past and wish to switch to a
79 dotted-decimal $VERSION, then you need to make a one-time conversion to
80 the new format.
81
82 Important Note: you must ensure that your new $VERSION is numerically
83 greater than your current decimal $VERSION; this is not always obvious.
84 First, convert your old decimal version (e.g. 1.02) to a normalized
85 dotted-decimal form:
86
87 $ perl -Mversion -e 'print version->parse("1.02")->normal'
88 v1.20.0
89
90 Then increment any of the dotted-decimal components (v1.20.1 or
91 v1.21.0).
92
93 How to "declare()" a dotted-decimal version
94 use version; our $VERSION = version->declare("v1.2.3");
95
96 The "declare()" method always creates dotted-decimal version objects.
97 When used in a module, you must put it on the same line as "use
98 version" to ensure that $VERSION is read correctly by PAUSE and
99 installer tools. You should also add 'version' to the
100 'configure_requires' section of your module metadata file. See
101 instructions in ExtUtils::MakeMaker or Module::Build for details.
102
103 Important Note: Even if you pass in what looks like a decimal number
104 ("1.2"), a dotted-decimal will be created ("v1.200.0"). To avoid
105 confusion or unintentional errors on older Perls, follow these
106 guidelines:
107
108 • Always use a dotted-decimal with (at least) three components
109
110 • Always use a leading-v
111
112 • Always quote the version
113
114 If you really insist on using version.pm with an ordinary decimal
115 version, use "parse()" instead of declare. See the "PARSING AND
116 COMPARING VERSIONS" for details.
117
118 See also version::Internals for more on version number conversion,
119 quoting, calculated version numbers and declaring developer or "alpha"
120 version numbers.
121
123 If you need to compare version numbers, but can't be sure whether they
124 are expressed as numbers, strings, v-strings or version objects, then
125 you should use version.pm to parse them all into objects for
126 comparison.
127
128 How to "parse()" a version
129 The "parse()" method takes in anything that might be a version and
130 returns a corresponding version object, doing any necessary conversion
131 along the way.
132
133 • Dotted-decimal: bare v-strings (v1.2.3) and strings with more than
134 one decimal point and a leading 'v' ("v1.2.3"); NOTE you can
135 technically use a v-string or strings with a leading-v and only one
136 decimal point (v1.2 or "v1.2"), but you will confuse both yourself
137 and others.
138
139 • Decimal: regular decimal numbers (literal or in a string)
140
141 Some examples:
142
143 $variable version->parse($variable)
144 --------- -------------------------
145 1.23 v1.230.0
146 "1.23" v1.230.0
147 v1.23 v1.23.0
148 "v1.23" v1.23.0
149 "1.2.3" v1.2.3
150 "v1.2.3" v1.2.3
151
152 See version::Internals for more on version number conversion.
153
154 How to check for a legal version string
155 If you do not want to actually create a full blown version object, but
156 would still like to verify that a given string meets the criteria to be
157 parsed as a version, there are two helper functions that can be
158 employed directly:
159
160 "is_lax()"
161 The lax criteria corresponds to what is currently allowed by the
162 version parser. All of the following formats are acceptable for
163 dotted-decimal formats strings:
164
165 v1.2
166 1.2345.6
167 v1.23_4
168 1.2345
169 1.2345_01
170
171 "is_strict()"
172 If you want to limit yourself to a much more narrow definition of
173 what a version string constitutes, "is_strict()" is limited to
174 version strings like the following list:
175
176 v1.234.5
177 2.3456
178
179 See version::Internals for details of the regular expressions that
180 define the legal version string forms, as well as how to use those
181 regular expressions in your own code if "is_lax()" and "is_strict()"
182 are not sufficient for your needs.
183
184 How to compare version objects
185 Version objects overload the "cmp" and "<=>" operators. Perl
186 automatically generates all of the other comparison operators based on
187 those two so all the normal logical comparisons will work.
188
189 if ( version->parse($v1) == version->parse($v2) ) {
190 # do stuff
191 }
192
193 If a version object is compared against a non-version object, the non-
194 object term will be converted to a version object using "parse()".
195 This may give surprising results:
196
197 $v1 = version->parse("v0.95.0");
198 $bool = $v1 < 0.94; # TRUE since 0.94 is v0.940.0
199
200 Always comparing to a version object will help avoid surprises:
201
202 $bool = $v1 < version->parse("v0.94.0"); # FALSE
203
204 Note that "alpha" version objects (where the version string contains a
205 trailing underscore segment) compare as less than the equivalent
206 version without an underscore:
207
208 $bool = version->parse("1.23_45") < version->parse("1.2345"); # TRUE
209
210 See version::Internals for more details on "alpha" versions.
211
213 is_alpha()
214 True if and only if the version object was created with a underscore,
215 e.g.
216
217 version->parse('1.002_03')->is_alpha; # TRUE
218 version->declare('1.2.3_4')->is_alpha; # TRUE
219
220 is_qv()
221 True only if the version object is a dotted-decimal version, e.g.
222
223 version->parse('v1.2.0')->is_qv; # TRUE
224 version->declare('v1.2')->is_qv; # TRUE
225 qv('1.2')->is_qv; # TRUE
226 version->parse('1.2')->is_qv; # FALSE
227
228 normal()
229 Returns a string with a standard 'normalized' dotted-decimal form with
230 a leading-v and at least 3 components.
231
232 version->declare('v1.2')->normal; # v1.2.0
233 version->parse('1.2')->normal; # v1.200.0
234
235 numify()
236 Returns a value representing the object in a pure decimal.
237
238 version->declare('v1.2')->numify; # 1.002000
239 version->parse('1.2')->numify; # 1.200
240
241 stringify()
242 Returns a string that is as close to the original representation as
243 possible. If the original representation was a numeric literal, it
244 will be returned the way perl would normally represent it in a string.
245 This method is used whenever a version object is interpolated into a
246 string.
247
248 version->declare('v1.2')->stringify; # v1.2
249 version->parse('1.200')->stringify; # 1.2
250 version->parse(1.02_30)->stringify; # 1.023
251
253 qv()
254 This function is no longer recommended for use, but is maintained for
255 compatibility with existing code. If you do not want to have it
256 exported to your namespace, use this form:
257
258 use version 0.77 ();
259
260 is_lax()
261 (Not exported by default)
262
263 This function takes a scalar argument and returns a boolean value
264 indicating whether the argument meets the "lax" rules for a version
265 number. Leading and trailing spaces are not allowed.
266
267 is_strict()
268 (Not exported by default)
269
270 This function takes a scalar argument and returns a boolean value
271 indicating whether the argument meets the "strict" rules for a version
272 number. Leading and trailing spaces are not allowed.
273
275 John Peacock <jpeacock@cpan.org>
276
278 version::Internals.
279
280 perl.
281
282
283
284perl v5.34.0 2021-07-27 version(3)