Skip to content

Commit

Permalink
fix version parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
haarg committed Sep 22, 2014
1 parent 8fc9ffa commit bf646f1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
30 changes: 17 additions & 13 deletions lib/MetaCPAN/Util.pm
Expand Up @@ -17,24 +17,28 @@ sub digest {

sub numify_version {
my $version = shift;
use warnings FATAL => 'numeric';
eval { $version = version->parse($version)->numify + 0; } or do {
$version = fix_version($version);
$version = eval { version->parse( $version || 0 )->numify + 0 };
};
$version = fix_version($version);
$version =~ s/_//g;
if ($version =~ s/^v//i || $version =~ tr/.// > 1) {
my @parts = split /\./, $version;
my $n = shift @parts;
$version = sprintf(join('.', '%s', ('%03s' x @parts)), $n, @parts);
}
$version += 0;
return $version;
}

sub fix_version {
my $version = shift;
return undef unless ( defined $version );
if ( $version =~ /^v/ ) {
eval { $version = eval( version->parse($version)->numify ) };
return $version + 0 unless ($@);
}
$version =~ s/[^\d\._]//g;
$version =~ s/_/00/g;
return $version;
return 0 unless defined $version;
my $v = ($version =~ s/^v//i);
$version =~ s/[^\d\._].*//;
$version =~ s/\.[._]+/./;
$version =~ s/[._]*_[._]*/_/g;
$version =~ s/\.{2,}/./g;
$v ||= $version =~ tr/.// > 1;
$version ||= 0;
return (($v ? 'v' : '') . $version);
}

sub author_dir {
Expand Down
14 changes: 7 additions & 7 deletions t/util.t
Expand Up @@ -10,15 +10,15 @@ is( MetaCPAN::Util::numify_version('v2.1.1'), 2.001001 );
is( MetaCPAN::Util::numify_version(undef), 0.000 );
is( MetaCPAN::Util::numify_version('LATEST'), 0.000 );
is( MetaCPAN::Util::numify_version('0.20_8'), 0.208 );
is( MetaCPAN::Util::numify_version('0.20_88'), 0.200088 );
is( MetaCPAN::Util::numify_version('0.208_8'), 0.208008 );
is( MetaCPAN::Util::numify_version('0.20_108'), 0.2000108 );
is( MetaCPAN::Util::numify_version('v0.9_9'), 0.009009 );
is( MetaCPAN::Util::numify_version('0.20_88'), 0.2088 );
is( MetaCPAN::Util::numify_version('0.208_8'), 0.2088 );
is( MetaCPAN::Util::numify_version('0.20_108'), 0.20108 );
is( MetaCPAN::Util::numify_version('v0.9_9'), 0.099 );

lives_ok { is( version("2a"), 2 ) };
lives_ok { is( version("V0.01"), 0.01 ) };
lives_ok { is( version('0.99_1'), '0.99001' ) };
lives_ok { is( version('0.99.01'), '0.99.01' ) };
lives_ok { is( version("V0.01"), 'v0.01' ) };
lives_ok { is( version('0.99_1'), '0.99_1' ) };
lives_ok { is( version('0.99.01'), 'v0.99.01' ) };

is( MetaCPAN::Util::strip_pod('hello L<link|http://www.google.com> foo'),
'hello link foo' );
Expand Down

2 comments on commit bf646f1

@monken
Copy link
Contributor

@monken monken commented on bf646f1 Sep 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should stay compatible with version.pm

    perl -Mversion -e "print version->parse('v0.9_9')->numify"
    0.009_009

So the original test seems to be correct.
What would v0.9_10 return in your case, and wouldn't that cause wrong ordering?

@haarg
Copy link
Member Author

@haarg haarg commented on bf646f1 Sep 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handling of this is based on the discussions we had in Lyon (https://gist.github.com/dagolden/9559280).

The handling of underscores in normal form versions could be changed, but we still wouldn't be entirely compatible with version.pm because these routines avoid version.pm's bugs.

Please sign in to comment.