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

flushing by default

by spx2 (Deacon)
on Jan 19, 2008 at 17:28 UTC ( #663226=perlquestion: print w/replies, xml ) Need Help??
spx2 has asked for the wisdom of the Perl Monks concerning the following question:


I have spent the last 50 minutes trying to figgure out why
a print to a file didn't work.

I usually do this fast this way:
open my $file,">path"; print $file "stuff";
However this time I had 7 files opened at the same time(not that this should make any difference...).
and the print didn't wanted to work,I have put the open and the print in an eval,tried warn $@ , and no luck.
I have talked with some friends who instructed me into use IO::Handle and ->autoflush(1) the filehandles wich eventually worked.
I am interested in how I can make autoflush a default behaviour for all of the filehandles I open
Does this have any disadvantages ?
Thank you

Replies are listed 'Best First'.
Re: flushing by default
by kirillm (Friar) on Jan 19, 2008 at 17:48 UTC
    I am interested in how I can make autoflush a default behaviour for all of the filehandles I open

    Put this somewhere at the beginning of your program:


    Setting $| to a non-zero value forces a flush right away and after every write or print on the currently selected output channel. IO::Handle makes use of $| as well.

    -- Kirill

      Setting $| to a non zero value will force a fflush()
      to be called on the currently selected output channel.

      Use setbuf to remove the buffer on a single file descriptor.
      yes,but won't this apply just to the handle STDOUT ? will it apply to all handles ?

        Not necessarily on STDOUT, but on the filehandle that is currently selected.

        As for the global default autoflush - I'm not aware of a way to do it. Buffering is usually the default behaviour for the reasons of efficiency.

Re: flushing by default
by DigitalKitty (Parson) on Jan 20, 2008 at 02:33 UTC
Re: flushing by default
by rowdog (Curate) on Jan 19, 2008 at 23:06 UTC
    You could use unbuffered IO by using syswrite instead of print but don't use both. syswrite is also available as part of IO::Handle.
Re: flushing by default
by apl (Monsignor) on Jan 19, 2008 at 18:55 UTC
    Do you have $file defined anywhere else? Perhaps where you did the print was out of the scope where you opened it, and in the scope of another open?

    Did you check to see if "stuff" was in any of the other six files you had open?
Re: flushing by default
by Anonymous Monk on Jan 20, 2008 at 09:44 UTC

    You don't seem to check if open() was successful. Use the common idiom open HANDLE,'>','file' or die "file: $!";. You also don't seem to check the return value of print() for success. And you seem to mix up two error variables: $@ contains the last eval() error, $! contains the last system call error (errno variable in C).


Re: flushing by default
by Zoffix (Acolyte) on Jan 20, 2008 at 15:51 UTC
    Most people close() the file after they are done printing to it which flushes the buffer. You can make default autoflush by setting the $| variable (read perldoc perlvar) You can also localize it. Advantages? No idea. Sometimes you have to set it for certain tasks, but usually I don't set it.
Re: flushing by default
by Lazarus (Novice) on Jan 22, 2008 at 15:28 UTC
    Did you try the outflush at the top of your code?: $|++; I'm so used to it, I almost always put it in whenever I'm coding a new script.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://663226]
Approved by Corion
Front-paged by Corion
marioroy At a large firm, had to awaken a security guy a few times so that nobody would catch him sleeping.
marioroy On a large dairy farm, almost went off the road with a 10 wheeler from exhaustion. On the farm, almost went off the silo with a big tractor. At the Fransiscan monastery, almost slipped off the dome while painting it.
[marioroy]: In Saudi Arabia caught fire but didn't burn. I think angels exist.
Lady_Aleena clean up after dinner finally.
[Discipulus]: uch! so we are twice lucky to have MCE..;=)
[marioroy]: Working a midnight graveyard shift, on the way home ran out of gas. just 2 minutes later somebody stops with a small tank of gas. It's wierd. There's always someone nearby for some reason.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-05-29 08:24 GMT
Find Nodes?
    Voting Booth?