Commit | Line | Data |
554a9ef5 |
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 | |
ec5fee46 |
33 | use strict; |
0a78cd5d |
34 | use vars qw($VERSION); |
ec5fee46 |
35 | $VERSION = sprintf "%.2f", substr(q$Rev: 254 $,4)/100; |
0a78cd5d |
36 | |
554a9ef5 |
37 | # CPAN::Version::vcmp courtesy Jost Krieger |
38 | sub vcmp { |
f3fe0ae6 |
39 | my($self,$l,$r) = @_; |
554a9ef5 |
40 | local($^W) = 0; |
41 | CPAN->debug("l[$l] r[$r]") if $CPAN::DEBUG; |
42 | |
43 | return 0 if $l eq $r; # short circuit for quicker success |
44 | |
45 | for ($l,$r) { |
46 | next unless tr/.// > 1; |
47 | s/^v?/v/; |
48 | 1 while s/\.0+(\d)/.$1/; |
49 | } |
50 | if ($l=~/^v/ <=> $r=~/^v/) { |
51 | for ($l,$r) { |
52 | next if /^v/; |
53 | $_ = $self->float2vv($_); |
54 | } |
55 | } |
56 | |
57 | return ( |
58 | ($l ne "undef") <=> ($r ne "undef") || |
59 | ( |
60 | $] >= 5.006 && |
61 | $l =~ /^v/ && |
62 | $r =~ /^v/ && |
63 | $self->vstring($l) cmp $self->vstring($r) |
64 | ) || |
65 | $l <=> $r || |
66 | $l cmp $r |
67 | ); |
68 | } |
69 | |
70 | sub vgt { |
f3fe0ae6 |
71 | my($self,$l,$r) = @_; |
554a9ef5 |
72 | $self->vcmp($l,$r) > 0; |
73 | } |
74 | |
75 | sub vstring { |
f3fe0ae6 |
76 | my($self,$n) = @_; |
554a9ef5 |
77 | $n =~ s/^v// or die "CPAN::Version::vstring() called with invalid arg [$n]"; |
78 | pack "U*", split /\./, $n; |
79 | } |
80 | |
81 | # vv => visible vstring |
82 | sub float2vv { |
f3fe0ae6 |
83 | my($self,$n) = @_; |
554a9ef5 |
84 | my($rev) = int($n); |
85 | $rev ||= 0; |
86 | my($mantissa) = $n =~ /\.(\d{1,12})/; # limit to 12 digits to limit |
87 | # architecture influence |
88 | $mantissa ||= 0; |
89 | $mantissa .= "0" while length($mantissa)%3; |
90 | my $ret = "v" . $rev; |
91 | while ($mantissa) { |
92 | $mantissa =~ s/(\d{1,3})// or |
93 | die "Panic: length>0 but not a digit? mantissa[$mantissa]"; |
94 | $ret .= ".".int($1); |
95 | } |
96 | # warn "n[$n]ret[$ret]"; |
97 | $ret; |
98 | } |
99 | |
100 | sub readable { |
f3fe0ae6 |
101 | my($self,$n) = @_; |
554a9ef5 |
102 | $n =~ /^([\w\-\+\.]+)/; |
103 | |
104 | return $1 if defined $1 && length($1)>0; |
105 | # if the first user reaches version v43, he will be treated as "+". |
106 | # We'll have to decide about a new rule here then, depending on what |
107 | # will be the prevailing versioning behavior then. |
108 | |
109 | if ($] < 5.006) { # or whenever v-strings were introduced |
110 | # we get them wrong anyway, whatever we do, because 5.005 will |
111 | # have already interpreted 0.2.4 to be "0.24". So even if he |
112 | # indexer sends us something like "v0.2.4" we compare wrongly. |
113 | |
114 | # And if they say v1.2, then the old perl takes it as "v12" |
115 | |
0a78cd5d |
116 | if (defined $CPAN::Frontend) { |
117 | $CPAN::Frontend->mywarn("Suspicious version string seen [$n]\n"); |
118 | } else { |
119 | warn("Suspicious version string seen [$n]\n"); |
120 | } |
554a9ef5 |
121 | return $n; |
122 | } |
123 | my $better = sprintf "v%vd", $n; |
124 | CPAN->debug("n[$n] better[$better]") if $CPAN::DEBUG; |
125 | return $better; |
126 | } |
127 | |
128 | 1; |
129 | |
130 | __END__ |
131 | |
132 | # Local Variables: |
133 | # mode: cperl |
134 | # cperl-indent-level: 2 |
135 | # End: |