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