简体   繁体   English

Perl 新手 - 在进行浮点比较时遇到问题

[英]new to Perl - having trouble with a floating point comparison

I'm new to Perl, writing a simple script that checks to see if the installed version of VMware Fusion is greater than 8.4.我是 Perl 的新手,正在编写一个简单的脚本来检查已安装的 VMware Fusion 版本是否大于 8.4。 I'm getting this error:我收到此错误:

syntax error at ./test.pl line 8, near ");"

This is the script:这是脚本:

 @vmVersion = `defaults read /Applications/VMware\ Fusion.app/Contents/Info CFBundleShortVersionString`;

 @vmMinimum = 8.4;

if($vmVersion > $vmMinimum);

then print "compliant";

 else print "update required";

 fi

That code you have isn't Perl.您拥有的代码不是 Perl。 Here's what might look more like Perl:下面是看起来更像 Perl 的东西:

my $vmVersion = `defaults read '/Applications/VMware Fusion.app/Contents/Info' CFBundleShortVersionString`;
if ($vmVersion > 8.4) {
    print "compliant";
} else {
    print "update required";
}

Note two things:注意两点:

  1. Version numbers are not floating point numbers.版本号不是浮点数。 You should really use a semantic version library, such as SemVer , for version comparison.您真的应该使用语义版本库,例如SemVer ,进行版本比较。
  2. Your messages should probably include newline characters ( \\n ).您的消息可能应该包含换行符 ( \\n )。 Without those, when a user runs your program, their prompt will print in a funny location afterwards.如果没有这些,当用户运行您的程序时,他们的提示将在之后打印在一个有趣的位置。 :-) :-)

I've never seen我从没见过

if($vmVersion > $vmMinimum);
...
fi

In perl before, replace with block braces.在 perl 之前,用块大括号替换。

if($vmVersion > $vmMinimum) {
...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM