比较版本而不使用版本CPAN模块

Che*_*tan -1 perl

假设我有2个版本变量:

$one = "1.2.3"
$two = "1.2.4"
Run Code Online (Sandbox Code Playgroud)

如何比较这两个版本变量的版本是否匹配,或者一>两个或一<两个​​?

请注意,我有一个只读系统,无法添加任何Perl模块。因此,我无法添加versionPerl模块。

ike*_*ami 7

For the strings you posted, the standard string comparison operators (cmp, lt, etc) will work as long as every component is less than 10.

For the strings you posted, the standard string comparison operators (cmp, lt, etc) will work as long as every component is less than 232 if you transform them using pack('N*', split(/\./, $v)) first.

But that won't work for all versions commonly seen in the Perl world. If that's the kind of versions with which you must deal, use the version module. (This doesn't require installing anything from CPAN, so it meets your demands.)

my $v1 = version->parse($one);
my $v2 = version->parse($two);
Run Code Online (Sandbox Code Playgroud)

The created objects override the numerical and string comparison operators so that they may be used to compare the objects.