Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Answer: How can I get just one character from STDIN?

by fizbin (Chaplain)
on Apr 14, 2003 at 14:50 UTC ( #250301=categorized answer: print w/replies, xml ) Need Help??

Q&A > input and output > How can I get just one character from STDIN? - Answer contributed by fizbin

The basic problem is that the terminal device driver doesn't consider a read to have happened until the user presses a return character - this means that no program is going to be able to read a single character unless they first tell the device driver: "Behave differently, and consider a read complete after a single character".

Hence the suggestion to use Term::ReadKey - it tells the device to behave differently. At a C level, you'd use tcsetattr out of the termios function set. If for some reason you don't think that your deployment environment will have Term::ReadKey installed, you could try a system call to stty; "stty cbreak" will enter the one-character-at-a-time mode and "stty icanon" will take you back. (see stty's man page)

By the way, on Win32 the way I'd approach this is though the InputChar function inside Win32::Console.

  • Comment on Answer: How can I get just one character from STDIN?
Log In?

What's my password?
Create A New User
[1nickt]: hippo lately I can tell just by the fact that Anonymous posts a reply after some hours that it is more than likely That Monk.
[virtualsue]: weird. my p6 simple web spider fails to extract anchor tags from
[holli]: Nice. Turns out I don't have to remove Sweet home Alabama from my playlist ;)
[Corion]: virtualsue: We have some links with a newline between <a and the attributes, maybe your extractor fails to handle that?

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-12-13 11:50 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (360 votes). Check out past polls.