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

I would like to include that favorite mantra "Press any key to continue" in one of my programs. So, I stupidly did
my $key; while(undef($key)) { $key = <STDIN>; }
which, obviously, did not work. I needed something that would not wait for a return. So, I started searching around for some non-blocking I/O calls; and I found this reply in this thread. So, I got the "any key phenomenon" to work with this code:
my $key; my $term = new POSIX::Termios; $term->setlflag(~ICANON); # turn canonical mode off $term->setcc(VMIN,1); # read 1 char min $term->setcc(VTIME,0); # no timeout $term->setattr(0,TCSANOW); # apply it to STDIN while( read(STDIN,$key,1) ) { last if(defined($key)); }
So, I had that working wonderfully. However, my terminal was going crazy. It would ignore CTRL-C and the like, it would interpret a return as ^M, etc. So, I decided to change the I/O gathering back to normal using the following code that directly follows the above while loop:
$term->setlflag(ICANON); # set back to canonical mode $term->setattr(0,TCSANOW); # apply it to STDIN undef($term); # cleaning up
But, alas, that does nothing. How do you reset STDIN? And, just in case, if anyone else has a better idea about implementing "press any key," I am all ears.

Jeremy

Replies are listed 'Best First'.
Re: Where Is The 'AnyKey'?
by yakko (Friar) on Mar 06, 2001 at 02:10 UTC
    I believe you want to see Term::ReadKey for what you're trying to do.

    --
    Me spell chucker work grate. Need grandma chicken.

      Yea, using Term::ReadKey would definitely be a better way to go; and while searching, I came across it. However, I was trying to write this script such that it would run with the base Perl install (no extra modules). It will end up running on various machines being run by various users with and without necessary skill-sets. Basically, I do not want to rely on a user and/or computer having or obtaining the module.

      But, if it comes down to it. I will do it that way. (Or, I will just say, press the "Enter" key to continue.) But, who wants to be simple. The fact that I cannot do it is what is drving me crazy. Thanks, though.

      Jeremy
Re: Where Is The 'AnyKey'?
by strredwolf (Chaplain) on Mar 06, 2001 at 03:12 UTC
    "Press [ENTER] or [RETURN] to continue."

    --
    $Stalag99{"URL"}="http://stalag99.keenspace.com";