Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Read from STDIN and stay on same line

by kschwab (Vicar)
on Dec 26, 2018 at 15:51 UTC ( [id://1227715]=note: print w/replies, xml ) Need Help??


in reply to Read from STDIN and stay on same line

You ask for no special module, but Term::ReadKey would be much easier. That said:
use POSIX; use strict; use warnings; my $FD = fileno(STDIN); my $TERM = POSIX::Termios->new(); $TERM->getattr($FD); my $OTERM = $TERM->getlflag(); my $ECHO = ECHO|ECHOK|ICANON; my $NOECHO = $OTERM&~$ECHO; select STDOUT;$| = 1; sub cbreak { $TERM->setlflag($NOECHO); $TERM->setcc(VTIME, 1); $TERM->setattr($FD, TCSANOW); } sub cooked { $TERM->setlflag($OTERM); $TERM->setcc(VTIME, 0); $TERM->setattr($FD, TCSANOW); } my $key = ''; my $answer = ''; cbreak(); print "How much is A? "; while(1) { sysread(STDIN, $key, 1); last if $key eq "\n"; syswrite(STDOUT,$key); $answer .= $key } syswrite(STDOUT," A:$answer\n"); cooked(); END { cooked() }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1227715]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-23 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found