Chapter 28. Suffering from Buffering

Mark Jason Dominus

“My log file has nothing in it!”

“My output is coming out in the wrong order!”

“When my program terminates abnormally, the output is incomplete!”

“My web server says I didn’t send the right headers, but I’m sure I did!”

“I’m trying to send data over the network, but nothing is sent!”

I’m afraid you’re probably a victim of buffering.

What Is Buffering?

All input and output in your programs is performed by your operating system. When Perl wants to read data from the disk, or to write it to the network, or to read or write data anywhere, Perl has to make a request to the operating system and ask that the data be read or written. This is an example of “making a system call.” (Don’t confuse this with Perl’s system function, which is totally unrelated.)

Making a system call is a relatively slow operation. On top of that, if the data is coming from the disk, you might have to wait for the disk to spin to the right position (the latency time) and you might have to wait for the disk heads to move over to the right track (the seek time). As computer operations go, that wait is unbearably long—typically, several milliseconds. For comparison, a typical computer operation, such as assigning to a variable or adding two numbers, takes a fraction of a microsecond.

Suppose you’re reading a 10,000 line file line-by-line:

	while (<FILE>) { print if /treasure/; }

If Perl made a system call for every read operation, that would be 10,001 system calls in all ...

Get Computer Science & Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.