Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

flushing by default

by spx2 (Chaplain)
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:

Hi,

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

Comment on flushing by default
Select or Download Code
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

      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.

      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.
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 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 DigitalKitty (Parson) on Jan 20, 2008 at 02:33 UTC
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).

    Alexander

Re: flushing by default
by Zoffix (Initiate) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://663226]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-08-21 16:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (136 votes), past polls