This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
don't depend on threads to do a watchdog when testing threads
[perl5.git] / lib / version.pod
1 =head1 NAME
2
3 version - Perl extension for Version Objects
4
5 =head1 SYNOPSIS
6
7   # Parsing version strings (decimal or dotted-decimal)
8
9   use version 0.77; # get latest bug-fixes and API
10   $ver = version->parse($string)
11
12   # Declaring a dotted-decimal $VERSION (keep on one line!)
13
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
17
18   # Declaring an old-style decimal $VERSION (use quotes!)
19
20   our $VERSION = "1.0203";                                     # recommended
21   use version 0.77; our $VERSION = version->parse("1.0203");   # formal
22   use version 0.77; our $VERSION = version->parse("1.02_03");  # alpha
23
24   # Comparing mixed version styles (decimals, dotted-decimals, objects)
25
26   if ( version->parse($v1) == version->parse($v2) ) {
27     # do stuff
28   }
29
30   # Sorting mixed version styles
31
32   @ordered = sort { version->parse($a) <=> version->parse($b) } @list;
33
34 =head1 DESCRIPTION
35
36 Version objects were added to Perl in 5.10.  This module implements version
37 objects for older version of Perl and provides the version object API for all
38 versions of Perl.  All previous releases before 0.74 are deprecated and should
39 not be used due to incompatible API changes.  Version 0.77 introduces the new
40 'parse' and 'declare' methods to standardize usage.  You are strongly urged to
41 set 0.77 as a minimum in your code, e.g.
42
43   use version 0.77; # even for Perl v.5.10.0
44
45 =head1 TYPES OF VERSION OBJECTS
46
47 There are two different types of version objects, corresponding to the two
48 different styles of versions in use:
49
50 =over 2
51
52 =item Decimal Versions
53
54 The classic floating-point number $VERSION.  The advantage to this style is
55 that you don't need to do anything special, just type a number into your
56 source file.  Quoting is recommended, as it ensures that trailing zeroes
57 ("1.50") are preserved in any warnings or other output.
58
59 =item Dotted Decimal Versions
60
61 The more modern form of version assignment, with 3 (or potentially more)
62 integers separated by decimal points (e.g. v1.2.3).  This is the form that
63 Perl itself has used since 5.6.0 was released.  The leading "v" is now
64 strongly recommended for clarity, and will throw a warning in a future
65 release if omitted.
66
67 =back
68
69 =head1 DECLARING VERSIONS
70
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:
74
75   our $VERSION = "1.02";
76
77 Since Perl v5.10.0 includes the version.pm comparison logic anyways,
78 you don't need to do anything at all.
79
80 =head2 How to convert a module from decimal to dotted-decimal
81
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
84 the new format.
85
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
89 form:
90
91   $ perl -Mversion -e 'print version->parse("1.02")->normal'
92   v1.20.0
93
94 Then increment any of the dotted-decimal components (v1.20.1 or v1.21.0).
95
96 =head2 How to C<declare()> a dotted-decimal version
97
98   use version 0.77; our $VERSION = version->declare("v1.2.3");
99
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.
106
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:
110
111 =over 2
112
113 =item *
114
115 Always use a dotted-decimal with (at least) three components
116
117 =item *
118
119 Always use a leading-v
120
121 =item *
122
123 Always quote the version
124
125 =back
126
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>
129 for details.
130
131 See also L<version::Internals> for more on version number conversion,
132 quoting, calculated version numbers and declaring developer or "alpha" version
133 numbers.
134
135 =head1 PARSING AND COMPARING VERSIONS
136
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 should
139 use version.pm to parse them all into objects for comparison.
140
141 =head2 How to C<parse()> a version
142
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.
145
146 =over 2
147
148 =item *
149
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.
154
155 =item *
156
157 Decimal: regular decimal numbers (literal or in a string)
158
159 =back
160
161 Some examples:
162
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
171
172 See L<version::Internals> for more on version number conversion.
173
174 =head2 How to check for a legal version string
175
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
179 employed directly:
180
181 =over 4
182
183 =item C<is_lax()>
184
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:
188
189     v1.2
190     1.2345.6
191     v1.23_4
192     1.2345
193     1.2345_01
194
195 =item C<is_strict()>
196
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:
200
201     v1.234.5
202     2.3456
203
204 =back
205
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 if C<is_lax()> and
209 C<is_strict()> are not sufficient for your needs.
210
211 =head2 How to compare version objects
212
213 Version objects overload the C<cmp> and C<< <=> >> operators.  Perl
214 automatically generates all of the other comparison operators based on those
215 two so all the normal logical comparisons will work.
216
217   if ( version->parse($v1) == version->parse($v2) ) {
218     # do stuff
219   }
220
221 If a version object is compared against a non-version object, the non-object
222 term will be converted to a version object using C<parse()>.  This may give
223 surprising results:
224
225   $v1 = version->parse("v0.95.0");
226   $bool = $v1 < 0.96; # FALSE since 0.96 is v0.960.0
227
228 Always comparing to a version object will help avoid surprises:
229
230   $bool = $v1 < version->parse("v0.96.0"); # TRUE
231
232 Note that "alpha" version objects (where the version string contains
233 a trailing underscore segment) compare as less than the equivalent
234 version without an underscore:
235
236   $bool = version->parse("1.23_45") < version->parse("1.2345"); # TRUE
237
238 See L<version::Internals> for more details on "alpha" versions.
239
240 =head1 OBJECT METHODS
241
242 =head2 is_alpha()
243
244 True if and only if the version object was created with a underscore, e.g.
245
246   version->parse('1.002_03')->is_alpha;  # TRUE
247   version->declare('1.2.3_4')->is_alpha; # TRUE
248
249 =head2 is_qv()
250
251 True only if the version object is a dotted-decimal version, e.g.
252
253   version->parse('v1.2.0')->is_qv;        # TRUE
254   version->declare('v1.2')->is_qv;       # TRUE
255   qv('1.2')->is_qv;                      # TRUE
256   version->parse('1.2')->is_qv;          # FALSE
257
258 =head2 normal()
259
260 Returns a string with a standard 'normalized' dotted-decimal form with a
261 leading-v and at least 3 components.
262
263  version->declare('v1.2')->normal;  # v1.2.0
264  version->parse('1.2')->normal;     # v1.200.0
265
266 =head2 numify()
267
268 Returns a value representing the object in a pure decimal form without
269 trailing zeroes.
270
271  version->declare('v1.2')->numify;  # 1.002
272  version->parse('1.2')->numify;     # 1.2
273
274 =head2 stringify()
275
276 Returns a string that is as close to the original representation as possible.
277 If the original representation was a numeric literal, it will be returned the
278 way perl would normally represent it in a string.  This method is used whenever
279 a version object is interpolated into a string.
280
281  version->declare('v1.2')->stringify;    # v1.2
282  version->parse('1.200')->stringify;     # 1.200
283  version->parse(1.02_30)->stringify;     # 1.023
284
285 =head1 EXPORTED FUNCTIONS
286
287 =head2 qv()
288
289 This function is no longer recommended for use, but is maintained for
290 compatibility with existing code.  If you do not want to have it exported
291 to your namespace, use this form:
292
293   use version 0.77 ();
294
295 =head2 is_lax()
296
297 (Not exported by default)
298
299 This function takes a scalar argument and returns a boolean value indicating
300 whether the argument meets the "lax" rules for a version number.  Leading and
301 trailing spaces are not allowed.
302
303 =head2 is_strict()
304
305 (Not exported by default)
306
307 This function takes a scalar argument and returns a boolean value indicating
308 whether the argument meets the "strict" rules for a version number.  Leading
309 and trailing spaces are not allowed.
310
311 =head1 AUTHOR
312
313 John Peacock E<lt>jpeacock@cpan.orgE<gt>
314
315 =head1 SEE ALSO
316
317 L<version::Internals>.
318
319 L<perl>.
320
321 =cut