ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I've written a code which should do the following function :till the window which is matched is active my script should be paused and as soon as that window ends my script should end.

use strict; use warnings; use Win32::GuiTest qw(:ALL); my @window=FindWindowLike(undef,"Calculator"); while($window[0]) { print("calculator window is open"); sleep 2; }

Now this script is not performing the function I require it to do. what is happening is it is going in a infinite loop and after the calculator window is closed then also it is printing "calculator window is open" with 2 seconds gap each time and the script keeps running.

P.S. calculator is taken as an example only. I've tried this with few other windows as well. it does not work.

P.S. I am not getting any error message as the script keeps running and does not end.

Any suggestions or help is appreciated! Thanks in advance .

Replies are listed 'Best First'.
Re: Win32::GuiTest module application problem
by GrandFather (Saint) on Oct 19, 2016 at 07:39 UTC

    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

      got my mistake. Thanks a lot for you guidance and help

Re: Win32::GuiTest module application problem
by Discipulus (Canon) on Oct 19, 2016 at 07:24 UTC
    this is a downside effect of DWIM when Do What I Mean differs from What That I want..

    The code is doing exaactly what you instructed to to do: with while($window[0]) you say "during the time it is defined my first found window.." ie is an infinite loop.

    The condition is checked continuosly and while it is true the program is expected to print your calculator window is open sentences. It is expected but it does not do; why? because the buffer it is not full. Generally speking command line programs are line buffered. Try to add a newline at the end of your sentence and see what happens. You can also force a non buffered output using $| and assigning somenthi true to it; the idiom is $|++; see perlvar

    For the buffer implication see the Suffering from buffering? milestone article.

    For Win32::GuiTest at the end of the module documentation is linked the site of the author which contains good winguitest tutorials

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.