Commit | Line | Data |
554a9ef5 |
1 | package CPAN::Version; |
2 | |
ec5fee46 |
3 | use strict; |
0a78cd5d |
4 | use vars qw($VERSION); |
7d97ad34 |
5 | $VERSION = sprintf "%.6f", substr(q$Rev: 950 $,4)/1000000 + 5.4; |
0a78cd5d |
6 | |
554a9ef5 |
7 | # CPAN::Version::vcmp courtesy Jost Krieger |
8 | sub vcmp { |
f3fe0ae6 |
9 | my($self,$l,$r) = @_; |
554a9ef5 |
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) { |
7d97ad34 |
16 | s/_//g; |
17 | } |
18 | CPAN->debug("l[$l] r[$r]") if $CPAN::DEBUG; |
19 | for ($l,$r) { |
554a9ef5 |
20 | next unless tr/.// > 1; |
21 | s/^v?/v/; |
22 | 1 while s/\.0+(\d)/.$1/; |
23 | } |
7d97ad34 |
24 | CPAN->debug("l[$l] r[$r]") if $CPAN::DEBUG; |
554a9ef5 |
25 | if ($l=~/^v/ <=> $r=~/^v/) { |
26 | for ($l,$r) { |
27 | next if /^v/; |
28 | $_ = $self->float2vv($_); |
29 | } |
30 | } |
7d97ad34 |
31 | CPAN->debug("l[$l] r[$r]") if $CPAN::DEBUG; |
554a9ef5 |
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 { |
f3fe0ae6 |
47 | my($self,$l,$r) = @_; |
554a9ef5 |
48 | $self->vcmp($l,$r) > 0; |
49 | } |
50 | |
c9869e1c |
51 | sub vlt { |
52 | my($self,$l,$r) = @_; |
53 | 0 + ($self->vcmp($l,$r) < 0); |
54 | } |
55 | |
554a9ef5 |
56 | sub vstring { |
f3fe0ae6 |
57 | my($self,$n) = @_; |
554a9ef5 |
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 { |
f3fe0ae6 |
64 | my($self,$n) = @_; |
554a9ef5 |
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 { |
f3fe0ae6 |
82 | my($self,$n) = @_; |
554a9ef5 |
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 | |
0a78cd5d |
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 | } |
554a9ef5 |
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 | |
26844e27 |
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 | |
554a9ef5 |
152 | # Local Variables: |
153 | # mode: cperl |
154 | # cperl-indent-level: 2 |
155 | # End: |