3 version - Perl extension for Version Objects
7 # Parsing version strings (decimal or dotted-decimal)
9 use version 0.77; # get latest bug-fixes and API
10 $ver = version->parse($string)
12 # Declaring a dotted-decimal $VERSION (keep on one line!)
14 use version 0.77; our $VERSION = version->declare("v1.2.3"); # formal
15 use version 0.77; our $VERSION = qv("v1.2.3"); # shorthand
16 use version 0.77; our $VERSION = qv("v1.2_3"); # alpha
18 # Declaring an old-style decimal $VERSION (use quotes!)
20 use version 0.77; our $VERSION = version->parse("1.0203"); # formal
21 use version 0.77; our $VERSION = version->parse("1.02_03"); # alpha
23 # Comparing mixed version styles (decimals, dotted-decimals, objects)
25 if ( version->parse($v1) == version->parse($v2) ) {
29 # Sorting mixed version styles
31 @ordered = sort { version->parse($a) <=> version->parse($b) } @list;
35 Version objects were added to Perl in 5.10. This module implements version
36 objects for older version of Perl and provides the version object API for all
37 versions of Perl. All previous releases before 0.74 are deprecated and should
38 not be used due to incompatible API changes. Version 0.77 introduces the new
39 'parse' and 'declare' methods to standardize usage. You are strongly urged to
40 set 0.77 as a minimum in your code, e.g.
42 use version 0.77; # even for Perl v.5.10.0
44 =head1 TYPES OF VERSION OBJECTS
46 There are two different types of version objects, corresponding to the two
47 different styles of versions in use:
51 =item Decimal Versions
53 The classic floating-point number $VERSION. The advantage to this style is
54 that you don't need to do anything special, just type a number (without
55 quotes) into your source file.
57 =item Dotted Decimal Versions
59 The more modern form of version assignment, with 3 (or potentially more)
60 integers seperated by decimal points (e.g. v1.2.3). This is the form that
61 Perl itself has used since 5.6.0 was released. The leading "v" is now
62 strongly recommended for clarity, and will throw a warning in a future
67 See L<VERSION OBJECT DETAILS> for further information.
69 =head1 DECLARING VERSIONS
71 If you have a module that uses a decimal $VERSION (floating point), and you
72 do not intend to ever change that, this module is not for you. There is
73 nothing that version.pm gains you over a simple $VERSION assignment:
77 Since Perl v5.10.0 includes the version.pm comparison logic anyways,
78 you don't need to do anything at all.
80 =head2 How to convert a module from decimal to dotted-decimal
82 If you have used a decimal $VERSION in the past and wish to switch to a
83 dotted-decimal $VERSION, then you need to make a one-time conversion to
86 B<Important Note>: you must ensure that your new $VERSION is numerically
87 greater than your current decimal $VERSION; this is not always obvious. First,
88 convert your old decimal version (e.g. 1.02) to a normalized dotted-decimal
91 $ perl -Mversion -e 'print version->parse("1.02")->normal'
94 Then increment any of the dotted-decimal components (v1.20.1 or v1.21.0).
96 =head2 How to C<declare()> a dotted-decimal version
98 use version 0.77; our $VERSION = version->declare("v1.2.3");
100 The C<declare()> method always creates dotted-decimal version objects. When
101 used in a module, you B<must> put it on the same line as "use version" to
102 ensure that $VERSION is read correctly by PAUSE and installer tools. You
103 should also add 'version' to the 'configure_requires' section of your
104 module metadata file. See instructions in L<ExtUtils::MakeMaker> or
105 L<Module::Build> for details.
107 B<Important Note>: Even if you pass in what looks like a decimal number
108 ("1.2"), a dotted-decimal will be created ("v1.200.0"). To avoid confusion
109 or unintentional errors on older Perls, follow these guidelines:
115 Always use a dotted-decimal with (at least) three components
119 Always use a leading-v
123 Always quote the version
127 If you really insist on using version.pm with an ordinary decimal version,
128 use C<parse()> instead of declare. See the L<PARSING AND COMPARING VERSIONS>
131 See also L<VERSION OBJECT DETAILS> for more on version number conversion,
132 quoting, calculated version numbers and declaring developer or "alpha" version
135 =head1 PARSING AND COMPARING VERSIONS
137 If you need to compare version numbers, but can't be sure whether they are
138 expressed as numbers, strings, v-strings or version objects, then you can
139 use version.pm to parse them all into objects for comparison.
141 =head2 How to C<parse()> a version
143 The C<parse()> method takes in anything that might be a version and returns
144 a corresponding version object, doing any necessary conversion along the way.
150 Dotted-decimal: bare v-strings (v1.2.3) and strings with more than one
151 decimal point and a leading 'v' ("v1.2.3"); NOTE you can technically use a
152 v-string or strings with a leading-v and only one decimal point (v1.2 or
153 "v1.2"), but you will confuse both yourself and others.
157 Decimal: regular decimal numbers (literal or in a string)
163 $variable version->parse($variable)
164 --------- -------------------------
172 See L<VERSION OBJECT DETAILS> for more on version number conversion.
174 =head2 How to check for a legal version string
176 If you do not want to actually create a full blown version object, but
177 would still like to verify that a given string meets the criteria to
178 be parsed as a version, there are two helper functions that can be
185 The lax criteria corresponds to what is currently allowed by the
186 version parser. All of the following formats are acceptable
187 for dotted-decimal formats strings:
197 If you want to limit youself to a much more narrow definition of what
198 a version string constitutes, C<is_strict()> is limited to version
199 strings like the following list:
206 See L<version::Internals> for details of the regular expressions
207 that define the legal version string forms, as well as how to use
208 those regular expressions in your own code.
210 =head2 How to compare version objects
212 Version objects overload the C<cmp> and C<< E<lt>=E<gt> >> operators. Perl
213 automatically generates all of the other comparison operators based on those
214 two so all the normal logical comparisons will work.
216 if ( version->parse($v1) == version->parse($v2) ) {
220 If a version object is compared against a non-version object, the non-object
221 term will be converted to a version object using C<parse()>. This may give
224 $v1 = version->parse("v0.95.0");
225 $bool = $v1 < 0.96; # FALSE since 0.96 is v0.960.0
227 Always comparing to a version object will help avoid surprises:
229 $bool = $v1 < version->parse("v0.96.0"); # TRUE
231 =head1 VERSION OBJECT DETAILS
233 =head2 Equivalence between Decimal and Dotted-Decimal Versions
235 When Perl 5.6.0 was released, the decision was made to provide a
236 transformation between the old-style decimal versions and new-style
237 dotted-decimal versions:
242 The floating point number is taken and split first on the single decimal
243 place, then each group of three digits to the right of the decimal makes up
244 the next digit, and so on until the number of significant digits is exhausted,
245 B<plus> enough trailing zeros to reach the next multiple of three.
247 This was the method that version.pm adopted as well. Some examples may be
251 decimal zero-padded dotted-decimal
252 ------- ----------- --------------
256 1.0023 1.002300 v1.2.300
257 1.00203 1.002030 v1.2.30
258 1.002003 1.002003 v1.2.3
262 Because of the nature of the Perl parsing and tokenizing routines,
263 certain initialization values B<must> be quoted in order to correctly
264 parse as the intended version, especially when using the L<declare> or
265 L<qv> methods. While you do not have to quote decimal numbers when
266 creating version objects, it is always safe to quote B<all> initial values
267 when using version.pm methods, as this will ensure that what you type is
270 Additionally, if you quote your initializer, then the quoted value that goes
271 B<in> will be be exactly what comes B<out> when your $VERSION is printed
272 (stringified). If you do not quote your value, Perl's normal numeric handling
273 comes into play and you may not get back what you were expecting.
275 If you use a mathematic formula that resolves to a floating point number,
276 you are dependent on Perl's conversion routines to yield the version you
277 expect. You are pretty safe by dividing by a power of 10, for example,
278 but other operations are not likely to be what you intend. For example:
280 $VERSION = version->new((qw$Revision: 1.4)[1]/10);
281 print $VERSION; # yields 0.14
282 $V2 = version->new(100/9); # Integer overflow in decimal number
283 print $V2; # yields something like 11.111.111.100
285 Perl 5.8.1 and beyond are able to automatically quote v-strings but
286 that is not possible in earlier versions of Perl. In other words:
288 $version = version->new("v2.5.4"); # legal in all versions of Perl
289 $newvers = version->new(v2.5.4); # legal only in Perl >= 5.8.1
291 =head2 What about v-strings?
293 There are two ways to enter v-strings: a bare number with two or more
294 decimal points, or a bare number with one or more decimal points and a
295 leading 'v' character (also bare). For example:
297 $vs1 = 1.2.3; # encoded as \1\2\3
298 $vs2 = v1.2; # encoded as \1\2
300 However, the use of bare v-strings to initialize version objects is
301 B<strongly> discouraged in all circumstances. Also, bare
302 v-strings are not completely supported in any version of Perl prior to
305 If you insist on using bare v-strings with Perl > 5.6.0, be aware of the
306 following limitations:
308 1) For Perl releases 5.6.0 through 5.8.0, the v-string code merely guesses,
309 based on some characteristics of v-strings. You B<must> use a three part
310 version, e.g. 1.2.3 or v1.2.3 in order for this heuristic to be successful.
312 2) For Perl releases 5.8.1 and later, v-strings have changed in the Perl
313 core to be magical, which means that the version.pm code can automatically
314 determine whether the v-string encoding was used.
316 3) In all cases, a version created using v-strings will have a stringified
317 form that has a leading 'v' character, for the simple reason that sometimes
318 it is impossible to tell whether one was present initially.
320 =head2 Alpha versions
322 For module authors using CPAN, the convention has been to note unstable
323 releases with an underscore in the version string. (See L<CPAN>.) version.pm
324 follows this convention and alpha releases will test as being newer than the
325 more recent stable release, and less than the next stable release. For
326 dotted-decimal versions, only the last element may be separated by an
330 use version 0.77; our $VERSION = version->declare("v1.2_3");
333 $v1 = version->parse("v1.2_3");
334 $v1 = version->parse("1.002_003");
336 =head1 OBJECT METHODS
340 True if and only if the version object was created with a underscore, e.g.
342 version->parse('1.002_03')->is_alpha; # TRUE
343 version->declare('1.2.3_4')->is_alpha; # TRUE
347 True only if the version object is a dotted-decimal version, e.g.
349 version->parse('v1.2.0')->is_qv; # TRUE
350 version->declare('v1.2')->is_qv; # TRUE
351 qv('1.2')->is_qv; # TRUE
352 version->parse('1.2')->is_qv; # FALSE
356 Returns a string with a standard 'normalized' dotted-decimal form with a
357 leading-v and at least 3 components.
359 version->declare('v1.2')->normal; # v1.2.0
360 version->parse('1.2')->normal; # v1.200.0
364 Returns a value representing the object in a pure decimal form without
367 version->declare('v1.2')->numify; # 1.002
368 version->parse('1.2')->numify; # 1.2
372 Returns a string that is as close to the original representation as possible.
373 If the original representation was a numeric literal, it will be returned the
374 way perl would normally represent it in a string. This method is used whenever
375 a version object is interpolated into a string.
377 version->declare('v1.2')->stringify; # v1.2
378 version->parse('1.200')->stringify; # 1.200
379 version->parse(1.02_30)->stringify; # 1.023
381 =head1 EXPORTED FUNCTIONS
385 This function is no longer recommended for use, but is maintained for
386 compatibility with existing code. If you do not want to have it exported
387 to your namespace, use this form:
393 (Not exported by default)
395 This function takes a scalar argument and returns a boolean value indicating
396 whether the argument meets the "lax" rules for a version number.
400 (Not exported by default)
402 This function takes a scalar argument and returns a boolean value indicating
403 whether the argument meets the "strict" rules for a version number.
407 John Peacock E<lt>jpeacock@cpan.orgE<gt>
411 L<version::Internal>.