Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieving only the first character from STDIN

Status
Not open for further replies.

k1ng0fn3rd

Programmer
Aug 6, 2006
17
US
I'm trying to find a function in perl that takes only the first character typed in STDIN, returns it, then just moves on, like the C++ getch().
Is there any way to do this?
 
There is although it's almost always a bad idea:
Code:
read stdin, $_, 1;

Maybe if you told us what you were trying to achieve you might get a much better answer and solution to your problem.
Perl tends to perform very badly when trying to process one character at a time and realistically it's almost never necessary.


Trojan.
 
The result from that code isn't quite what I was aiming for. I was looking for a way to retrieve the first character of input, which is exactly what that does. But I was also looking for a way for that to be all the user could type. The program would then move on once one character was typed.
 
look up Term::ReadKey on should be all you can eat for what you want ;)

If you're more specific, we can be more specific with the responses, note Trojan's codacil "Maybe if you told us what you were trying to achieve you might get a much better answer and solution to your problem."

HTH



Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Maybe this isn't the best question to ask. I guess I was just curious... It just seemed like the users of certain programs should be able to only type one character and let the program do the rest... I don't know...
 
CPAN said:
ReadKey MODE [, Filehandle]

Takes an integer argument, which can currently be one of the following values:

0 Perform a normal read using getc
-1 Perform a non-blocked read
>0 Perform a timed read
It's all there ...

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Notice that it uses getc, which, as stated in the perl documentation:

GETC
Returns the next character from the input file attached to FILEHANDLE, or the undefined value at end of file, or if there was an error (in the latter case $! is set). If FILEHANDLE is omitted, reads from STDIN. This is not particularly efficient. However, it cannot be used by itself to fetch single characters without waiting for the user to hit enter.
perldoc.perl.org
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top