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

Hi monks...

i would like to read-from-STDIN normally and remain on the same line so the next 'print' will be in the same line as the read-from-STDIN

I have the next code:

print "How much is A:"; my $a=<STDIN>; chomp $a; print " A=$a";

that produces an output like this (assuming i type the number 25):

How much is A:25 A=25

but i want a result like:

How much is A:25 A=25
How can i do that? is it possible without any especial module? Thanks...

Replies are listed 'Best First'.
Re: Read from STDIN and stay on same line
by Discipulus (Canon) on Dec 26, 2018 at 15:34 UTC
    Hello pedrete,

    you cant, without modules. Infact what can terminate your input if not a newline ( or a CTRL-Z on empty line )?

    But with Term::ReadKey you can read a char at time:

    use strict; use warnings; use Term::ReadKey; $|++; #you must autoflush buffer or it print only at newlines ReadMode('cbreak'); print "How much is A:"; my $input; my $char; while ( $char = ReadKey(0) and $char =~/^\d$/ ){ $input.=$char; print $char; } print " A=$input\n";

    In the above code any non digit entered by the user will stop the while loop and you can print on the same line.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Read from STDIN and stay on same line
by kschwab (Vicar) on Dec 26, 2018 at 15:51 UTC
    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() }
Re: Read from STDIN and stay on same line
by johngg (Canon) on Dec 26, 2018 at 15:39 UTC

    It may be that the getc man page will point you in the right direction. The tricky part will be in setting and reverting terminal modes without employing a module like Term::ReadKey.

    Cheers,

    JohnGG

Re: Read from STDIN and stay on same line
by haukex (Archbishop) on Dec 27, 2018 at 08:10 UTC

    You've seen how (relatively) long the code to read from the terminal without the newline is, and you've asked for how to do it without a special module, but why? Yes, even you can use CPAN...

    Since I was just playing around with it the other day, here's a solution using IO::Prompter, nice and short - I've added the -num option so that it even validates the input for you and limits it to numbers.

    use IO::Prompter; my $num = prompt("How much is A:", -num, -return=>" "); print "A=$num\n";
Re: Read from STDIN and stay on same line
by Anonymous Monk on Dec 26, 2018 at 15:39 UTC

    You would have to set terminal attributes: enable non-canonical mode to get each character (not line) that the user is typing, disable echo so that the entered newline won't be automatically printed back; read characters in a loop until you get the newline. See POSIX::Termios on how to do that in Perl on a POSIX-compliant system and perhaps look in Win32::Console for a similar thing on Windows.

    Search CPAN for keyword "propmt" to see if someone else has already implemented this functionality.

Re: Read from STDIN and stay on same line
by pedrete (Sexton) on Dec 26, 2018 at 16:20 UTC
    Thanks everybody... i have a better idea now...