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

I've currently got a text interface. I want the user to be able to input a single character without pressing return.

For example,

I've been trying to figure out a way to set $/ to make it work, but I haven't been able to figure out how yet.

print qq(Please enter a character: ); $/ = ???; my $ans = <STDIN>; undef $/; print qq(\nYour character is '$ans'\n);
Should yield (but doesn't...)
Please enter a character: x Your character is 'x'
Any help would be appreciated.

Rich36

Replies are listed 'Best First'.
(jeffa) Re: stdin problem
by jeffa (Bishop) on Oct 10, 2001 at 01:31 UTC
    use strict; use Term::ReadKey; print qq(Please enter a character: ); ReadMode 'cbreak'; my $ans = ReadKey(0); ReadMode 'normal'; print qq(\nYour character is '$ans'\n);<p>
    jeffa
      Thanks very much. That worked great. I wouldn't have even known to look for that module...

      Rich36

        perldoc -q key eventually will show the following (which mentions Term::ReadKey in adition to other mechanisms):

        Found in /usr/lib/perl5/5.6.1/pod/perlfaq5.pod How can I read a single character from a file? From the keyboard?
Re: stdin problem
by kschwab (Vicar) on Oct 10, 2001 at 01:28 UTC
    One idea is Term::ReadKey. "perldoc -q keyboard" might give some others.