in reply to Re: split and sysread()
in thread split and sysread()

Take another look. The last line of the loop, $buffer = substr($buffer, rindex($buffer, "\n")); in conjunction with the length $buffer as the fourth parameter to sysread has the effect of grabbing any partial line from the end of the buffer and moving it to the beginning where the next buffer load is then appended.

In this way, every line is processed as a complete line whilst benefiting from reading large chunks from the file without resorting to slurping the whle file into memory.

See my post below for two variations of the algorithm and a benchmark.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.