When STDOUT is connected to a terminal then it is "hot" by default and perl flushes its buffer when "\n" is written, but when it is not connected to a terminal, as when you redirect STDOUT to the file, then it is not "hot" by default and perl does not, by default, flush its buffer when "\n" is written. You can make it "hot" by setting "$|" to a true value (e.g. 1).
You can use strace, on linux, to see when perl writes to the system. When I run your test program under strace I get the following:
[ian@alula ~]$ strace perl ./test.pl >test.log ... write(1, "From C: 1\n", 10) = 10 write(1, "From C: 3\n", 10) = 10 write(1, "From C: 5\n", 10) = 10 write(1, "From C: 7\n", 10) = 10 write(1, "From C: 9\n", 10) = 10 write(1, "From perl: 0\nFrom perl: 2\nFrom p"..., 65) = 65 exit_group(0) = ?
Note that all the output from perl appears in a single write to the system. In this case, perl flushes its partially filled buffer as it prepares to exit.
If I add "$| = 1;", then the strace output ends as follows.
write(1, "From C: 1\n", 10) = 10 write(1, "From perl: 2\n", 13) = 13 write(1, "From C: 3\n", 10) = 10 write(1, "From perl: 4\n", 13) = 13 write(1, "From C: 5\n", 10) = 10 write(1, "From perl: 6\n", 13) = 13 write(1, "From C: 7\n", 10) = 10 write(1, "From perl: 8\n", 13) = 13 write(1, "From C: 9\n", 10) = 10 exit_group(0) = ?
In this case, because STDOUT was made "hot" by setting $| to 1, perl flushed its buffer to the system every time "\n" was written.
In reply to Re^3: Buffered, bruised and broken
by ig
in thread Buffered, bruised and broken
by syphilis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |