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