3 version::Internals - Perl extension for Version Objects
7 Overloaded version objects for all modern versions of Perl. This documents
8 the internal data representation and underlying code for version.pm. See
9 L<version.pod> for daily usage. This document is only useful for users
10 interested in the gory details.
12 =head1 WHAT IS A VERSION?
14 For the purposes of this module, a version "number" is a sequence of
15 positive integer values separated by one or more decimal points and
16 optionally a single underscore. This corresponds to what Perl itself
17 uses for a version, as well as extending the "version as number" that
18 is discussed in the various editions of the Camel book.
20 There are actually two distinct kinds of version objects:
24 =item Decimal Versions
26 Any version which "looks like a number", see L<Decimal Versions>. This
27 also includes versions with a single decimal point and a single embedded
28 underscore, see L<Alpha Versions>, even though these must be quoted
29 to preserve the underscore formatting.
31 =item Dotted-Decimal Versions
33 Also referred to as "Dotted-Integer", these contains more than one decimal
34 point and may have an optional embedded underscore, see L<Dotted-Decimal
35 Versions>. This is what is commonly used in most open source software as
36 the "external" version (the one used as part of the tag or tarfile name).
37 A leading 'v' character is now required and will warn if it missing.
41 Both of these methods will produce similar version objects, in that
42 the default stringification will yield the version L<Normal Form> only
45 $v = version->new(1.002); # 1.002, but compares like 1.2.0
46 $v = version->new(1.002003); # 1.002003
47 $v2 = version->new("v1.2.3"); # v1.2.3
49 In specific, version numbers initialized as L<Decimal Versions> will
50 stringify as they were originally created (i.e. the same string that was
51 passed to C<new()>. Version numbers initialized as L<Dotted-Decimal Versions>
52 will be stringified as L<Normal Form>.
54 =head2 Decimal Versions
56 These correspond to historical versions of Perl itself prior to 5.6.0,
57 as well as all other modules which follow the Camel rules for the
58 $VERSION scalar. A Decimal version is initialized with what looks like
59 a floating point number. Leading zeros B<are> significant and trailing
60 zeros are implied so that a minimum of three places is maintained
61 between subversions. What this means is that any subversion (digits
62 to the right of the decimal place) that contains less than three digits
63 will have trailing zeros added to make up the difference, but only for
64 purposes of comparison with other version objects. For example:
66 # Prints Equivalent to
67 $v = version->new( 1.2); # 1.2 v1.200.0
68 $v = version->new( 1.02); # 1.02 v1.20.0
69 $v = version->new( 1.002); # 1.002 v1.2.0
70 $v = version->new( 1.0023); # 1.0023 v1.2.300
71 $v = version->new( 1.00203); # 1.00203 v1.2.30
72 $v = version->new( 1.002003); # 1.002003 v1.2.3
74 All of the preceding examples are true whether or not the input value is
75 quoted. The important feature is that the input value contains only a
76 single decimal. See also L<Alpha Versions>.
78 IMPORTANT NOTE: As shown above, if your Decimal version contains more
79 than 3 significant digits after the decimal place, it will be split on
80 each multiple of 3, so 1.0003 is equivalent to v1.0.300, due to the need
81 to remain compatible with Perl's own 5.005_03 == 5.5.30 interpretation.
82 Any trailing zeros are ignored for mathematical comparison purposes.
84 =head2 Dotted-Decimal Versions
86 These are the newest form of versions, and correspond to Perl's own
87 version style beginning with 5.6.0. Starting with Perl 5.10.0,
88 and most likely Perl 6, this is likely to be the preferred form. This
89 method normally requires that the input parameter be quoted, although
90 Perl's after 5.8.1 can use v-strings as a special form of quoting, but
91 this is highly discouraged.
93 Unlike L<Decimal Versions>, Dotted-Decimal Versions have more than
94 a single decimal point, e.g.:
97 $v = version->new( "v1.200"); # v1.200.0
98 $v = version->new("v1.20.0"); # v1.20.0
99 $v = qv("v1.2.3"); # v1.2.3
100 $v = qv("1.2.3"); # v1.2.3
101 $v = qv("1.20"); # v1.20.0
103 In general, Dotted-Decimal Versions permit the greatest amount of freedom
104 to specify a version, whereas Decimal Versions enforce a certain
107 Just like L<Decimal Versions>, Dotted-Decimal Versions can be used as
110 =head2 Alpha Versions
112 For module authors using CPAN, the convention has been to note unstable
113 releases with an underscore in the version string. (See L<CPAN>.) version.pm
114 follows this convention and alpha releases will test as being newer than the
115 more recent stable release, and less than the next stable release. Only the
116 last element may be separated by an underscore:
119 use version 0.77; our $VERSION = version->declare("v1.2_3");
122 $v1 = version->parse("v1.2_3");
123 $v1 = version->parse("1.002_003");
125 Note that you B<must> quote the version when writing an alpha Decimal version.
126 The stringified form of Decimal versions will always be the same string that
127 was used to initialize the version object.
129 =head2 Regular Expressions for Version Parsing
131 A formalized definition of the legal forms for version strings is
132 included in the main F<version.pm> file. Primitives are included for
133 common elements, although they are scoped to the file so they are useful
134 for reference purposes only. There are two publicly accessible scalars
135 that can be used in other code (not exported):
139 =item C<$version::LAX>
141 This regexp covers all of the legal forms allowed under the current
142 version string parser. This is not to say that all of these forms
143 are recommended, and some of them can only be used when quoted.
151 The leading 'v' is optional if two or more decimals appear. If only
152 a single decimal is included, then the leading 'v' is required to
153 trigger the dotted-decimal parsing. A leading zero is permitted,
154 though not recommended except when quoted, because of the risk that
155 Perl will treat the number as octal. A trailing underscore plus one
156 or more digits denotes an alpha or development release (and must be
157 quoted to be parsed properly).
159 For decimal versions:
165 an integer portion, an optional decimal point, and optionally one or
166 more digits to the right of the decimal are all required. A trailing
167 underscore is permitted and a leading zero is permitted. Just like
168 the lax dotted-decimal version, quoting the values is required for
169 alpha/development forms to be parsed correctly.
171 =item C<$version::STRICT>
173 This regexp covers a much more limited set of formats and constitutes
174 the best practices for initializing version objects. Whether you choose
175 to employ decimal or dotted-decimal for is a personal preference however.
181 For dotted-decimal versions, a leading 'v' is required, with three or
182 more sub-versions of no more than three digits. A leading 0 (zero)
183 before the first sub-version (in the above example, '1') is also
188 For decimal versions, an integer portion (no leading 0), a decimal point,
189 and one or more digits to the right of the decimal are all required.
195 Both of the provided scalars are already compiled as regular expressions
196 and do not contain either anchors or implicit groupings, so they can be
197 included in your own regular expressions freely. For example, consider
203 (?:[ \t]+($version::STRICT))?
207 This would match a line of the form:
209 use Foo::Bar::Baz v1.2.3; # legal only in Perl 5.8.1+
211 where C<$PKGNAME> is another regular expression that defines the legal
212 forms for package names.
214 =head1 IMPLEMENTATION DETAILS
216 =head2 Equivalence between Decimal and Dotted-Decimal Versions
218 When Perl 5.6.0 was released, the decision was made to provide a
219 transformation between the old-style decimal versions and new-style
220 dotted-decimal versions:
225 The floating point number is taken and split first on the single decimal
226 place, then each group of three digits to the right of the decimal makes up
227 the next digit, and so on until the number of significant digits is exhausted,
228 B<plus> enough trailing zeros to reach the next multiple of three.
230 This was the method that version.pm adopted as well. Some examples may be
234 decimal zero-padded dotted-decimal
235 ------- ----------- --------------
239 1.0023 1.002300 v1.2.300
240 1.00203 1.002030 v1.2.30
241 1.002003 1.002003 v1.2.3
245 Because of the nature of the Perl parsing and tokenizing routines,
246 certain initialization values B<must> be quoted in order to correctly
247 parse as the intended version, especially when using the L<declare> or
248 L<qv> methods. While you do not have to quote decimal numbers when
249 creating version objects, it is always safe to quote B<all> initial values
250 when using version.pm methods, as this will ensure that what you type is
253 Additionally, if you quote your initializer, then the quoted value that goes
254 B<in> will be be exactly what comes B<out> when your $VERSION is printed
255 (stringified). If you do not quote your value, Perl's normal numeric handling
256 comes into play and you may not get back what you were expecting.
258 If you use a mathematic formula that resolves to a floating point number,
259 you are dependent on Perl's conversion routines to yield the version you
260 expect. You are pretty safe by dividing by a power of 10, for example,
261 but other operations are not likely to be what you intend. For example:
263 $VERSION = version->new((qw$Revision: 1.4)[1]/10);
264 print $VERSION; # yields 0.14
265 $V2 = version->new(100/9); # Integer overflow in decimal number
266 print $V2; # yields something like 11.111.111.100
268 Perl 5.8.1 and beyond are able to automatically quote v-strings but
269 that is not possible in earlier versions of Perl. In other words:
271 $version = version->new("v2.5.4"); # legal in all versions of Perl
272 $newvers = version->new(v2.5.4); # legal only in Perl >= 5.8.1
274 =head2 What about v-strings?
276 There are two ways to enter v-strings: a bare number with two or more
277 decimal points, or a bare number with one or more decimal points and a
278 leading 'v' character (also bare). For example:
280 $vs1 = 1.2.3; # encoded as \1\2\3
281 $vs2 = v1.2; # encoded as \1\2
283 However, the use of bare v-strings to initialize version objects is
284 B<strongly> discouraged in all circumstances. Also, bare
285 v-strings are not completely supported in any version of Perl prior to
288 If you insist on using bare v-strings with Perl > 5.6.0, be aware of the
289 following limitations:
291 1) For Perl releases 5.6.0 through 5.8.0, the v-string code merely guesses,
292 based on some characteristics of v-strings. You B<must> use a three part
293 version, e.g. 1.2.3 or v1.2.3 in order for this heuristic to be successful.
295 2) For Perl releases 5.8.1 and later, v-strings have changed in the Perl
296 core to be magical, which means that the version.pm code can automatically
297 determine whether the v-string encoding was used.
299 3) In all cases, a version created using v-strings will have a stringified
300 form that has a leading 'v' character, for the simple reason that sometimes
301 it is impossible to tell whether one was present initially.
303 =head2 Version Object Internals
305 version.pm provides an overloaded version object that is designed to both
306 encapsulate the author's intended $VERSION assignment as well as make it
307 completely natural to use those objects as if they were numbers (e.g. for
308 comparisons). To do this, a version object contains both the original
309 representation as typed by the author, as well as a parsed representation
310 to ease comparisons. Version objects employ L<overload> methods to
311 simplify code that needs to compare, print, etc the objects.
313 The internal structure of version objects is a blessed hash with several
317 'original' => 'v1.2.3_4',
332 A faithful representation of the value used to initialize this version
333 object. The only time this will not be precisely the same characters
334 that exist in the source file is if a short dotted-decimal version like
335 v1.2 was used (in which case it will contain 'v1.2'). This form is
336 B<STRONGLY> discouraged, in that it will confuse you and your users.
340 A boolean that denotes whether this is a decimal or dotted-decimal version.
345 A boolean that denotes whether this is an alpha version. NOTE: that the
346 underscore can can only appear in the last position. See L<is_alpha>.
350 An array of non-negative integers that is used for comparison purposes with
351 other version objects.
355 =head2 Replacement UNIVERSAL::VERSION
357 In addition to the version objects, this modules also replaces the core
358 UNIVERSAL::VERSION function with one that uses version objects for its
359 comparisons. The return from this operator is always the stringified form
360 as a simple scalar (i.e. not an object), but the warning message generated
361 includes either the stringified form or the normal form, depending on how
370 $VERSION = "v1.3.5"; # works with all Perl's (since it is quoted)
375 print $Foo::VERSION; # prints 1.2
377 print $Bar::VERSION; # prints 1.003005
380 print $@; # prints "foo version 10 required..."
381 eval "use foo 1.3.5; # work in Perl 5.6.1 or better
382 print $@; # prints "foo version 1.3.5 required..."
384 eval "use bar 1.3.6";
385 print $@; # prints "bar version 1.3.6 required..."
386 eval "use bar 1.004"; # note Decimal version
387 print $@; # prints "bar version 1.004 required..."
390 IMPORTANT NOTE: This may mean that code which searches for a specific
391 string (to determine whether a given module is available) may need to be
392 changed. It is always better to use the built-in comparison implicit in
393 C<use> or C<require>, rather than manually poking at C<< class->VERSION >>
394 and then doing a comparison yourself.
396 The replacement UNIVERSAL::VERSION, when used as a function, like this:
398 print $module->VERSION;
400 will also exclusively return the stringified form. See L<Stringification>
405 =head2 Using modules that use version.pm
407 As much as possible, the version.pm module remains compatible with all
408 current code. However, if your module is using a module that has defined
409 C<$VERSION> using the version class, there are a couple of things to be
410 aware of. For purposes of discussion, we will assume that we have the
411 following module installed:
414 use version; $VERSION = qv('1.2.2');
415 ...module code here...
420 =item Decimal versions always work
424 use Example 1.002003;
426 will always work correctly. The C<use> will perform an automatic
427 C<$VERSION> comparison using the floating point number given as the first
428 term after the module name (e.g. above 1.002.003). In this case, the
429 installed module is too old for the requested line, so you would see an
432 Example version 1.002003 (v1.2.3) required--this is only version 1.002002 (v1.2.2)...
434 =item Dotted-Decimal version work sometimes
436 With Perl >= 5.6.2, you can also use a line like this:
440 and it will again work (i.e. give the error message as above), even with
441 releases of Perl which do not normally support v-strings (see L<version/What about v-strings> below). This has to do with that fact that C<use> only checks
442 to see if the second term I<looks like a number> and passes that to the
443 replacement L<UNIVERSAL::VERSION>. This is not true in Perl 5.005_04,
444 however, so you are B<strongly encouraged> to always use a Decimal version
445 in your code, even for those versions of Perl which support the Dotted-Decimal
450 =head2 Object Methods
456 Like many OO interfaces, the new() method is used to initialize version
457 objects. If two arguments are passed to C<new()>, the B<second> one will be
458 used as if it were prefixed with "v". This is to support historical use of the
459 C<qw> operator with the CVS variable $Revision, which is automatically
460 incremented by CVS every time the file is committed to the repository.
462 In order to facilitate this feature, the following
463 code can be employed:
465 $VERSION = version->new(qw$Revision: 2.7 $);
467 and the version object will be created as if the following code
470 $VERSION = version->new("v2.7");
472 In other words, the version will be automatically parsed out of the
473 string, and it will be quoted to preserve the meaning CVS normally
474 carries for versions. The CVS $Revision$ increments differently from
475 Decimal versions (i.e. 1.10 follows 1.9), so it must be handled as if
476 it were a Dotted-Decimal Version.
478 A new version object can be created as a copy of an existing version
479 object, either as a class method:
481 $v1 = version->new(12.3);
482 $v2 = version->new($v1);
484 or as an object method:
486 $v1 = version->new(12.3);
487 $v2 = $v1->new(12.3);
489 and in each case, $v1 and $v2 will be identical. NOTE: if you create
490 a new object using an existing object like this:
494 the new object B<will not> be a clone of the existing object. In the
495 example case, $v2 will be an empty object of the same type as $v1.
503 An alternate way to create a new version object is through the exported
504 qv() sub. This is not strictly like other q? operators (like qq, qw),
505 in that the only delimiters supported are parentheses (or spaces). It is
506 the best way to initialize a short version without triggering the floating
507 point interpretation. For example:
509 $v1 = qv(1.2); # v1.2.0
510 $v2 = qv("1.2"); # also v1.2.0
512 As you can see, either a bare number or a quoted string can usually
513 be used interchangably, except in the case of a trailing zero, which
514 must be quoted to be converted properly. For this reason, it is strongly
515 recommended that all initializers to qv() be quoted strings instead of
518 To prevent the C<qv()> function from being exported to the caller's namespace,
519 either use version with a null parameter:
523 or just require version, like this:
527 Both methods will prevent the import() method from firing and exporting the
532 For the subsequent examples, the following three objects will be used:
534 $ver = version->new("1.2.3.4"); # see "Quoting Rules"
535 $alpha = version->new("1.2.3_4"); # see "Alpha Versions"
536 $nver = version->new(1.002); # see "Decimal Versions"
542 For any version object which is initialized with multiple decimal
543 places (either quoted or if possible v-string), or initialized using
544 the L<qv>() operator, the stringified representation is returned in
545 a normalized or reduced form (no extraneous zeros), and with a leading 'v':
547 print $ver->normal; # prints as v1.2.3.4
548 print $ver->stringify; # ditto
550 print $nver->normal; # prints as v1.2.0
551 print $nver->stringify; # prints as 1.002, see "Stringification"
553 In order to preserve the meaning of the processed version, the
554 normalized representation will always contain at least three sub terms.
555 In other words, the following is guaranteed to always be true:
557 my $newver = version->new($ver->stringify);
558 if ($newver eq $ver ) # always true
567 Although all mathematical operations on version objects are forbidden
568 by default, it is possible to retrieve a number which corresponds
569 to the version object through the use of the $obj->numify
570 method. For formatting purposes, when displaying a number which
571 corresponds a version object, all sub versions are assumed to have
572 three decimal places. So for example:
574 print $ver->numify; # prints 1.002003004
575 print $nver->numify; # prints 1.002
577 Unlike the stringification operator, there is never any need to append
578 trailing zeros to preserve the correct version value.
584 =item Stringification
586 The default stringification for version objects returns exactly the same
587 string as was used to create it, whether you used C<new()> or C<qv()>,
588 with one exception. The sole exception is if the object was created using
589 C<qv()> and the initializer did not have two decimal places or a leading
590 'v' (both optional), then the stringified form will have a leading 'v'
591 prepended, in order to support round-trip processing.
595 Initialized as Stringifies to
596 ============== ==============
597 version->new("1.2") 1.2
598 version->new("v1.2") v1.2
601 qv("1.2") v1.2 ### exceptional case
603 See also L<UNIVERSAL::VERSION>, as this also returns the stringified form
604 when used as a class method.
606 IMPORTANT NOTE: There is one exceptional cases shown in the above table
607 where the "initializer" is not stringwise equivalent to the stringified
608 representation. If you use the C<qv>() operator on a version without a
609 leading 'v' B<and> with only a single decimal place, the stringified output
610 will have a leading 'v', to preserve the sense. See the L<qv>() operator
613 IMPORTANT NOTE 2: Attempting to bypass the normal stringification rules by
614 manually applying L<numify>() and L<normal>() will sometimes yield
617 print version->new(version->new("v1.0")->numify)->normal; # v1.0.0
619 The reason for this is that the L<numify>() operator will turn "v1.0"
620 into the equivalent string "1.000000". Forcing the outer version object
621 to L<normal>() form will display the mathematically equivalent "v1.0.0".
623 As the example in L<new>() shows, you can always create a copy of an
624 existing version object with the same value by the very compact:
628 and be assured that both C<$v1> and C<$v2> will be completely equivalent,
629 down to the same internal representation as well as stringification.
635 =item Comparison operators
637 Both C<cmp> and C<E<lt>=E<gt>> operators perform the same comparison between
638 terms (upgrading to a version object automatically). Perl automatically
639 generates all of the other comparison operators based on those two.
640 In addition to the obvious equalities listed below, appending a single
641 trailing 0 term does not change the value of a version for comparison
642 purposes. In other words "v1.2" and "1.2.0" will compare as identical.
644 For example, the following relations hold:
646 As Number As String Truth Value
647 ------------- ---------------- -----------
648 $ver > 1.0 $ver gt "1.0" true
649 $ver < 2.5 $ver lt true
650 $ver != 1.3 $ver ne "1.3" true
651 $ver == 1.2 $ver eq "1.2" false
652 $ver == 1.2.3.4 $ver eq "1.2.3.4" see discussion below
654 It is probably best to chose either the Decimal notation or the string
655 notation and stick with it, to reduce confusion. Perl6 version objects
656 B<may> only support Decimal comparisons. See also L<Quoting Rules>.
658 WARNING: Comparing version with unequal numbers of decimal points (whether
659 explicitly or implicitly initialized), may yield unexpected results at
660 first glance. For example, the following inequalities hold:
662 version->new(0.96) > version->new(0.95); # 0.960.0 > 0.950.0
663 version->new("0.96.1") < version->new(0.95); # 0.096.1 < 0.950.0
665 For this reason, it is best to use either exclusively L<Decimal Versions> or
666 L<Dotted-Decimal Versions> with multiple decimal points.
672 =item Logical Operators
674 If you need to test whether a version object
675 has been initialized, you can simply test it directly:
677 $vobj = version->new($something);
678 if ( $vobj ) # true only if $something was non-blank
680 You can also test whether a version object is an alpha version, for
681 example to prevent the use of some feature not present in the main
684 $vobj = version->new("1.2_3"); # MUST QUOTE
686 if ( $vobj->is_alpha ) # True
692 John Peacock E<lt>jpeacock@cpan.orgE<gt>