in reply to $|++ Does What, Exactly? (was: $++ Does What, Exactly?)
open T1, ">>test1.out"; open T2, ">>test1.out"; foreach (0..20) { print T1 "1:$_$/"; print T2 "2:$_$/"; } close T2; #notice close order... close T1; open T1, ">>test1.out"; open T2, ">>test1.out"; select T1; $|=1; select T2; $|=1; foreach (0..20) { print T1 "3:$_$/"; print T2 "4:$_$/"; } close T2; close T1;
The total output of the first for loop is smaller than the print buffers on most machines. You'll wind up with the lines out of order from what you'd expect because the data isn't actually written until the close flushes the buffer to disk.
With both the filehandles set to autoflush, the lines alternate the way you would expect. The benefit to caching is that you get better disk performance. The benefit to flushing is that you get the data written immediately. That can be a big deal if you have multuple time sensitive programs writing to one log file. Imagine if a webserver left caching on while writting the log. you'd get chunks of lines in order from each httpd child and then flip back 3 seconds for the next child's data rather than a smooth temporal stream of requests. Very ugly.
--
$you = new YOU;
honk() if $you->love(perl)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: $++ Does What, Exactly?
by jdgamache (Novice) on Feb 12, 2001 at 19:27 UTC | |
by extremely (Priest) on Feb 12, 2001 at 19:32 UTC |