1version::Internals(3) User Contributed Perl Documentationversion::Internals(3)
2
3
4
6 version::Internals - Perl extension for Version Objects
7
9 Overloaded version objects for all modern versions of Perl. This
10 documents the internal data representation and underlying code for
11 version.pm. See version.pod for daily usage. This document is only
12 useful for users interested in the gory details.
13
15 For the purposes of this module, a version "number" is a sequence of
16 positive integer values separated by one or more decimal points and
17 optionally a single underscore. This corresponds to what Perl itself
18 uses for a version, as well as extending the "version as number" that
19 is discussed in the various editions of the Camel book.
20
21 There are actually two distinct kinds of version objects:
22
23 Decimal versions
24 Any version which "looks like a number", see "Decimal Versions".
25 This also includes versions with a single decimal point and a
26 single embedded underscore, see "Alpha Versions", even though these
27 must be quoted to preserve the underscore formatting.
28
29 Dotted-Decimal versions
30 Also referred to as "Dotted-Integer", these contains more than one
31 decimal point and may have an optional embedded underscore, see
32 Dotted-Decimal Versions. This is what is commonly used in most
33 open source software as the "external" version (the one used as
34 part of the tag or tarfile name). A leading 'v' character is now
35 required and will warn if it missing.
36
37 Both of these methods will produce similar version objects, in that the
38 default stringification will yield the version "Normal Form" only if
39 required:
40
41 $v = version->new(1.002); # 1.002, but compares like 1.2.0
42 $v = version->new(1.002003); # 1.002003
43 $v2 = version->new("v1.2.3"); # v1.2.3
44
45 In specific, version numbers initialized as "Decimal Versions" will
46 stringify as they were originally created (i.e. the same string that
47 was passed to new(). Version numbers initialized as "Dotted-Decimal
48 Versions" will be stringified as "Normal Form".
49
50 Decimal Versions
51 These correspond to historical versions of Perl itself prior to 5.6.0,
52 as well as all other modules which follow the Camel rules for the
53 $VERSION scalar. A Decimal version is initialized with what looks like
54 a floating point number. Leading zeros are significant and trailing
55 zeros are implied so that a minimum of three places is maintained
56 between subversions. What this means is that any subversion (digits to
57 the right of the decimal place) that contains less than three digits
58 will have trailing zeros added to make up the difference, but only for
59 purposes of comparison with other version objects. For example:
60
61 # Prints Equivalent to
62 $v = version->new( 1.2); # 1.2 v1.200.0
63 $v = version->new( 1.02); # 1.02 v1.20.0
64 $v = version->new( 1.002); # 1.002 v1.2.0
65 $v = version->new( 1.0023); # 1.0023 v1.2.300
66 $v = version->new( 1.00203); # 1.00203 v1.2.30
67 $v = version->new( 1.002003); # 1.002003 v1.2.3
68
69 All of the preceding examples are true whether or not the input value
70 is quoted. The important feature is that the input value contains only
71 a single decimal. See also "Alpha Versions".
72
73 IMPORTANT NOTE: As shown above, if your Decimal version contains more
74 than 3 significant digits after the decimal place, it will be split on
75 each multiple of 3, so 1.0003 is equivalent to v1.0.300, due to the
76 need to remain compatible with Perl's own 5.005_03 == 5.5.30
77 interpretation. Any trailing zeros are ignored for mathematical
78 comparison purposes.
79
80 Dotted-Decimal Versions
81 These are the newest form of versions, and correspond to Perl's own
82 version style beginning with 5.6.0. Starting with Perl 5.10.0, and
83 most likely Perl 6, this is likely to be the preferred form. This
84 method normally requires that the input parameter be quoted, although
85 Perl's after 5.8.1 can use v-strings as a special form of quoting, but
86 this is highly discouraged.
87
88 Unlike "Decimal Versions", Dotted-Decimal Versions have more than a
89 single decimal point, e.g.:
90
91 # Prints
92 $v = version->new( "v1.200"); # v1.200.0
93 $v = version->new("v1.20.0"); # v1.20.0
94 $v = qv("v1.2.3"); # v1.2.3
95 $v = qv("1.2.3"); # v1.2.3
96 $v = qv("1.20"); # v1.20.0
97
98 In general, Dotted-Decimal Versions permit the greatest amount of
99 freedom to specify a version, whereas Decimal Versions enforce a
100 certain uniformity.
101
102 Just like "Decimal Versions", Dotted-Decimal Versions can be used as
103 "Alpha Versions".
104
105 Alpha Versions
106 For module authors using CPAN, the convention has been to note unstable
107 releases with an underscore in the version string. (See CPAN.)
108 version.pm follows this convention and alpha releases will test as
109 being newer than the more recent stable release, and less than the next
110 stable release. Only the last element may be separated by an
111 underscore:
112
113 # Declaring
114 use version 0.77; our $VERSION = version->declare("v1.2_3");
115
116 # Parsing
117 $v1 = version->parse("v1.2_3");
118 $v1 = version->parse("1.002_003");
119
120 Note that you must quote the version when writing an alpha Decimal
121 version. The stringified form of Decimal versions will always be the
122 same string that was used to initialize the version object.
123
124 Regular Expressions for Version Parsing
125 A formalized definition of the legal forms for version strings is
126 included in the "version::regex" class. Primitives are included for
127 common elements, although they are scoped to the file so they are
128 useful for reference purposes only. There are two publicly accessible
129 scalars that can be used in other code (not exported):
130
131 $version::LAX
132 This regexp covers all of the legal forms allowed under the current
133 version string parser. This is not to say that all of these forms
134 are recommended, and some of them can only be used when quoted.
135
136 For dotted decimals:
137
138 v1.2
139 1.2345.6
140 v1.23_4
141
142 The leading 'v' is optional if two or more decimals appear. If
143 only a single decimal is included, then the leading 'v' is required
144 to trigger the dotted-decimal parsing. A leading zero is
145 permitted, though not recommended except when quoted, because of
146 the risk that Perl will treat the number as octal. A trailing
147 underscore plus one or more digits denotes an alpha or development
148 release (and must be quoted to be parsed properly).
149
150 For decimal versions:
151
152 1
153 1.2345
154 1.2345_01
155
156 an integer portion, an optional decimal point, and optionally one
157 or more digits to the right of the decimal are all required. A
158 trailing underscore is permitted and a leading zero is permitted.
159 Just like the lax dotted-decimal version, quoting the values is
160 required for alpha/development forms to be parsed correctly.
161
162 $version::STRICT
163 This regexp covers a much more limited set of formats and
164 constitutes the best practices for initializing version objects.
165 Whether you choose to employ decimal or dotted-decimal for is a
166 personal preference however.
167
168 v1.234.5
169 For dotted-decimal versions, a leading 'v' is required, with
170 three or more sub-versions of no more than three digits. A
171 leading 0 (zero) before the first sub-version (in the above
172 example, '1') is also prohibited.
173
174 2.3456
175 For decimal versions, an integer portion (no leading 0), a
176 decimal point, and one or more digits to the right of the
177 decimal are all required.
178
179 Both of the provided scalars are already compiled as regular
180 expressions and do not contain either anchors or implicit groupings, so
181 they can be included in your own regular expressions freely. For
182 example, consider the following code:
183
184 ($pkg, $ver) =~ /
185 ^[ \t]*
186 use [ \t]+($PKGNAME)
187 (?:[ \t]+($version::STRICT))?
188 [ \t]*;
189 /x;
190
191 This would match a line of the form:
192
193 use Foo::Bar::Baz v1.2.3; # legal only in Perl 5.8.1+
194
195 where $PKGNAME is another regular expression that defines the legal
196 forms for package names.
197
199 Equivalence between Decimal and Dotted-Decimal Versions
200 When Perl 5.6.0 was released, the decision was made to provide a
201 transformation between the old-style decimal versions and new-style
202 dotted-decimal versions:
203
204 5.6.0 == 5.006000
205 5.005_04 == 5.5.40
206
207 The floating point number is taken and split first on the single
208 decimal place, then each group of three digits to the right of the
209 decimal makes up the next digit, and so on until the number of
210 significant digits is exhausted, plus enough trailing zeros to reach
211 the next multiple of three.
212
213 This was the method that version.pm adopted as well. Some examples may
214 be helpful:
215
216 equivalent
217 decimal zero-padded dotted-decimal
218 ------- ----------- --------------
219 1.2 1.200 v1.200.0
220 1.02 1.020 v1.20.0
221 1.002 1.002 v1.2.0
222 1.0023 1.002300 v1.2.300
223 1.00203 1.002030 v1.2.30
224 1.002003 1.002003 v1.2.3
225
226 Quoting Rules
227 Because of the nature of the Perl parsing and tokenizing routines,
228 certain initialization values must be quoted in order to correctly
229 parse as the intended version, especially when using the "declare" or
230 "qv()" methods. While you do not have to quote decimal numbers when
231 creating version objects, it is always safe to quote all initial values
232 when using version.pm methods, as this will ensure that what you type
233 is what is used.
234
235 Additionally, if you quote your initializer, then the quoted value that
236 goes in will be exactly what comes out when your $VERSION is printed
237 (stringified). If you do not quote your value, Perl's normal numeric
238 handling comes into play and you may not get back what you were
239 expecting.
240
241 If you use a mathematic formula that resolves to a floating point
242 number, you are dependent on Perl's conversion routines to yield the
243 version you expect. You are pretty safe by dividing by a power of 10,
244 for example, but other operations are not likely to be what you intend.
245 For example:
246
247 $VERSION = version->new((qw$Revision: 1.4)[1]/10);
248 print $VERSION; # yields 0.14
249 $V2 = version->new(100/9); # Integer overflow in decimal number
250 print $V2; # yields something like 11.111.111.100
251
252 Perl 5.8.1 and beyond are able to automatically quote v-strings but
253 that is not possible in earlier versions of Perl. In other words:
254
255 $version = version->new("v2.5.4"); # legal in all versions of Perl
256 $newvers = version->new(v2.5.4); # legal only in Perl >= 5.8.1
257
258 What about v-strings?
259 There are two ways to enter v-strings: a bare number with two or more
260 decimal points, or a bare number with one or more decimal points and a
261 leading 'v' character (also bare). For example:
262
263 $vs1 = 1.2.3; # encoded as \1\2\3
264 $vs2 = v1.2; # encoded as \1\2
265
266 However, the use of bare v-strings to initialize version objects is
267 strongly discouraged in all circumstances. Also, bare v-strings are
268 not completely supported in any version of Perl prior to 5.8.1.
269
270 If you insist on using bare v-strings with Perl > 5.6.0, be aware of
271 the following limitations:
272
273 1) For Perl releases 5.6.0 through 5.8.0, the v-string code merely
274 guesses, based on some characteristics of v-strings. You must use a
275 three part version, e.g. 1.2.3 or v1.2.3 in order for this heuristic to
276 be successful.
277
278 2) For Perl releases 5.8.1 and later, v-strings have changed in the
279 Perl core to be magical, which means that the version.pm code can
280 automatically determine whether the v-string encoding was used.
281
282 3) In all cases, a version created using v-strings will have a
283 stringified form that has a leading 'v' character, for the simple
284 reason that sometimes it is impossible to tell whether one was present
285 initially.
286
287 Version Object Internals
288 version.pm provides an overloaded version object that is designed to
289 both encapsulate the author's intended $VERSION assignment as well as
290 make it completely natural to use those objects as if they were numbers
291 (e.g. for comparisons). To do this, a version object contains both the
292 original representation as typed by the author, as well as a parsed
293 representation to ease comparisons. Version objects employ overload
294 methods to simplify code that needs to compare, print, etc the objects.
295
296 The internal structure of version objects is a blessed hash with
297 several components:
298
299 bless( {
300 'original' => 'v1.2.3_4',
301 'alpha' => 1,
302 'qv' => 1,
303 'version' => [
304 1,
305 2,
306 3,
307 4
308 ]
309 }, 'version' );
310
311 original
312 A faithful representation of the value used to initialize this
313 version object. The only time this will not be precisely the same
314 characters that exist in the source file is if a short dotted-
315 decimal version like v1.2 was used (in which case it will contain
316 'v1.2'). This form is STRONGLY discouraged, in that it will
317 confuse you and your users.
318
319 qv A boolean that denotes whether this is a decimal or dotted-decimal
320 version. See "is_qv()" in version.
321
322 alpha
323 A boolean that denotes whether this is an alpha version. NOTE:
324 that the underscore can only appear in the last position. See
325 "is_alpha()" in version.
326
327 version
328 An array of non-negative integers that is used for comparison
329 purposes with other version objects.
330
331 Replacement UNIVERSAL::VERSION
332 In addition to the version objects, this modules also replaces the core
333 UNIVERSAL::VERSION function with one that uses version objects for its
334 comparisons. The return from this operator is always the stringified
335 form as a simple scalar (i.e. not an object), but the warning message
336 generated includes either the stringified form or the normal form,
337 depending on how it was called.
338
339 For example:
340
341 package Foo;
342 $VERSION = 1.2;
343
344 package Bar;
345 $VERSION = "v1.3.5"; # works with all Perl's (since it is quoted)
346
347 package main;
348 use version;
349
350 print $Foo::VERSION; # prints 1.2
351
352 print $Bar::VERSION; # prints 1.003005
353
354 eval "use foo 10";
355 print $@; # prints "foo version 10 required..."
356 eval "use foo 1.3.5; # work in Perl 5.6.1 or better
357 print $@; # prints "foo version 1.3.5 required..."
358
359 eval "use bar 1.3.6";
360 print $@; # prints "bar version 1.3.6 required..."
361 eval "use bar 1.004"; # note Decimal version
362 print $@; # prints "bar version 1.004 required..."
363
364 IMPORTANT NOTE: This may mean that code which searches for a specific
365 string (to determine whether a given module is available) may need to
366 be changed. It is always better to use the built-in comparison
367 implicit in "use" or "require", rather than manually poking at
368 "class->VERSION" and then doing a comparison yourself.
369
370 The replacement UNIVERSAL::VERSION, when used as a function, like this:
371
372 print $module->VERSION;
373
374 will also exclusively return the stringified form. See
375 "Stringification" for more details.
376
378 Using modules that use version.pm
379 As much as possible, the version.pm module remains compatible with all
380 current code. However, if your module is using a module that has
381 defined $VERSION using the version class, there are a couple of things
382 to be aware of. For purposes of discussion, we will assume that we
383 have the following module installed:
384
385 package Example;
386 use version; $VERSION = qv('1.2.2');
387 ...module code here...
388 1;
389
390 Decimal versions always work
391 Code of the form:
392
393 use Example 1.002003;
394
395 will always work correctly. The "use" will perform an automatic
396 $VERSION comparison using the floating point number given as the
397 first term after the module name (e.g. above 1.002.003). In this
398 case, the installed module is too old for the requested line, so
399 you would see an error like:
400
401 Example version 1.002003 (v1.2.3) required--this is only version 1.002002 (v1.2.2)...
402
403 Dotted-Decimal version work sometimes
404 With Perl >= 5.6.2, you can also use a line like this:
405
406 use Example 1.2.3;
407
408 and it will again work (i.e. give the error message as above), even
409 with releases of Perl which do not normally support v-strings (see
410 "What about v-strings?" above). This has to do with that fact that
411 "use" only checks to see if the second term looks like a number and
412 passes that to the replacement UNIVERSAL::VERSION. This is not
413 true in Perl 5.005_04, however, so you are strongly encouraged to
414 always use a Decimal version in your code, even for those versions
415 of Perl which support the Dotted-Decimal version.
416
417 Object Methods
418 new()
419 Like many OO interfaces, the new() method is used to initialize
420 version objects. If two arguments are passed to new(), the second
421 one will be used as if it were prefixed with "v". This is to
422 support historical use of the "qw" operator with the CVS variable
423 $Revision, which is automatically incremented by CVS every time the
424 file is committed to the repository.
425
426 In order to facilitate this feature, the following code can be
427 employed:
428
429 $VERSION = version->new(qw$Revision: 2.7 $);
430
431 and the version object will be created as if the following code
432 were used:
433
434 $VERSION = version->new("v2.7");
435
436 In other words, the version will be automatically parsed out of the
437 string, and it will be quoted to preserve the meaning CVS normally
438 carries for versions. The CVS $Revision$ increments differently
439 from Decimal versions (i.e. 1.10 follows 1.9), so it must be
440 handled as if it were a Dotted-Decimal Version.
441
442 A new version object can be created as a copy of an existing
443 version object, either as a class method:
444
445 $v1 = version->new(12.3);
446 $v2 = version->new($v1);
447
448 or as an object method:
449
450 $v1 = version->new(12.3);
451 $v2 = $v1->new(12.3);
452
453 and in each case, $v1 and $v2 will be identical. NOTE: if you
454 create a new object using an existing object like this:
455
456 $v2 = $v1->new();
457
458 the new object will not be a clone of the existing object. In the
459 example case, $v2 will be an empty object of the same type as $v1.
460
461 qv()
462 An alternate way to create a new version object is through the
463 exported qv() sub. This is not strictly like other q? operators
464 (like qq, qw), in that the only delimiters supported are
465 parentheses (or spaces). It is the best way to initialize a short
466 version without triggering the floating point interpretation. For
467 example:
468
469 $v1 = qv(1.2); # v1.2.0
470 $v2 = qv("1.2"); # also v1.2.0
471
472 As you can see, either a bare number or a quoted string can usually
473 be used interchangeably, except in the case of a trailing zero,
474 which must be quoted to be converted properly. For this reason, it
475 is strongly recommended that all initializers to qv() be quoted
476 strings instead of bare numbers.
477
478 To prevent the qv() function from being exported to the caller's
479 namespace, either use version with a null parameter:
480
481 use version ();
482
483 or just require version, like this:
484
485 require version;
486
487 Both methods will prevent the import() method from firing and
488 exporting the qv() sub.
489
490 For the subsequent examples, the following three objects will be used:
491
492 $ver = version->new("1.2.3.4"); # see "Quoting Rules"
493 $alpha = version->new("1.2.3_4"); # see "Alpha Versions"
494 $nver = version->new(1.002); # see "Decimal Versions"
495
496 Normal Form
497 For any version object which is initialized with multiple decimal
498 places (either quoted or if possible v-string), or initialized
499 using the qv() operator, the stringified representation is returned
500 in a normalized or reduced form (no extraneous zeros), and with a
501 leading 'v':
502
503 print $ver->normal; # prints as v1.2.3.4
504 print $ver->stringify; # ditto
505 print $ver; # ditto
506 print $nver->normal; # prints as v1.2.0
507 print $nver->stringify; # prints as 1.002,
508 # see "Stringification"
509
510 In order to preserve the meaning of the processed version, the
511 normalized representation will always contain at least three sub
512 terms. In other words, the following is guaranteed to always be
513 true:
514
515 my $newver = version->new($ver->stringify);
516 if ($newver eq $ver ) # always true
517 {...}
518
519 Numification
520 Although all mathematical operations on version objects are
521 forbidden by default, it is possible to retrieve a number which
522 corresponds to the version object through the use of the
523 $obj->numify method. For formatting purposes, when displaying a
524 number which corresponds a version object, all sub versions are
525 assumed to have three decimal places. So for example:
526
527 print $ver->numify; # prints 1.002003004
528 print $nver->numify; # prints 1.002
529
530 Unlike the stringification operator, there is never any need to
531 append trailing zeros to preserve the correct version value.
532
533 Stringification
534 The default stringification for version objects returns exactly the
535 same string as was used to create it, whether you used new() or
536 qv(), with one exception. The sole exception is if the object was
537 created using qv() and the initializer did not have two decimal
538 places or a leading 'v' (both optional), then the stringified form
539 will have a leading 'v' prepended, in order to support round-trip
540 processing.
541
542 For example:
543
544 Initialized as Stringifies to
545 ============== ==============
546 version->new("1.2") 1.2
547 version->new("v1.2") v1.2
548 qv("1.2.3") 1.2.3
549 qv("v1.3.5") v1.3.5
550 qv("1.2") v1.2 ### exceptional case
551
552 See also UNIVERSAL::VERSION, as this also returns the stringified
553 form when used as a class method.
554
555 IMPORTANT NOTE: There is one exceptional cases shown in the above
556 table where the "initializer" is not stringwise equivalent to the
557 stringified representation. If you use the "qv"() operator on a
558 version without a leading 'v' and with only a single decimal place,
559 the stringified output will have a leading 'v', to preserve the
560 sense. See the "qv()" operator for more details.
561
562 IMPORTANT NOTE 2: Attempting to bypass the normal stringification
563 rules by manually applying numify() and normal() will sometimes
564 yield surprising results:
565
566 print version->new(version->new("v1.0")->numify)->normal; # v1.0.0
567
568 The reason for this is that the numify() operator will turn "v1.0"
569 into the equivalent string "1.000000". Forcing the outer version
570 object to normal() form will display the mathematically equivalent
571 "v1.0.0".
572
573 As the example in "new()" shows, you can always create a copy of an
574 existing version object with the same value by the very compact:
575
576 $v2 = $v1->new($v1);
577
578 and be assured that both $v1 and $v2 will be completely equivalent,
579 down to the same internal representation as well as
580 stringification.
581
582 Comparison operators
583 Both "cmp" and "<=>" operators perform the same comparison between
584 terms (upgrading to a version object automatically). Perl
585 automatically generates all of the other comparison operators based
586 on those two. In addition to the obvious equalities listed below,
587 appending a single trailing 0 term does not change the value of a
588 version for comparison purposes. In other words "v1.2" and "1.2.0"
589 will compare as identical.
590
591 For example, the following relations hold:
592
593 As Number As String Truth Value
594 ------------- ---------------- -----------
595 $ver > 1.0 $ver gt "1.0" true
596 $ver < 2.5 $ver lt true
597 $ver != 1.3 $ver ne "1.3" true
598 $ver == 1.2 $ver eq "1.2" false
599 $ver == 1.2.3.4 $ver eq "1.2.3.4" see discussion below
600
601 It is probably best to chose either the Decimal notation or the
602 string notation and stick with it, to reduce confusion. Perl6
603 version objects may only support Decimal comparisons. See also
604 "Quoting Rules".
605
606 WARNING: Comparing version with unequal numbers of decimal points
607 (whether explicitly or implicitly initialized), may yield
608 unexpected results at first glance. For example, the following
609 inequalities hold:
610
611 version->new(0.96) > version->new(0.95); # 0.960.0 > 0.950.0
612 version->new("0.96.1") < version->new(0.95); # 0.096.1 < 0.950.0
613
614 For this reason, it is best to use either exclusively "Decimal
615 Versions" or "Dotted-Decimal Versions" with multiple decimal
616 points.
617
618 Logical Operators
619 If you need to test whether a version object has been initialized,
620 you can simply test it directly:
621
622 $vobj = version->new($something);
623 if ( $vobj ) # true only if $something was non-blank
624
625 You can also test whether a version object is an alpha version, for
626 example to prevent the use of some feature not present in the main
627 release:
628
629 $vobj = version->new("1.2_3"); # MUST QUOTE
630 ...later...
631 if ( $vobj->is_alpha ) # True
632
634 John Peacock <jpeacock@cpan.org>
635
637 perl.
638
639
640
641perl v5.38.0 2023-09-12 version::Internals(3)