Debian Codegraph @

upstream/debian-unstable/man3/version::Internals.3perl

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