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

Hello. I am just beginning to learn Perl, and I'm having a problem using <STDIN> to get input from the user. Whenever I use <STDIN> in a program (no matter where it appears in the program), the system stops when the program is executed and waits for a response. For example, in this program:
#! C:\Perl print "Enter a number: "; $number = <STDIN>; print "You entered $number";


The system just stops when the program is executed (It does not display the prompt first). If I type a number, then it displays the prompt and continues to execute. How can I fix this problem so that the programs will execute normally. I'm using a Windows XP machine, the ActivePerl port of Perl 5.8, and the Open Perl IDE. I'll try to provide addition information if necessary, but if this issue is not resolved I will not be able to use <STDIN> at all. I was told that placing the following lines before the rest of a program would fix this problem:

use IO::Handle; STDOUT -> autoflush(1); STDERR -> autoflush(1);


However, using this method causes the program to accept any characters on the same line as the entered data as the intended input (including the prompt). Does anyone know a way to fix this STDIN problem? Thank you very much.

Replies are listed 'Best First'.
Re: Problem with STDIN
by Thelonius (Priest) on Jun 11, 2003 at 20:04 UTC
    This is a "feature" of the console widget for OpenPerlIDE. You should ask the maintainer about it or on the mailing list on sourceforge. It is already listed as in the bug list on sourceforge: 457304 Console input sends whole line to STDIN
Re: Problem with STDIN
by perlplexer (Hermit) on Jun 11, 2003 at 19:44 UTC
    Well, it's really not a problem with STDIN. The "problem" is that STDOUT has autoflush turned off by default. What this means is that unless you print() something with a "\n" at the end, it won't appear on the screen. There may be a size limit also; e.g., buffers get flushed when you print() more than, say, 2K, but you normally don't hit that with user prompts.
    Anyway, to fix the issue, add $|++ at the beginning of your program.
    $|++; print "Enter a number: "; $number = <STDIN>; print "You entered $number";
    perldoc perlvar will explain what $| does.

    --perlplexer
Re: Problem with STDIN
by chromatic (Archbishop) on Jun 11, 2003 at 19:45 UTC

    Print a newline at the end of your prompt. Unbuffering STDOUT and STDERR shouldn't make a difference (though I'm not positive on Windows):

    print "Enter a number:\n";
Re: Problem with STDIN
by Anonymous Monk on Jun 11, 2003 at 20:10 UTC
    Thank you perlplexer. Using $|++ allows the prompt to display normally. However, if the prompt and the input are on the same line, the program takes the entire line (prompt and input) as the input. As cromatic said, the obvious solution is to include a newline at the end of each prompt. That is one solution to the problem, but I was wondering if STDIN can be used to get input from a line while not taking in the prompt as well. Again, I don't know much about the nuts and bolts of Perl yet, and my only other programming experience is some very limited knowledge of C++. Anyway, thank you both for your help. If there is a way to read input from a prompt on the same line, please let me know. Otherwise, thank you again for your assistance.
      It is almost certainly not reading the prompt back in. It may look like it is, as an artifact of end-of-line processing. Modify the print statement to look like this and see what you get:

      print "\nYou entered \n|$number|\n";
      See what's between the two '|' symbols, I'll bet it doesn't include the prompt. Or try chomp $number; before the print.

      --Bob Niederman, http://bob-n.com
        bobn, I tried your suggestion, but it didn't fix the problem. However, Thelonius has said that this is a problem with the Open Perl IDE that I used, not Perl itself or my Windows system's port of it. And when I used a freeware Perl frontend for Win32 systems, the problem did not occur.