1version(3pm) Perl Programmers Reference Guide version(3pm)
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 0.77; our $VERSION = version->declare("v1.2.3"); # formal
17 use version 0.77; our $VERSION = qv("v1.2.3"); # shorthand
18 use version 0.77; our $VERSION = qv("v1.2_3"); # alpha
19
20 # Declaring an old-style decimal $VERSION (use quotes!)
21
22 our $VERSION = "1.0203"; # recommended
23 use version 0.77; our $VERSION = version->parse("1.0203"); # formal
24 use version 0.77; 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 seperated 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.
64
66 If you have a module that uses a decimal $VERSION (floating point), and
67 you do not intend to ever change that, this module is not for you.
68 There is nothing that version.pm gains you over a simple $VERSION
69 assignment:
70
71 our $VERSION = "1.02";
72
73 Since Perl v5.10.0 includes the version.pm comparison logic anyways,
74 you don't need to do anything at all.
75
76 How to convert a module from decimal to dotted-decimal
77 If you have used a decimal $VERSION in the past and wish to switch to a
78 dotted-decimal $VERSION, then you need to make a one-time conversion to
79 the new format.
80
81 Important Note: you must ensure that your new $VERSION is numerically
82 greater than your current decimal $VERSION; this is not always obvious.
83 First, convert your old decimal version (e.g. 1.02) to a normalized
84 dotted-decimal form:
85
86 $ perl -Mversion -e 'print version->parse("1.02")->normal'
87 v1.20.0
88
89 Then increment any of the dotted-decimal components (v1.20.1 or
90 v1.21.0).
91
92 How to "declare()" a dotted-decimal version
93 use version 0.77; our $VERSION = version->declare("v1.2.3");
94
95 The "declare()" method always creates dotted-decimal version objects.
96 When used in a module, you must put it on the same line as "use
97 version" to ensure that $VERSION is read correctly by PAUSE and
98 installer tools. You should also add 'version' to the
99 'configure_requires' section of your module metadata file. See
100 instructions in ExtUtils::MakeMaker or Module::Build for details.
101
102 Important Note: Even if you pass in what looks like a decimal number
103 ("1.2"), a dotted-decimal will be created ("v1.200.0"). To avoid
104 confusion or unintentional errors on older Perls, follow these
105 guidelines:
106
107 · Always use a dotted-decimal with (at least) three components
108
109 · Always use a leading-v
110
111 · Always quote the version
112
113 If you really insist on using version.pm with an ordinary decimal
114 version, use "parse()" instead of declare. See the "PARSING AND
115 COMPARING VERSIONS" for details.
116
117 See also version::Internals for more on version number conversion,
118 quoting, calculated version numbers and declaring developer or "alpha"
119 version numbers.
120
122 If you need to compare version numbers, but can't be sure whether they
123 are expressed as numbers, strings, v-strings or version objects, then
124 you should use version.pm to parse them all into objects for
125 comparison.
126
127 How to "parse()" a version
128 The "parse()" method takes in anything that might be a version and
129 returns a corresponding version object, doing any necessary conversion
130 along the way.
131
132 · Dotted-decimal: bare v-strings (v1.2.3) and strings with more than
133 one decimal point and a leading 'v' ("v1.2.3"); NOTE you can
134 technically use a v-string or strings with a leading-v and only one
135 decimal point (v1.2 or "v1.2"), but you will confuse both yourself
136 and others.
137
138 · Decimal: regular decimal numbers (literal or in a string)
139
140 Some examples:
141
142 $variable version->parse($variable)
143 --------- -------------------------
144 1.23 v1.230.0
145 "1.23" v1.230.0
146 v1.23 v1.23.0
147 "v1.23" v1.23.0
148 "1.2.3" v1.2.3
149 "v1.2.3" v1.2.3
150
151 See version::Internals for more on version number conversion.
152
153 How to check for a legal version string
154 If you do not want to actually create a full blown version object, but
155 would still like to verify that a given string meets the criteria to be
156 parsed as a version, there are two helper functions that can be
157 employed directly:
158
159 "is_lax()"
160 The lax criteria corresponds to what is currently allowed by the
161 version parser. All of the following formats are acceptable for
162 dotted-decimal formats strings:
163
164 v1.2
165 1.2345.6
166 v1.23_4
167 1.2345
168 1.2345_01
169
170 "is_strict()"
171 If you want to limit youself to a much more narrow definition of
172 what a version string constitutes, "is_strict()" is limited to
173 version strings like the following list:
174
175 v1.234.5
176 2.3456
177
178 See version::Internals for details of the regular expressions that
179 define the legal version string forms, as well as how to use those
180 regular expressions in your own code if "is_lax()" and "is_strict()"
181 are not sufficient for your needs.
182
183 How to compare version objects
184 Version objects overload the "cmp" and "<=>" operators. Perl
185 automatically generates all of the other comparison operators based on
186 those two so all the normal logical comparisons will work.
187
188 if ( version->parse($v1) == version->parse($v2) ) {
189 # do stuff
190 }
191
192 If a version object is compared against a non-version object, the non-
193 object term will be converted to a version object using "parse()".
194 This may give surprising results:
195
196 $v1 = version->parse("v0.95.0");
197 $bool = $v1 < 0.96; # FALSE since 0.96 is v0.960.0
198
199 Always comparing to a version object will help avoid surprises:
200
201 $bool = $v1 < version->parse("v0.96.0"); # TRUE
202
203 Note that "alpha" version objects (where the version string contains a
204 trailing underscore segment) compare as less than the equivalent
205 version without an underscore:
206
207 $bool = version->parse("1.23_45") < version->parse("1.2345"); # TRUE
208
209 See version::Internals for more details on "alpha" versions.
210
212 is_alpha()
213 True if and only if the version object was created with a underscore,
214 e.g.
215
216 version->parse('1.002_03')->is_alpha; # TRUE
217 version->declare('1.2.3_4')->is_alpha; # TRUE
218
219 is_qv()
220 True only if the version object is a dotted-decimal version, e.g.
221
222 version->parse('v1.2.0')->is_qv; # TRUE
223 version->declare('v1.2')->is_qv; # TRUE
224 qv('1.2')->is_qv; # TRUE
225 version->parse('1.2')->is_qv; # FALSE
226
227 normal()
228 Returns a string with a standard 'normalized' dotted-decimal form with
229 a leading-v and at least 3 components.
230
231 version->declare('v1.2')->normal; # v1.2.0
232 version->parse('1.2')->normal; # v1.200.0
233
234 numify()
235 Returns a value representing the object in a pure decimal form without
236 trailing zeroes.
237
238 version->declare('v1.2')->numify; # 1.002
239 version->parse('1.2')->numify; # 1.2
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.200
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.12.4 2011-06-07 version(3pm)