[Fwd: CPAN Upload: J/JP/JPEACOCK/version-0.47.tar.gz]
[p5sagit/p5-mst-13.2.git] / lib / version.pm
CommitLineData
c8d69e4a 1#!perl -w
a7ad731c 2package version;
3
4use 5.005_03;
5use strict;
6
137d6fc0 7require Exporter;
a7ad731c 8require DynaLoader;
137d6fc0 9use vars qw(@ISA $VERSION $CLASS @EXPORT);
a7ad731c 10
137d6fc0 11@ISA = qw(Exporter DynaLoader);
a7ad731c 12
137d6fc0 13@EXPORT = qw(qv);
14
e0218a61 15$VERSION = "0.47";
a7ad731c 16
17$CLASS = 'version';
18
26ec6fc3 19local $^W; # shut up the 'redefined' warning for UNIVERSAL::VERSION
a7ad731c 20bootstrap version if $] < 5.009;
21
22# Preloaded methods go here.
23
241;
25__END__
26
27=head1 NAME
28
29version - Perl extension for Version Objects
30
31=head1 SYNOPSIS
32
33 use version;
b9381830 34 $version = version->new("12.2.1"); # must be quoted for Perl < 5.8.1
a7ad731c 35 print $version; # 12.2.1
36 print $version->numify; # 12.002001
137d6fc0 37 if ( $version gt "12.2" ) # true
a7ad731c 38
9137345a 39 $alphaver = version->new("1.02_03"); # must be quoted!
40 print $alphaver; # 1.02_030
c8d69e4a 41 print $alphaver->is_alpha(); # true
137d6fc0 42
43 $ver = qv(1.2); # 1.2.0
44 $ver = qv("1.2"); # 1.2.0
a7ad731c 45
b9381830 46 $perlver = version->new(5.005_03); # must not be quoted!
9137345a 47 print $perlver; # 5.005030
a7ad731c 48
49=head1 DESCRIPTION
50
51Overloaded version objects for all versions of Perl. This module
26ec6fc3 52implements all of the features of version objects which will be part
137d6fc0 53of Perl 5.10.0 except automatic version object creation.
a7ad731c 54
55=head2 What IS a version
56
57For the purposes of this module, a version "number" is a sequence of
58positive integral values separated by decimal points and optionally a
59single underscore. This corresponds to what Perl itself uses for a
60version, as well as extending the "version as number" that is discussed
61in the various editions of the Camel book.
62
129318bd 63There are actually two distinct ways to initialize versions:
64
65=over 4
66
c8d69e4a 67=item * Numeric Versions
129318bd 68
c8d69e4a 69Any initial parameter which "looks like a number", see L<Numeric
9137345a 70Versions>. This also covers versions with a single decimal place and
71a single embedded underscore, see L<Numeric Alpha Versions>, even though
72these must be quoted to preserve the underscore formatting.
c8d69e4a 73
137d6fc0 74=item * Quoted Versions
c8d69e4a 75
137d6fc0 76Any initial parameter which contains more than one decimal point
9137345a 77and an optional embedded underscore, see L<Quoted Versions>.
129318bd 78
79=back
80
81Both of these methods will produce similar version objects, in that
b9381830 82the default stringification will yield the version L<Normal Form> only
83if required:
129318bd 84
b9381830 85 $v = version->new(1.002); # 1.002, but compares like 1.2.0
9137345a 86 $v = version->new(1.002003); # 1.002003
87 $v2 = version->new( "1.2.3"); # v1.2.3
88 $v3 = version->new( 1.2.3); # v1.2.3 for Perl >= 5.8.1
129318bd 89
9137345a 90In specific, version numbers initialized as L<Numeric Versions> will
91stringify in Numeric form. Version numbers initialized as L<Quoted Versions>
92will be stringified as L<Normal Form>.
93
94Please see L<Quoting> for more details on how Perl will parse various
129318bd 95input values.
46314c13 96
97Any value passed to the new() operator will be parsed only so far as it
98contains a numeric, decimal, or underscore character. So, for example:
99
b9381830 100 $v1 = version->new("99 and 94/100 percent pure"); # $v1 == 99.0
101 $v2 = version->new("something"); # $v2 == "" and $v2->numify == 0
46314c13 102
129318bd 103However, see L<New Operator> for one case where non-numeric text is
104acceptable when initializing version objects.
105
137d6fc0 106=head2 What about v-strings?
107
108Beginning with Perl 5.6.0, an alternate method to code arbitrary strings
109of bytes was introduced, called v-strings. They were intended to be an
110easy way to enter, for example, Unicode strings (which contain two bytes
111per character). Some programs have used them to encode printer control
112characters (e.g. CRLF). They were also intended to be used for $VERSION.
113Their use has been problematic from the start and they will be phased out
114beginning in Perl 5.10.0.
115
116There are two ways to enter v-strings: a bare number with two or more
117decimal places, or a bare number with one or more decimal places and a
118leading 'v' character (also bare). For example:
119
120 $vs1 = 1.2.3; # encoded as \1\2\3
b9381830 121 $vs2 = v1.2; # encoded as \1\2
137d6fc0 122
123The first of those two syntaxes is destined to be the default way to create
124a version object in 5.10.0, whereas the second will issue a mandatory
b9381830 125deprecation warning beginning at the same time. In both cases, a v-string
126encoded version will always be stringified in the version L<Normal Form>.
137d6fc0 127
128Consequently, the use of v-strings to initialize version objects with
b9381830 129this module is only possible with Perl 5.8.1 or better (which contain special
137d6fc0 130code to enable it). Their use is B<strongly> discouraged in all
b9381830 131circumstances (especially the leading 'v' style), since the meaning will
137d6fc0 132change depending on which Perl you are running. It is better to use
133L<"Quoted Versions"> to ensure the proper interpretation.
134
129318bd 135=head2 Numeric Versions
136
137d6fc0 137These correspond to historical versions of Perl itself prior to 5.6.0,
129318bd 138as well as all other modules which follow the Camel rules for the
139$VERSION scalar. A numeric version is initialized with what looks like
140a floating point number. Leading zeros B<are> significant and trailing
141zeros are implied so that a minimum of three places is maintained
142between subversions. What this means is that any subversion (digits
143to the right of the decimal place) that contains less than three digits
b9381830 144will have trailing zeros added to make up the difference, but only for
145purposes of comparison with other version objects. For example:
129318bd 146
b9381830 147 $v = version->new( 1.2); # prints 1.2, compares as 1.200.0
148 $v = version->new( 1.02); # prints 1.02, compares as 1.20.0
149 $v = version->new( 1.002); # prints 1.002, compares as 1.2.0
150 $v = version->new( 1.0023); # 1.2.300
151 $v = version->new( 1.00203); # 1.2.30
152 $v = version->new( 1.002_03); # 1.2.30 See "Quoting"
153 $v = version->new( 1.002003); # 1.2.3
129318bd 154
3c4b39be 155All of the preceding examples except the second to last are true
129318bd 156whether or not the input value is quoted. The important feature is that
157the input value contains only a single decimal.
158
b9381830 159IMPORTANT NOTE: If your numeric version contains more than 3 significant
160digits after the decimal place, it will be split on each multiple of 3, so
1611.0003 becomes 1.0.300, due to the need to remain compatible with Perl's
162own 5.005_03 == 5.5.30 interpretation.
163
137d6fc0 164=head2 Quoted Versions
129318bd 165
166These are the newest form of versions, and correspond to Perl's own
137d6fc0 167version style beginning with 5.6.0. Starting with Perl 5.10.0,
168and most likely Perl 6, this is likely to be the preferred form. This
169method requires that the input parameter be quoted, although Perl's after
1705.9.0 can use bare numbers with multiple decimal places as a special form
171of quoting.
172
173Unlike L<Numeric Versions>, Quoted Versions may have more than
b9381830 174a single decimal point, e.g. "5.6.1" (for all versions of Perl). If a
175Quoted Version has only one decimal place (and no embedded underscore),
176it is interpreted exactly like a L<Numeric Version>.
129318bd 177
178So, for example:
179
b9381830 180 $v = version->new( "1.002"); # 1.2
181 $v = version->new( "1.2.3"); # 1.2.3
182 $v = version->new("1.0003"); # 1.0.300
129318bd 183
137d6fc0 184In addition to conventional versions, Quoted Versions can be
c8d69e4a 185used to create L<Alpha Versions>.
129318bd 186
137d6fc0 187In general, Quoted Versions permit the greatest amount of freedom
c8d69e4a 188to specify a version, whereas Numeric Versions enforce a certain
129318bd 189uniformity. See also L<New Operator> for an additional method of
190initializing version objects.
46314c13 191
9137345a 192=head2 Numeric Alpha Versions
193
194The one time that a numeric version must be quoted is when a alpha form is
195used with an otherwise numeric version (i.e. a single decimal place). This
196is commonly used for CPAN releases, where CPAN or CPANPLUS will ignore alpha
197versions for automatic updating purposes. Since some developers have used
198only two significant decimal places for their non-alpha releases, the
199version object will automatically take that into account if the initializer
200is quoted. For example Module::Example was released to CPAN with the
201following sequence of $VERSION's:
202
203 # $VERSION Stringified
204 0.01 0.010
205 0.02 0.020
206 0.02_01 0.02_0100
207 0.02_02 0.02_0200
208 0.03 0.030
209 etc.
210
211As you can see, the version object created from the values in the first
212column may contain a trailing 0, but will otherwise be both mathematically
213equivalent and sorts alpha-numerically as would be expected.
214
a7ad731c 215=head2 Object Methods
216
217Overloading has been used with version objects to provide a natural
218interface for their use. All mathematical operations are forbidden,
c8d69e4a 219since they don't make any sense for base version objects.
129318bd 220
221=over 4
222
c8d69e4a 223=item * New Operator
224
225Like all OO interfaces, the new() operator is used to initialize
226version objects. One way to increment versions when programming is to
227use the CVS variable $Revision, which is automatically incremented by
228CVS every time the file is committed to the repository.
129318bd 229
129318bd 230In order to facilitate this feature, the following
231code can be employed:
232
b9381830 233 $VERSION = version->new(qw$Revision: 2.7 $);
129318bd 234
235and the version object will be created as if the following code
236were used:
237
b9381830 238 $VERSION = version->new("v2.7");
129318bd 239
240In other words, the version will be automatically parsed out of the
241string, and it will be quoted to preserve the meaning CVS normally
13f8f398 242carries for versions. The CVS $Revision$ increments differently from
243numeric versions (i.e. 1.10 follows 1.9), so it must be handled as if
244it were a L<Quoted Version>.
245
9137345a 246A new version object can be created as a copy of an existing version
247object, either as a class method:
13f8f398 248
249 $v1 = version->new(12.3);
250 $v2 = version->new($v1);
251
9137345a 252or as an object method:
253
254 $v1 = version->new(12.3);
255 $v2 = $v1->new();
256
257and in each case, $v1 and $v2 will be identical.
129318bd 258
137d6fc0 259=back
260
261=over 4
262
263=item * qv()
264
265An alternate way to create a new version object is through the exported
266qv() sub. This is not strictly like other q? operators (like qq, qw),
267in that the only delimiters supported are parentheses (or spaces). It is
268the best way to initialize a short version without triggering the floating
269point interpretation. For example:
270
271 $v1 = qv(1.2); # 1.2.0
272 $v2 = qv("1.2"); # also 1.2.0
273
274As you can see, either a bare number or a quoted string can be used, and
275either will yield the same version number.
276
277=back
278
13f8f398 279For the subsequent examples, the following three objects will be used:
a7ad731c 280
13f8f398 281 $ver = version->new("1.2.3.4"); # see "Quoting" below
282 $alpha = version->new("1.2.3_4"); # see "Alpha versions" below
9137345a 283 $nver = version->new(1.002); # see "Numeric Versions" above
a7ad731c 284
129318bd 285=over 4
286
b9381830 287=item * Normal Form
c8d69e4a 288
b9381830 289For any version object which is initialized with multiple decimal
290places (either quoted or if possible v-string), or initialized using
291the L<qv()> operator, the stringified representation is returned in
9137345a 292a normalized or reduced form (no extraneous zeros), and with a leading 'v':
a7ad731c 293
9137345a 294 print $ver->normal; # prints as v1.2.3
b9381830 295 print $ver->stringify; # ditto
296 print $ver; # ditto
9137345a 297 print $nver->normal; # prints as v1.2.0
298 print $nver->stringify; # prints as 1.002, see "Stringification"
a7ad731c 299
137d6fc0 300In order to preserve the meaning of the processed version, the
b9381830 301normalized representation will always contain at least three sub terms.
302In other words, the following is guaranteed to always be true:
137d6fc0 303
304 my $newver = version->new($ver->stringify);
305 if ($newver eq $ver ) # always true
306 {...}
307
137d6fc0 308=back
309
129318bd 310=over 4
311
c8d69e4a 312=item * Numification
a7ad731c 313
c8d69e4a 314Although all mathematical operations on version objects are forbidden
315by default, it is possible to retrieve a number which roughly
316corresponds to the version object through the use of the $obj->numify
317method. For formatting purposes, when displaying a number which
318corresponds a version object, all sub versions are assumed to have
319three decimal places. So for example:
129318bd 320
a7ad731c 321 print $ver->numify; # prints 1.002003
9137345a 322 print $nver->numify; # prints 1.002
a7ad731c 323
137d6fc0 324Unlike the stringification operator, there is never any need to append
325trailing zeros to preserve the correct version value.
326
327=back
328
329=over 4
330
b9381830 331=item * Stringification
332
333In order to mirror as much as possible the existing behavior of ordinary
334$VERSION scalars, the stringification operation will display differently,
335depending on whether the version was initialized as a L<Numeric Version>
336or L<Quoted Version>.
337
338What this means in practice is that if the normal CPAN and Camel rules are
339followed ($VERSION is a floating point number with no more than 3 decimal
340places), the stringified output will be exactly the same as the numified
341output. There will be no visible difference, although the internal
342representation will be different, and the L<Comparison operators> will
343function using the internal coding.
344
345If a version object is initialized using a L<Quoted Version> form, or if
346the number of significant decimal places exceed three, then the stringified
347form will be the L<Normal Form>. The $obj->normal operation can always be
348used to produce the L<Normal Form>, even if the version was originally a
349L<Numeric Version>.
350
9137345a 351 print $ver->stringify; # prints v1.2.3
352 print $nver->stringify; # prints 1.002
b9381830 353
354=back
355
356=over 4
357
c8d69e4a 358=item * Comparison operators
129318bd 359
c8d69e4a 360Both cmp and <=> operators perform the same comparison between terms
361(upgrading to a version object automatically). Perl automatically
362generates all of the other comparison operators based on those two.
363In addition to the obvious equalities listed below, appending a single
364trailing 0 term does not change the value of a version for comparison
137d6fc0 365purposes. In other words "v1.2" and "1.2.0" will compare as identical.
129318bd 366
367For example, the following relations hold:
a7ad731c 368
129318bd 369 As Number As String Truth Value
370 --------- ------------ -----------
a7ad731c 371 $ver > 1.0 $ver gt "1.0" true
372 $ver < 2.5 $ver lt true
373 $ver != 1.3 $ver ne "1.3" true
374 $ver == 1.2 $ver eq "1.2" false
375 $ver == 1.2.3 $ver eq "1.2.3" see discussion below
a7ad731c 376
137d6fc0 377It is probably best to chose either the numeric notation or the string
378notation and stick with it, to reduce confusion. Perl6 version objects
379B<may> only support numeric comparisons. See also L<"Quoting">.
a7ad731c 380
b9381830 381WARNING: Comparing version with unequal numbers of decimal places (whether
3c4b39be 382explicitly or implicitly initialized), may yield unexpected results at
b9381830 383first glance. For example, the following inequalities hold:
384
385 version->new(0.96) > version->new(0.95); # 0.960.0 > 0.950.0
386 version->new("0.96.1") < version->new(0.95); # 0.096.1 < 0.950.0
387
388For this reason, it is best to use either exclusively L<Numeric Versions> or
389L<Quoted Versions> with multiple decimal places.
390
137d6fc0 391=back
a7ad731c 392
137d6fc0 393=over 4
a7ad731c 394
c8d69e4a 395=item * Logical Operators
396
397If you need to test whether a version object
398has been initialized, you can simply test it directly:
399
b9381830 400 $vobj = version->new($something);
c8d69e4a 401 if ( $vobj ) # true only if $something was non-blank
402
137d6fc0 403You can also test whether a version object is an L<Alpha version>, for
c8d69e4a 404example to prevent the use of some feature not present in the main
405release:
406
b9381830 407 $vobj = version->new("1.2_3"); # MUST QUOTE
c8d69e4a 408 ...later...
409 if ( $vobj->is_alpha ) # True
410
411=back
412
a7ad731c 413=head2 Quoting
414
c8d69e4a 415Because of the nature of the Perl parsing and tokenizing routines,
129318bd 416certain initialization values B<must> be quoted in order to correctly
417parse as the intended version, and additionally, some initial values
418B<must not> be quoted to obtain the intended version.
419
c8d69e4a 420Except for L<Alpha versions>, any version initialized with something
129318bd 421that looks like a number (a single decimal place) will be parsed in
422the same way whether or not the term is quoted. In order to be
423compatible with earlier Perl version styles, any use of versions of
424the form 5.006001 will be translated as 5.6.1. In other words, a
425version with a single decimal place will be parsed as implicitly
426having three places between subversions.
427
428The complicating factor is that in bare numbers (i.e. unquoted), the
429underscore is a legal numeric character and is automatically stripped
430by the Perl tokenizer before the version code is called. However, if
13f8f398 431a number containing one or more decimals and an underscore is quoted, i.e.
c8d69e4a 432not bare, that is considered a L<Alpha Version> and the underscore is
129318bd 433significant.
a7ad731c 434
435If you use a mathematic formula that resolves to a floating point number,
436you are dependent on Perl's conversion routines to yield the version you
437expect. You are pretty safe by dividing by a power of 10, for example,
438but other operations are not likely to be what you intend. For example:
439
b9381830 440 $VERSION = version->new((qw$Revision: 1.4)[1]/10);
a7ad731c 441 print $VERSION; # yields 0.14
b9381830 442 $V2 = version->new(100/9); # Integer overflow in decimal number
443 print $V2; # yields something like 11.111.111.100
a7ad731c 444
9137345a 445Perl 5.8.1 and beyond will be able to automatically quote v-strings but
446that is not possible in earlier versions of Perl. In other words:
a7ad731c 447
b9381830 448 $version = version->new("v2.5.4"); # legal in all versions of Perl
449 $newvers = version->new(v2.5.4); # legal only in Perl >= 5.8.1
a7ad731c 450
451
452=head2 Types of Versions Objects
453
129318bd 454There are two types of Version Objects:
455
456=over 4
a7ad731c 457
c8d69e4a 458=item * Ordinary versions
a7ad731c 459
c8d69e4a 460These are the versions that normal modules will use. Can contain as
461many subversions as required. In particular, those using RCS/CVS can
b9381830 462use the following:
129318bd 463
b9381830 464 $VERSION = version->new(qw$Revision: 2.7 $);
a7ad731c 465
c8d69e4a 466and the current RCS Revision for that file will be inserted
467automatically. If the file has been moved to a branch, the Revision
468will have three or more elements; otherwise, it will have only two.
469This allows you to automatically increment your module version by
470using the Revision number from the primary file in a distribution, see
471L<ExtUtils::MakeMaker/"VERSION_FROM">.
a7ad731c 472
9137345a 473=item * Alpha Versions
129318bd 474
c8d69e4a 475For module authors using CPAN, the convention has been to note
476unstable releases with an underscore in the version string, see
477L<CPAN>. Alpha releases will test as being newer than the more recent
478stable release, and less than the next stable release. For example:
129318bd 479
9137345a 480 $alphaver = version->new("12.03_01"); # must be quoted
a7ad731c 481
482obeys the relationship
483
9137345a 484 12.03 < $alphaver < 12.04
a7ad731c 485
13f8f398 486Alpha versions with a single decimal place will be treated exactly as if
487they were L<Numeric Versions>, for parsing purposes. The stringification for
3c4b39be 488alpha versions with a single decimal place may seem surprising, since any
13f8f398 489trailing zeros will visible. For example, the above $alphaver will print as
490
9137345a 491 12.03_0100
492
493which is mathematically equivalent and ASCII sorts exactly the same as
494without the trailing zeros.
13f8f398 495
496Alpha versions with more than a single decimal place will be treated
497exactly as if they were L<Quoted Versions>, and will display without any
498trailing (or leading) zeros, in the L<Version Normal> form. For example,
499
500 $newver = version->new("12.3.1_1");
9137345a 501 print $newver; # v12.3.1_1
13f8f398 502
a7ad731c 503=head2 Replacement UNIVERSAL::VERSION
504
505In addition to the version objects, this modules also replaces the core
506UNIVERSAL::VERSION function with one that uses version objects for its
b9381830 507comparisons. The return from this operator is always the numified form,
508and the warning message generated includes both the numified and normal
509forms (for clarity).
510
511For example:
512
513 package Foo;
514 $VERSION = 1.2;
515
516 package Bar;
517 $VERSION = "1.3.5"; # works with all Perl's (since it is quoted)
518
519 package main;
520 use version;
521
522 print $Foo::VERSION; # prints 1.2
523
524 print $Bar::VERSION; # prints 1.003005
525
526 eval "use CGI 10"; # some far future release
527 print $@; # prints "CGI version 10 (10.0.0) required..."
528
529IMPORTANT NOTE: This may mean that code which searches for a specific
530string (to determine whether a given module is available) may need to be
531changed.
a7ad731c 532
13f8f398 533The replacement UNIVERSAL::VERSION, when used as a function, like this:
534
535 print $module->VERSION;
536
9137345a 537will also exclusively return the numified form. Technically, the
538$module->VERSION function returns a string (PV) that can be converted to a
539number following the normal Perl rules, when used in a numeric context.
13f8f398 540
e0218a61 541=head1 SUBCLASSING
542
543This module is specifically designed and tested to be easily subclassed.
544In practice, you only need to override the methods you want to change, but
545you have to take some care when overriding new() (since that is where all
546of the parsing takes place). For example, this is a perfect acceptable
547derived class:
548
549 package myversion;
550 use base version;
551 sub new {
552 my($self,$n)=@_;
553 my $obj;
554 # perform any special input handling here
555 $obj = $self->SUPER::new($n);
556 # and/or add additional hash elements here
557 return $obj;
558 }
559
560See also L<version::AlphaBeta> on CPAN for an alternate representation of
561version strings.
562
a7ad731c 563=head1 EXPORT
564
137d6fc0 565qv - quoted version initialization operator
a7ad731c 566
567=head1 AUTHOR
568
9137345a 569John Peacock E<lt>jpeacock@cpan.orgE<gt>
a7ad731c 570
571=head1 SEE ALSO
572
573L<perl>.
574
575=cut