Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: $++ Does What, Exactly?

by extremely (Priest)
on Feb 10, 2001 at 11:36 UTC ( [id://57598]=note: print w/replies, xml ) Need Help??


in reply to $|++ Does What, Exactly? (was: $++ Does What, Exactly?)

Are you on Unix? (not sure how WinXx reacts) Try this:
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
    It reacts well on Windows. Why should it react weirdly under windows ?
      Well, because a lot of Perl's behavior on the IO side is dictated by the C stdio library and the OS's defaults. For all I know some Windows platforms or C libs may default autoflush to on for all handles when the program is run at console. You never really know about some of these little things and since I didn't have a WinXx box to test it on, I threw in a caveat. With the weirder aspects of any cross-platform system, you need to be certain that you aren't seeing the platform and not the system.

      Just for fun, goto Super Search and look for "Windows fork" in article bodies.

      --
      $you = new YOU;
      honk() if $you->love(perl)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://57598]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found