Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know why this doesn't do what is looks like it should do? I want it to sit and wait when it gets to  $a=<>; but instead it magically continues printing the time and STILL takes the input. Someone told me it acted properly for them under win32, others said they got the same results as me... whats going on?!?
use Term::ReadKey; while(1) { while(not defined($firstkey=ReadKey(-1))) { print localtime(); print "\n"; } chomp($a=<STDIN>); if($a eq 'q') { exit; } }

Replies are listed 'Best First'.
Re: While loop insanity
by sauoq (Abbot) on Nov 02, 2002 at 01:32 UTC

    You need to put the terminal in raw mode. Try it like this:

    use Term::ReadKey; while(1) { ReadMode(4); # Set raw mode. while(not defined($firstkey=ReadKey(-1))) { print localtime(); print "\n"; } ReadMode(0); # Restore original mode. chomp($a=<STDIN>); if($a eq 'q') { exit; } }
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: While loop insanity
by BrowserUk (Patriarch) on Nov 02, 2002 at 01:11 UTC

    I'll confirm that this works exactly as you would expect under NT4 sp6/AS v5.6.1 built for MSWin32-x86-multi-thread.

    ie. It loops printing then time until a keystroke is entered, pauses for input, the continues printing the time once the input is completed with the enter key.


    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy