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

I face the problem of not getting the complete input when reading from STDIN when using select and <STDIN>. Usage: prompt>A.pl | B.pl I have script A output piped to Script B. And I use select call to find out when there is input in the STDIN and then call my $input=<STDIN> when select says that there is data to read. The problem that is faced is that the last 2 lines or sometimes last line is not coming up in the script B. There is no problem with the Script A, since the command A.pl | cat prints all the lines. The problem is only with Select and STDIN. Select doesn't recognize the last line of the input and doesn't say that the STDIN is ready for reading even though data is present. I modified the script in such way that I commented out all select portions and instead had while(1) { my $input=<STDIN>}, the program gets blocked, but it gets the entire input. Any help will be greatly appreciated.
  • Comment on Select not recognizing input from STDIN

Replies are listed 'Best First'.
Re: Select not recognizing input from STDIN
by zwon (Abbot) on Sep 26, 2009 at 09:31 UTC

    There are some useful tips and links under the textarea into which you typed your question:

    Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!

    You've posted the code snippet that works for you:

    while(1) { my $input=<STDIN>}
    But you want to get help for code that doesn't work, and you haven't posted that code. Can you show us how you're using the select?

    Update: Oh! In general it is not a good idea to mix select with buffered operations like <STDIN>. See the warning in the end of select function documentation. The problem is that the last line has been read already and placed into the buffer when you're calling select, and select doesn't know about buffer, it checks only file descriptor.