a weird problem

Z

Zhiliang Hu

I have a perl script that takes a while to run. I inserted a few
lines in the program to print some progress messages to the console
(STDOUT) so I know where it is at, like:

#!/usr/bin/perl
print STDOUT "Please wait "; #-- This is the very first line in the
script
open(FILE,">localfile");
use DBI;
$dbh = DBI->connect("dbi:mysql:.....");
#-- omitted lines: query and fetch etc.
while (@content = $query_a->fetchrow_array) {
#-- some nested stuff omitted, etc etc
print FILE "--some stuff...\n";
print STDOUT "...";
}
print STDOUT "done\n";
$dbh->disconnect;
close(FILE);

But it prints out nothing to the screen until the very end, when it
finishes, it dumps all for STDOUT at once to the screen. The other
parts of the program runs fine. I thought I have used similar schemes
some years ago and it worked nicely but I cannot see what's wrong
here. Something new on perl 5.8.x or what else could be the cause?

Thanks in advance!

Zhiliang
 
J

John W. Krahn

Zhiliang said:
I have a perl script that takes a while to run. I inserted a few
lines in the program to print some progress messages to the console
(STDOUT) so I know where it is at, like:

#!/usr/bin/perl
print STDOUT "Please wait "; #-- This is the very first line in the
script
open(FILE,">localfile");
use DBI;
$dbh = DBI->connect("dbi:mysql:.....");
#-- omitted lines: query and fetch etc.
while (@content = $query_a->fetchrow_array) {
#-- some nested stuff omitted, etc etc
print FILE "--some stuff...\n";
print STDOUT "...";
}
print STDOUT "done\n";
$dbh->disconnect;
close(FILE);

But it prints out nothing to the screen until the very end, when it
finishes, it dumps all for STDOUT at once to the screen. The other
parts of the program runs fine. I thought I have used similar schemes
some years ago and it worked nicely but I cannot see what's wrong
here. Something new on perl 5.8.x or what else could be the cause?

Either use STDERR instead of STDOUT or use autoflush on the STDOUT
filehandle.



John
 
M

Martijn Lievaart

I have a perl script that takes a while to run. I inserted a few lines
in the program to print some progress messages to the console (STDOUT)
so I know where it is at, like:
(snip)

But it prints out nothing to the screen until the very end, when it
finishes, it dumps all for STDOUT at once to the screen. The other
parts of the program runs fine. I thought I have used similar schemes
some years ago and it worked nicely but I cannot see what's wrong here.
Something new on perl 5.8.x or what else could be the cause?

Try:

$|=1; # make pipes hot

See perldoc perlvar.

HTH,
M4
 
T

Tad J McClellan

Zhiliang Hu said:
I have a perl script that takes a while to run. I inserted a few
lines in the program to print some progress messages to the console
(STDOUT) so I know where it is at, like:

#!/usr/bin/perl
print STDOUT "Please wait "; #-- This is the very first line in the
script
open(FILE,">localfile");


You should always, yes *always*, check the return value from open():

open(FILE,">localfile") || die "could not open 'localfile' $!";

You should really be using the 3-argument form of open with
a lexical filehandle:

open my $FILE, '>', 'localfile' or die "could not open 'localfile' $!";



The 3rd line of your program happens before the 1st and 2nd lines.

That can lead to confusion. It would be better to put what happens
first at the beginning of the program rather than in the middle.

print FILE "--some stuff...\n";


print $FILE "--some stuff...\n"; # with a lexical filehandle

print STDOUT "...";

But it prints out nothing to the screen until the very end, when it
finishes, it dumps all for STDOUT at once to the screen.


It is clear that you are

Suffering from Buffering

http://perl.plover.com/FAQs/Buffering.html
 
T

Ted Zlatanov

ML> $|=1; # make pipes hot

I've often wondered why there's no command-line switch for this.

Ted
 
Z

Zhiliang Hu

Many thanks to all for replies, which opened something new to me.

Best regards,

Zhiliang
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top