Perl: the Markov chain saw | |
PerlMonks |
Re^3: Non-blocking I/O woesby juster (Friar) |
on Jan 11, 2011 at 15:41 UTC ( [id://881695]=note: print w/replies, xml ) | Need Help?? |
The bug doesn't happen because perl has STDIN line-buffered. Select won't notice STDIN has read data until you press enter. This also means there will always be a newline to read for <STDIN>.
sysread will give you the unbuffered activity you expect but select will not. I think this is where you are mistaken. This means select will still be blocking until you press enter. I was able to get select to trigger on a single character by using tcsetattr to disable line-buffering (aka canonical mode) for the terminal. (code at the end of the post) edit: Just to be clear about what this implies, this leads me to believe that line-buffering happens at a lower level than the perl interpreter. This answers your rhetorical question: How can the select system call know that Perl has data waiting to be read in a buffer? It is my hypothesis that the buffer you speak of happens at the system level and not inside the perl interpreter. Using stdio.h functions like getc and getline, you must read input through the terminal's internal buffer while in canonical mode. The read system call (via perl's sysread) does not use this higher-level terminal line buffer. I have no idea how this works in windows.
In Section
Seekers of Perl Wisdom
|
|