Debian Codegraph @

upstream/mageia-cauldron/man3pm/version::Internals.3pm

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