Upgrade to CPAN-1.76_61
[p5sagit/p5-mst-13.2.git] / lib / CPAN / Version.pm
1 =head1 NAME
2
3 CPAN::Version - utility functions to compare CPAN versions
4
5 =head1 SYNOPSIS
6
7   use CPAN::Version;
8
9   CPAN::Version->vgt("1.1","1.1.1");    # 1
10
11   CPAN::Version->vcmp("1.1","1.1.1");   # 1
12
13   CPAN::Version->readable(v1.2.3);      # "v1.2.3"
14
15   CPAN::Version->vstring("v1.2.3");     # v1.2.3
16
17   CPAN::Version->float2vv(1.002003);    # "v1.2.3"
18
19 =head1 DESCRIPTION
20
21 This module mediates between some version that perl sees in a package
22 and the version that is published by the CPAN indexer.
23
24 It's only written as a helper module for both CPAN.pm and CPANPLUS.pm.
25
26 As it stands it predates version.pm but has the same goal: make
27 version strings visible and comparable.
28
29 =cut
30
31 package CPAN::Version;
32
33 use vars qw($VERSION);
34 $VERSION = sprintf "%.2f", substr(q$Rev: 231 $,4)/100;
35
36 # CPAN::Version::vcmp courtesy Jost Krieger
37 sub vcmp {
38   my($self,$l,$r) = @_;
39   local($^W) = 0;
40   CPAN->debug("l[$l] r[$r]") if $CPAN::DEBUG;
41
42   return 0 if $l eq $r; # short circuit for quicker success
43
44   for ($l,$r) {
45       next unless tr/.// > 1;
46       s/^v?/v/;
47       1 while s/\.0+(\d)/.$1/;
48   }
49   if ($l=~/^v/ <=> $r=~/^v/) {
50       for ($l,$r) {
51           next if /^v/;
52           $_ = $self->float2vv($_);
53       }
54   }
55
56   return (
57           ($l ne "undef") <=> ($r ne "undef") ||
58           (
59            $] >= 5.006 &&
60            $l =~ /^v/ &&
61            $r =~ /^v/ &&
62            $self->vstring($l) cmp $self->vstring($r)
63           ) ||
64           $l <=> $r ||
65           $l cmp $r
66          );
67 }
68
69 sub vgt {
70   my($self,$l,$r) = @_;
71   $self->vcmp($l,$r) > 0;
72 }
73
74 sub vstring {
75   my($self,$n) = @_;
76   $n =~ s/^v// or die "CPAN::Version::vstring() called with invalid arg [$n]";
77   pack "U*", split /\./, $n;
78 }
79
80 # vv => visible vstring
81 sub float2vv {
82     my($self,$n) = @_;
83     my($rev) = int($n);
84     $rev ||= 0;
85     my($mantissa) = $n =~ /\.(\d{1,12})/; # limit to 12 digits to limit
86                                           # architecture influence
87     $mantissa ||= 0;
88     $mantissa .= "0" while length($mantissa)%3;
89     my $ret = "v" . $rev;
90     while ($mantissa) {
91         $mantissa =~ s/(\d{1,3})// or
92             die "Panic: length>0 but not a digit? mantissa[$mantissa]";
93         $ret .= ".".int($1);
94     }
95     # warn "n[$n]ret[$ret]";
96     $ret;
97 }
98
99 sub readable {
100   my($self,$n) = @_;
101   $n =~ /^([\w\-\+\.]+)/;
102
103   return $1 if defined $1 && length($1)>0;
104   # if the first user reaches version v43, he will be treated as "+".
105   # We'll have to decide about a new rule here then, depending on what
106   # will be the prevailing versioning behavior then.
107
108   if ($] < 5.006) { # or whenever v-strings were introduced
109     # we get them wrong anyway, whatever we do, because 5.005 will
110     # have already interpreted 0.2.4 to be "0.24". So even if he
111     # indexer sends us something like "v0.2.4" we compare wrongly.
112
113     # And if they say v1.2, then the old perl takes it as "v12"
114
115     if (defined $CPAN::Frontend) {
116       $CPAN::Frontend->mywarn("Suspicious version string seen [$n]\n");
117     } else {
118       warn("Suspicious version string seen [$n]\n");
119     }
120     return $n;
121   }
122   my $better = sprintf "v%vd", $n;
123   CPAN->debug("n[$n] better[$better]") if $CPAN::DEBUG;
124   return $better;
125 }
126
127 1;
128
129 __END__
130
131 # Local Variables:
132 # mode: cperl
133 # cperl-indent-level: 2
134 # End: