This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
note that delete/exists ARRAY_ELEM should be avoided
[perl5.git] / lib / version.pod
CommitLineData
cb5772bb
RGS
1=head1 NAME
2
3version - Perl extension for Version Objects
4
5=head1 SYNOPSIS
6
692a467c 7 # Parsing version strings (decimal or dotted-decimal)
cb5772bb 8
692a467c
JP
9 use version 0.77; # get latest bug-fixes and API
10 $ver = version->parse($string)
cb5772bb 11
692a467c 12 # Declaring a dotted-decimal $VERSION (keep on one line!)
cb5772bb 13
692a467c
JP
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
317f7c8a 17
692a467c 18 # Declaring an old-style decimal $VERSION (use quotes!)
317f7c8a 19
692a467c
JP
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
317f7c8a 22
692a467c 23 # Comparing mixed version styles (decimals, dotted-decimals, objects)
317f7c8a 24
692a467c
JP
25 if ( version->parse($v1) == version->parse($v2) ) {
26 # do stuff
27 }
317f7c8a 28
692a467c 29 # Sorting mixed version styles
317f7c8a 30
692a467c 31 @ordered = sort { version->parse($a) <=> version->parse($b) } @list;
317f7c8a 32
692a467c 33=head1 DESCRIPTION
317f7c8a 34
692a467c
JP
35Version objects were added to Perl in 5.10. This module implements version
36objects for older version of Perl and provides the version object API for all
37versions of Perl. All previous releases before 0.74 are deprecated and should
38not 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
40set 0.77 as a minimum in your code, e.g.
317f7c8a 41
692a467c 42 use version 0.77; # even for Perl v.5.10.0
cb5772bb 43
692a467c 44=head1 TYPES OF VERSION OBJECTS
cb5772bb 45
692a467c
JP
46There are two different types of version objects, corresponding to the two
47different styles of versions in use:
cb5772bb 48
692a467c 49=over 2
cb5772bb 50
692a467c 51=item Decimal Versions
cb5772bb 52
692a467c
JP
53The classic floating-point number $VERSION. The advantage to this style is
54that you don't need to do anything special, just type a number (without
55quotes) into your source file.
cb5772bb 56
692a467c 57=item Dotted Decimal Versions
cb5772bb 58
692a467c
JP
59The more modern form of version assignment, with 3 (or potentially more)
60integers seperated by decimal points (e.g. v1.2.3). This is the form that
61Perl itself has used since 5.6.0 was released. The leading "v" is now
62strongly recommended for clarity, and will throw a warning in a future
63release if omitted.
cb5772bb
RGS
64
65=back
66
692a467c 67See L<VERSION OBJECT DETAILS> for further information.
cb5772bb 68
692a467c 69=head1 DECLARING VERSIONS
43eaf59d 70
692a467c
JP
71If you have a module that uses a decimal $VERSION (floating point), and you
72do not intend to ever change that, this module is not for you. There is
73nothing that version.pm gains you over a simple $VERSION assignment:
cb5772bb 74
692a467c 75 our $VERSION = 1.02;
cb5772bb 76
692a467c
JP
77Since Perl v5.10.0 includes the version.pm comparison logic anyways,
78you don't need to do anything at all.
cb5772bb 79
692a467c 80=head2 How to convert a module from decimal to dotted-decimal
cb5772bb 81
692a467c
JP
82If you have used a decimal $VERSION in the past and wish to switch to a
83dotted-decimal $VERSION, then you need to make a one-time conversion to
84the new format.
cb5772bb 85
692a467c
JP
86B<Important Note>: you must ensure that your new $VERSION is numerically
87greater than your current decimal $VERSION; this is not always obvious. First,
88convert your old decimal version (e.g. 1.02) to a normalized dotted-decimal
89form:
cb5772bb 90
692a467c
JP
91 $ perl -Mversion -e 'print version->parse("1.02")->normal'
92 v1.20.0
cb5772bb 93
692a467c 94Then increment any of the dotted-decimal components (v1.20.1 or v1.21.0).
cb5772bb 95
692a467c 96=head2 How to C<declare()> a dotted-decimal version
cb5772bb 97
692a467c 98 use version 0.77; our $VERSION = version->declare("v1.2.3");
cb5772bb 99
692a467c
JP
100The C<declare()> method always creates dotted-decimal version objects. When
101used in a module, you B<must> put it on the same line as "use version" to
102ensure that $VERSION is read correctly by PAUSE and installer tools. You
103should also add 'version' to the 'configure_requires' section of your
104module metadata file. See instructions in L<ExtUtils::MakeMaker> or
105L<Module::Build> for details.
cb5772bb 106
692a467c
JP
107B<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
109or unintentional errors on older Perls, follow these guidelines:
cb5772bb 110
692a467c 111=over 2
cb5772bb 112
692a467c 113=item *
cb5772bb 114
692a467c 115Always use a dotted-decimal with (at least) three components
92dcf8ce 116
692a467c 117=item *
92dcf8ce 118
692a467c 119Always use a leading-v
92dcf8ce 120
692a467c 121=item *
92dcf8ce 122
692a467c 123Always quote the version
92dcf8ce 124
cb5772bb
RGS
125=back
126
692a467c
JP
127If you really insist on using version.pm with an ordinary decimal version,
128use C<parse()> instead of declare. See the L<PARSING AND COMPARING VERSIONS>
129for details.
cb5772bb 130
692a467c
JP
131See also L<VERSION OBJECT DETAILS> for more on version number conversion,
132quoting, calculated version numbers and declaring developer or "alpha" version
133numbers.
cb5772bb 134
692a467c 135=head1 PARSING AND COMPARING VERSIONS
cb5772bb 136
692a467c
JP
137If you need to compare version numbers, but can't be sure whether they are
138expressed as numbers, strings, v-strings or version objects, then you can
139use version.pm to parse them all into objects for comparison.
cb5772bb 140
692a467c 141=head2 How to C<parse()> a version
cb5772bb 142
692a467c
JP
143The C<parse()> method takes in anything that might be a version and returns
144a corresponding version object, doing any necessary conversion along the way.
cb5772bb 145
692a467c 146=over 2
cb5772bb 147
692a467c 148=item *
cb5772bb 149
692a467c
JP
150Dotted-decimal: bare v-strings (v1.2.3) and strings with more than one
151decimal point and a leading 'v' ("v1.2.3"); NOTE you can technically use a
152v-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.
cb5772bb 154
692a467c 155=item *
cb5772bb 156
692a467c 157Decimal: regular decimal numbers (literal or in a string)
cb5772bb
RGS
158
159=back
160
692a467c 161Some examples:
cb5772bb 162
692a467c
JP
163 $variable version->parse($variable)
164 --------- -------------------------
165 1.23 v1.230.0
166 "1.23" v1.230.0
167 v1.23 v1.23.0
168 "v1.23" v1.23.0
169 "1.2.3" v1.2.3
170 "v1.2.3" v1.2.3
cb5772bb 171
692a467c 172See L<VERSION OBJECT DETAILS> for more on version number conversion.
cb5772bb 173
692a467c 174=head2 How to compare version objects
cb5772bb 175
692a467c
JP
176Version objects overload the C<cmp> and C<< E<lt>=E<gt> >> operators. Perl
177automatically generates all of the other comparison operators based on those
178two so all the normal logical comparisons will work.
cb5772bb 179
692a467c
JP
180 if ( version->parse($v1) == version->parse($v2) ) {
181 # do stuff
182 }
219bf418 183
692a467c
JP
184If a version object is compared against a non-version object, the non-object
185term will be converted to a version object using C<parse()>. This may give
219bf418
RGS
186surprising results:
187
692a467c
JP
188 $v1 = version->parse("v0.95.0");
189 $bool = $v1 < 0.96; # FALSE since 0.96 is v0.960.0
219bf418 190
692a467c 191Always comparing to a version object will help avoid surprises:
cb5772bb 192
692a467c 193 $bool = $v1 < version->parse("v0.96.0"); # TRUE
cb5772bb 194
692a467c 195=head1 VERSION OBJECT DETAILS
cb5772bb 196
692a467c 197=head2 Equivalence between Decimal and Dotted-Decimal Versions
cb5772bb 198
692a467c
JP
199When Perl 5.6.0 was released, the decision was made to provide a
200transformation between the old-style decimal versions and new-style
201dotted-decimal versions:
cb5772bb 202
692a467c
JP
203 5.6.0 == 5.006000
204 5.005_04 == 5.5.40
cb5772bb 205
692a467c
JP
206The floating point number is taken and split first on the single decimal
207place, then each group of three digits to the right of the decimal makes up
208the next digit, and so on until the number of significant digits is exhausted,
209B<plus> enough trailing zeros to reach the next multiple of three.
cb5772bb 210
692a467c
JP
211This was the method that version.pm adopted as well. Some examples may be
212helpful:
cb5772bb 213
692a467c
JP
214 equivalent
215 decimal zero-padded dotted-decimal
216 ------- ----------- --------------
217 1.2 1.200 v1.200.0
218 1.02 1.020 v1.20.0
219 1.002 1.002 v1.2.0
220 1.0023 1.002300 v1.2.300
221 1.00203 1.002030 v1.2.30
222 1.002003 1.002003 v1.2.3
cb5772bb 223
692a467c 224=head2 Quoting rules
cb5772bb
RGS
225
226Because of the nature of the Perl parsing and tokenizing routines,
227certain initialization values B<must> be quoted in order to correctly
692a467c
JP
228parse as the intended version, especially when using the L<declare> or
229L<qv> methods. While you do not have to quote decimal numbers when
230creating version objects, it is always safe to quote B<all> initial values
231when using version.pm methods, as this will ensure that what you type is
232what is used.
233
234Additionally, if you quote your initializer, then the quoted value that goes
235B<in> will be be exactly what comes B<out> when your $VERSION is printed
236(stringified). If you do not quote your value, Perl's normal numeric handling
237comes into play and you may not get back what you were expecting.
cb5772bb
RGS
238
239If you use a mathematic formula that resolves to a floating point number,
240you are dependent on Perl's conversion routines to yield the version you
241expect. You are pretty safe by dividing by a power of 10, for example,
242but other operations are not likely to be what you intend. For example:
243
244 $VERSION = version->new((qw$Revision: 1.4)[1]/10);
245 print $VERSION; # yields 0.14
246 $V2 = version->new(100/9); # Integer overflow in decimal number
247 print $V2; # yields something like 11.111.111.100
248
692a467c 249Perl 5.8.1 and beyond are able to automatically quote v-strings but
cb5772bb
RGS
250that is not possible in earlier versions of Perl. In other words:
251
252 $version = version->new("v2.5.4"); # legal in all versions of Perl
253 $newvers = version->new(v2.5.4); # legal only in Perl >= 5.8.1
254
43eaf59d
SP
255=head2 What about v-strings?
256
43eaf59d
SP
257There are two ways to enter v-strings: a bare number with two or more
258decimal points, or a bare number with one or more decimal points and a
259leading 'v' character (also bare). For example:
260
261 $vs1 = 1.2.3; # encoded as \1\2\3
262 $vs2 = v1.2; # encoded as \1\2
263
f34c6aaf 264However, the use of bare v-strings to initialize version objects is
692a467c
JP
265B<strongly> discouraged in all circumstances. Also, bare
266v-strings are not completely supported in any version of Perl prior to
2675.8.1.
f34c6aaf
JP
268
269If you insist on using bare v-strings with Perl > 5.6.0, be aware of the
270following limitations:
43eaf59d 271
f34c6aaf
JP
2721) For Perl releases 5.6.0 through 5.8.0, the v-string code merely guesses,
273based on some characteristics of v-strings. You B<must> use a three part
274version, e.g. 1.2.3 or v1.2.3 in order for this heuristic to be successful.
275
2762) For Perl releases 5.8.1 and later, v-strings have changed in the Perl
277core to be magical, which means that the version.pm code can automatically
278determine whether the v-string encoding was used.
cb5772bb 279
8cb289bd
RGS
2803) In all cases, a version created using v-strings will have a stringified
281form that has a leading 'v' character, for the simple reason that sometimes
282it is impossible to tell whether one was present initially.
283
692a467c 284=head2 Alpha versions
cb5772bb 285
692a467c
JP
286For module authors using CPAN, the convention has been to note unstable
287releases with an underscore in the version string. (See L<CPAN>.) version.pm
288follows this convention and alpha releases will test as being newer than the
289more recent stable release, and less than the next stable release. For
290dotted-decimal versions, only the last element may be separated by an
291underscore:
cb5772bb 292
692a467c
JP
293 # Declaring
294 use version 0.77; our $VERSION = version->declare("v1.2_3");
cb5772bb 295
692a467c
JP
296 # Parsing
297 $v1 = version->parse("v1.2_3");
298 $v1 = version->parse("1.002_003");
cb5772bb 299
692a467c 300=head1 OBJECT METHODS
cb5772bb 301
692a467c 302=head2 is_alpha()
cb5772bb 303
692a467c 304True if and only if the version object was created with a underscore, e.g.
cb5772bb 305
692a467c
JP
306 version->parse('1.002_03')->is_alpha; # TRUE
307 version->declare('1.2.3_4')->is_alpha; # TRUE
cb5772bb 308
692a467c 309=head2 is_qv()
cb5772bb 310
692a467c 311True only if the version object is a dotted-decimal version, e.g.
cb5772bb 312
692a467c
JP
313 version->parse('v1.2.0')->is_qv; # TRUE
314 version->declare('v1.2')->is_qv; # TRUE
315 qv('1.2')->is_qv; # TRUE
316 version->parse('1.2')->is_qv; # FALSE
cb5772bb 317
692a467c 318=head2 normal()
cb5772bb 319
692a467c
JP
320Returns a string with a standard 'normalized' dotted-decimal form with a
321leading-v and at least 3 components.
cb5772bb 322
692a467c
JP
323 version->declare('v1.2')->normal; # v1.2.0
324 version->parse('1.2')->normal; # v1.200.0
8cb289bd 325
692a467c 326=head2 numify()
8cb289bd 327
692a467c
JP
328Returns a value representing the object in a pure decimal form without
329trailing zeroes.
cb5772bb 330
692a467c
JP
331 version->declare('v1.2')->numify; # 1.002
332 version->parse('1.2')->numify; # 1.2
cb5772bb 333
692a467c 334=head2 stringify()
cb5772bb 335
692a467c
JP
336Returns a string that is as close to the original representation as possible.
337If the original representation was a numeric literal, it will be returned the
338way perl would normally represent it in a string. This method is used whenever
339a version object is interpolated into a string.
cb5772bb 340
692a467c
JP
341 version->declare('v1.2')->stringify; # v1.2
342 version->parse('1.200')->stringify; # 1.200
343 version->parse(1.02_30)->stringify; # 1.023
cb5772bb 344
692a467c 345=head1 EXPORTED FUNCTIONS
cb5772bb 346
692a467c 347=head2 qv()
cb5772bb 348
692a467c
JP
349This function is no longer recommended for use, but is maintained for
350compatibility with existing code. If you do not want to have it exported
351to your namespace, use this form:
cb5772bb 352
692a467c 353 use version 0.77 ();
cb5772bb
RGS
354
355=head1 AUTHOR
356
357John Peacock E<lt>jpeacock@cpan.orgE<gt>
358
359=head1 SEE ALSO
360
692a467c
JP
361L<version::Internal>.
362
cb5772bb
RGS
363L<perl>.
364
365=cut