Skip to content

Commit f23628c

Browse files
committed
a short perl distribution version parsing subroutine.
1 parent cb9538e commit f23628c

File tree

2 files changed

+131
-4
lines changed

2 files changed

+131
-4
lines changed

lib/App/perlbrew.pm

+3-4
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ sub perl_version_to_integer {
100100
my $version = shift;
101101
my @v = split(/[\.\-_]/, $version);
102102
if ($v[1] <= 5) {
103-
$v[2] ||= 9;
103+
$v[2] ||= 0;
104104
$v[3] = 0;
105105
}
106106
else {
107-
$v[3] ||= 9;
107+
$v[3] ||= $v[1] >= 6 ? 9 : 0;
108108
$v[3] =~ s/[^0-9]//g;
109109
}
110110

111-
return $v[0]*10000000 + $v[1]*10000 + $v[2]*10 + $v[3];
111+
return $v[1]*1000000 + $v[2]*1000 + $v[3];
112112
}
113113

114114
sub new {
@@ -169,7 +169,6 @@ sub new {
169169
return bless \%opt, $class;
170170
}
171171

172-
173172
sub root {
174173
my ($self, $new_root) = @_;
175174

t/14.perl_version_parsing.t

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!perl
2+
use strict;
3+
use App::perlbrew;
4+
use File::Temp qw( tempdir );
5+
$App::perlbrew::PERLBREW_ROOT = tempdir( CLEANUP => 1 );
6+
$App::perlbrew::PERLBREW_HOME = tempdir( CLEANUP => 1 );
7+
$ENV{PERLBREW_ROOT} = $App::perlbrew::PERLBREW_ROOT;
8+
9+
#
10+
# This test checks if the sorting order of parsed version is the same as
11+
# the order of @versions array defined below.
12+
#
13+
14+
my @versions = qw(
15+
5.003_07
16+
5.004
17+
5.004_01
18+
5.004_02
19+
5.004_03
20+
5.004_04
21+
5.004_05
22+
5.005
23+
5.005_01
24+
5.005_02
25+
5.005_03
26+
5.005_04
27+
5.6.0
28+
5.6.1-TRIAL1
29+
5.6.1-TRIAL2
30+
5.6.1-TRIAL3
31+
5.6.1
32+
5.6.2
33+
5.7.0
34+
5.7.1
35+
5.7.2
36+
5.7.3
37+
5.8.0
38+
5.8.1
39+
5.8.2
40+
5.8.3
41+
5.8.4
42+
5.8.5
43+
5.8.6
44+
5.8.7
45+
5.8.8
46+
5.8.9
47+
5.9.0
48+
5.9.1
49+
5.9.2
50+
5.9.3
51+
5.9.4
52+
5.9.5
53+
5.10.0-RC1
54+
5.10.0-RC2
55+
5.10.0
56+
5.10.1
57+
5.11.0
58+
5.11.1
59+
5.11.2
60+
5.11.3
61+
5.11.4
62+
5.11.5
63+
5.12.0
64+
5.12.1-RC1
65+
5.12.1-RC2
66+
5.12.1
67+
5.12.2-RC1
68+
5.12.2
69+
5.12.3
70+
5.12.4-RC1
71+
5.12.4-RC2
72+
5.12.4
73+
5.13.0
74+
5.13.1
75+
5.13.2
76+
5.13.3
77+
5.13.4
78+
5.13.5
79+
5.13.6
80+
5.13.7
81+
5.13.8
82+
5.13.9
83+
5.13.10
84+
5.13.11
85+
5.14.0-RC1
86+
5.14.0-RC2
87+
5.14.0-RC3
88+
5.14.0
89+
5.14.1-RC1
90+
5.14.1
91+
5.14.2-RC1
92+
5.14.2
93+
5.14.3-RC1
94+
5.14.3-RC2
95+
5.14.3
96+
5.15.0
97+
5.15.1
98+
5.15.2
99+
5.15.3
100+
5.15.4
101+
5.15.5
102+
5.15.6
103+
5.15.7
104+
5.15.8
105+
5.15.9
106+
5.16.0-RC0
107+
5.16.0-RC1
108+
5.16.0-RC2
109+
5.16.0
110+
5.16.1-RC1
111+
5.16.1
112+
5.16.2-RC1
113+
5.17.0
114+
5.17.1
115+
5.17.2
116+
5.17.3
117+
5.17.4
118+
5.17.5
119+
);
120+
121+
use Test::More;
122+
123+
plan tests => 0+@versions;
124+
125+
my @versions_i = sort { $a->[0] <=> $b->[0] } map { [App::perlbrew::perl_version_to_integer($_), $_] } @versions;
126+
for my $i (0..$#versions) {
127+
is $versions[$i], $versions_i[$i]->[1];
128+
}

0 commit comments

Comments
 (0)