问题
I am having difficulties in comparing 2 versions. If we have 5.1 and 5.10 then 5.1 should be smaller than 5.10. I know that in decimal it should read 5.01 and 5.10. But is there a way to compare it using 5.1 and 5.10?
perl -e 'use warnings; use version; if (version->parse("5.1") < version->parse("5.10")) { print "ok"; }'
回答1:
The version module documentation shows how to do this:
print version->declare('5.1')->numify; # 5.00100
print version->declare('5.10')->numify; # 5.01000
回答2:
Use the code module CPAN::Version :
use CPAN::Version;
say CPAN::Version->vlt("5.1","5.10") ? "OK" : "KO";
output:
OK
回答3:
I am not sure I understand what you are trying to achieve. Perl has two comparison operators, for numbers and strings. None of them works the way you want. If you just want to avoid using version, you can for example use split:
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
my @v = qw(5.1 5.10);
sub by_version {
my @a = split /\./, $a;
my @b = split /\./, $b;
$a[0] <=> $b[0] or $a[1] <=> $b[1];
}
say for sort by_version @v;
来源:https://stackoverflow.com/questions/19973852/perl-compare-version-numbers-5-1-5-10