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