how to flush a file

D

Data Cruncher

My process writes to the log file. I want to periodically flush it so
that tail -f on the the log file shows the progress on a regular
basis.

This is what I do to write in the log file:-

open(LOGFILE,"> file name") or die ...
print LOGFILE "aaaaa\n" ;
this print is done during the life of the process.
What is happening is that until the process terminates
the log file can not be viewed by tail -f because perl is
still buffering the output to the log file.
and then finally
close(LOGFILE);

How do I flush it. There is no command like flush(LOGFILE).

What I have done is a crude approach of closing the log file
and then reopening it with '>>filename'. As it happens only
once a minute, it is not that expensive. But I am not happy
with this approach. It is not an elegant approach.

TIA.
 
D

Dr.Ruud

Data Cruncher schreef:
My process writes to the log file. I want to periodically flush it so
that tail -f on the the log file shows the progress on a regular
basis.

This is from `perldoc -f select`:

$oldfh = select(STDERR); $| = 1; select($oldfh);

Some programmers may prefer to think of filehandles as objects
with methods, preferring to write the last example as:

use IO::Handle;
STDERR->autoflush(1);
 
U

Uri Guttman

A> Tad McClellan ([email protected]) wrote on MMMMDCCCXCVIII September
A> MCMXCIII in <URL:A> &&
A> && > How do I flush it.
A> &&
A> &&
A> && perldoc -q flush
A> &&
A> && How do I flush/unbuffer an output filehandle? Why must I do this?

A> To be fair to the OP, this FAQ only explains how to unbuffer the handle.
A> It doesn't explain how to flush a filehandle.

A> I have to say, flushing a filehandle in Perl is unintuitive, awkward
A> and phrone to do it while doing unintended side-effects. And, amazingly,
A> it only takes one line in C.

that is because you (and the rest of this thread) are confusing
autoflushing (setting $|) with actual flushing which can be done with
the flush call in IO::Handle. it used to be fflush in POSIX but that
refers to IO::Handle now. so for older perls look for fflush in POSIX.

setting $| may not be a desirable thing as it will flush for each
print. sometimes you do want to let buffering work for you and flush as
needed. i prefer to buffer stuff myself with .= into a string and use
syswrite (or File::Slurp::write_file). it has many benefits including
more control over where and when you write.

uri
 
D

Dr.Ruud

Abigail schreef:
Dr.Ruud:

Well, that will flush STDERR but it has the effect of leaving it
unbuffered. Which may not be what the OP wants.

OP wrote "periodically flush it", so you are right that leaving it
buffered in between can be what he wants.

I'd write it as:

my $old_fh = select STDERR; {local $| = 1;} select $old_fh;

Yes, that is a nice and safe way to do a (single) flush, and restore the
original autoflush-status.
 
B

Bo Lindbergh

But nowhere in the documentation does it say that setting auto-flush
mode for a filehandle results in an immediate flush of that filehandle.

But perldoc perlvar says:
$| If set to nonzero, forces a flush right away and after every
write or print on the currently selected output channel.

What part of that sentence is unclear?


/Bo Lindbergh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top