The issue is that you only search for the window once. $window[0] doesn't automatically update as the window state changes. You need to test each time through the loop. The test code cleans up a little by putting it in a sub:
use strict;
use warnings;
use Win32::GuiTest;
while (my $window = findWindow("Calculator")) {
print("calculator window is open\n");
sleep 2;
}
print "Calculator window closed\n";
sub findWindow {
my ($caption) = @_;
my @windows = Win32::GuiTest::FindWindowLike(undef, "Calculator");
return $windows[0];
}
Prints (for my test):
calculator window is open
calculator window is open
calculator window is open
calculator window is open
Calculator window closed
Premature optimization is the root of all job security