1. Perl
  2. File input/output
  3. here

What is buffering?

The Perl output function print function provides buffering. Buffering means "to store data". Perl's print function does not immediately write its contents to a file when it is actually called. The contents to be written are buffered, and when the buffer exceeds the capacity, it is actually written to a file.

If you want to disable buffering, see the following articles.

The following is an example to experience buffering. You'll see that it won't be written to the file right away.

use strict;
use warnings;

# File for writing (Note that it will be overwritten)
my $file = "example20080811.txt";

open(my $fh, ">", $file)
  or die "Cannot open $file:$!";
    
print $fh "delayed write\n"; # print function call
print "File size not yet". -S $file. "Not written in bytes.\n";
sleep 3;

# Write the buffer to a file at the same time as close(delayed write).
close $fh or die "Cannot close $file";

print "Written at this point. Size is". -S $file. "Bytes.\n";
sleep 3;

(Reference) open function, close function

Perl print function buffering specification

(1) Block buffering

Perl's print function does block buffering by default. (The exception is when printing to the display, which does line buffering. Perl 5.6 and later)

Block buffering is a buffering method that stores data in a buffer up to a certain number of bytes. When the written contents stored in the buffer exceed a certain number of bytes, the file is actually written.

The fixed number of bytes depends on the setting, but I think that it is generally about 4kB. Also, the contents of the buffer are written to the file when the file is closed.

(2) Line buffering

Line buffering is done when printing to the display. The contents to be written are accumulated in the buffer until a line break character appears, and when a line break appears, the contents of the buffer are written to a file.

Buffering and deferred writing

The print function stores the written contents in a buffer and outputs them all at once because the cost of outputting to a file is very high. Writing to a file means accessing the disc.

Disk I/O is much slower than memory I/O, so reducing the number of disk accesses as much as possible will improve program performance.

For this reason, Perl doesn't actually write when you call the print function, it stores up to a certain number of bytes in the write buffer and writes it to a file at once. This is called delayed writing.

Code explanation

(1) Block buffering is performed

print $fh "delayed write\n"; # print function call
print "File size not yet". -S $file. "Not written in bytes.\n";
sleep 3;

I wrote to $fh with the print function, but at this point the content has not been written yet and the file size remains 0 bytes. The written contents are stored in the buffer.

By the way, the output to the display is line buffering, so it will be output when "\n" appears.

(2) Delayed writing is performed

# Write the buffer to a file at the same time as close(delayed write).
close $fh or die "Cannot close $file";

print "Written at this point. Size is". -S $file. "Bytes.\n";
sleep 3;

When $fh is closed, the contents written in the buffer are output to the file. You can see that the delayed write was done.

Related Informatrion