

Task

Obtain a valid   Y   or   N   response from the keyboard.

The keyboard should be flushed, so that any outstanding key-presses are removed, preventing any existing   Y   or   N   key-press from being evaluated.

The response should be obtained as soon as   Y   or   N   are pressed, and there should be no need to press an   enter   key.




my $TTY = open("/dev/tty");
 
sub prompt-char($prompt) {
    ENTER shell "stty raw -echo min 1 time 1";
    LEAVE shell "stty sane";
 
    print $prompt;
    $TTY.read(1).decode('latin1');
}
 
say so prompt-char("Y or N? ") ~~ /:i y/;