Perl while循环不按预期运行

时间:2015-08-04 15:04:08

标签: perl while-loop vmware-tools

我正在使用带有睡眠的while循环来检查事件何时发生(我正在启动虚拟机,然后使用循环等待并确保它已启动并启动)。

我的循环如下:

my $vm_powerState = $vm_view->runtime->powerState->val;
my $vm_toolsStatus = $vm_view->guest->toolsStatus->val;
while (
    ($vm_powerState ne "poweredOn")
 && ($vm_toolsStatus ne "toolsOk")
) {
    debug($vmname);
    $vm_view = Vim::find_entity_view(
        view_type => 'VirtualMachine',
        filter => {"config.name" => $vmname }
    );
    debug ($vm_powerState . "\nbla\n" . $vm_toolsStatus);
    # $vm_toolsStatus = $vm_view->guest->toolsStatus->val
    if (my $ref = eval { $vm_view->can("guest")} )
    {
        print "OK\n";
        print $vm_view->guest->toolsStatus->val;
        $vm_toolsStatus = $vm_view->guest->toolsStatus->val;
    }
    $vm_powerState = $vm_view->runtime->powerState->val if (defined($vm_view));
    debug($vm_powerState . "\n\nbla222\n\n" . $vm_toolsStatus);
    debug("...");
    sleep(1);
}
debug ($vm_toolsStatus);
debug ("VM is ON");

简而言之,我获取VM的状态(默认情况下为poweredOff和toolsNotRunning),然后循环直到它们都具有良好的值(poweredOn和toolsOk)。应该工作吧?

嗯,不。在powerState更改为poweredOn之后的第一个循环,循环结束,即使toolsStatus是" toolsNotRunning"。

这是我输出的主要部分:

$VAR1 = \'VM TESTVM2is being turned on';
$VAR1 = \'Waiting for the VM to turn on and boot, it might take some time';
$VAR1 = \'TESTVM2';
$VAR1 = \'poweredOff
bla
toolsNotRunning';
OK
toolsNotRunning$VAR1 = \'poweredOn

bla222

toolsNotRunning';
$VAR1 = \'...';
$VAR1 = \'toolsNotRunning';
$VAR1 = \'VM is ON';

正如您所看到的,即使明确不满足while循环的第二个条件,程序也会在powerState的值上电后立即退出。

为什么会发生这种情况?

2 个答案:

答案 0 :(得分:3)

($vm_powerState ne "poweredOn") && ($vm_toolsStatus ne "toolsOk")
FALSE && TRUE
FALSE

您应该使用||代替&&

!(A && B)相当于(!A || !B)

编写此循环的更好方法是:

until ($vm_powerState eq "poweredOn" && $vm_toolsStatus eq "toolsOk") {
  ...
}

答案 1 :(得分:2)

你想循环直到

$vm_powerState eq "poweredOn" && $vm_toolsStatus eq "toolsOk"

这意味着你想在

时循环
!( $vm_powerState eq "poweredOn" && $vm_toolsStatus eq "toolsOk" )

等效:

!( $vm_powerState eq "poweredOn" ) || !( $vm_toolsStatus eq "toolsOk" )

等效:

$vm_powerState ne "poweredOn" || $vm_toolsStatus ne "toolsOk"

换句话说,只要两者都有错误的值,就要循环,但只要两者都有错误的值,就会循环。