O'Reilly logo

Computer Science & Perl Programming by Jon Orwant

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required