Haskell default io buffering
The GHC runtime system tries to be clever when it chooses the default buffering. If it looks like stdin and stdout are directly connected to terminal, they will be line-buffered. If it looks like they are connected to something else, they are block-buffered. This can be problematic if you want to run a program with line-by-line input that doesn't come directly from a terminal. For example, I think that cat | your-program
behaves differently from just your-program
.
Do i have to set manually the buffering if i want to write a working program with both running method?
Yes.
It's not specific to Haskell (e.g. the standard C library does the same thing).
Traditionally, if a file descriptor corresponds to the terminal, buffering is set to line mode, otherwise to block mode. File descriptor type can be checked by the isatty(3)
function -- not sure if it is exported to System.IO
.
And yes, you need to set buffering mode manually if you depend on it.
By the way, you can cheat the system and force block buffering in the command line by running your program as cat | ./prog | cat
.