Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > a weird problem

Reply
Thread Tools

a weird problem

 
 
Zhiliang Hu
Guest
Posts: n/a
 
      08-06-2008
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
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      08-06-2008
Zhiliang Hu wrote:
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
Martijn Lievaart
Guest
Posts: n/a
 
      08-06-2008
On Wed, 06 Aug 2008 10:49:04 -0700, Zhiliang Hu wrote:

> 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
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      08-07-2008
Zhiliang Hu <(E-Mail Removed)> wrote:
> 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' $!";


> use DBI;



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


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      08-07-2008
On Wed, 6 Aug 2008 23:54:02 +0200 Martijn Lievaart <(E-Mail Removed)> wrote:

ML> $|=1; # make pipes hot

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

Ted
 
Reply With Quote
 
Zhiliang Hu
Guest
Posts: n/a
 
      08-07-2008
Many thanks to all for replies, which opened something new to me.

Best regards,

Zhiliang
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: A Weird Appearance for a Weird Site Beauregard T. Shagnasty HTML 1 01-21-2011 04:17 PM
Re: A Weird Appearance for a Weird Site richard HTML 0 01-21-2011 07:10 AM
Re: A Weird Appearance for a Weird Site dorayme HTML 1 01-21-2011 06:51 AM
Re: A Weird Appearance for a Weird Site richard HTML 0 01-21-2011 06:46 AM
newbie with a weird technical problem (@ least I think it's weird) will Ruby 6 12-27-2006 04:46 PM



Advertisments