a weird problem

Discussion in 'Perl Misc' started by Zhiliang Hu, Aug 6, 2008.

  1. Zhiliang Hu

    Zhiliang Hu Guest

    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
    Zhiliang Hu, Aug 6, 2008
    #1
    1. Advertising

  2. 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
    John W. Krahn, Aug 6, 2008
    #2
    1. Advertising

  3. 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
    Martijn Lievaart, Aug 6, 2008
    #3
  4. 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");



    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/"
    Tad J McClellan, Aug 7, 2008
    #4
  5. Zhiliang Hu

    Ted Zlatanov Guest

    make pipes hot (was: a weird problem)

    On Wed, 6 Aug 2008 23:54:02 +0200 Martijn Lievaart <> wrote:

    ML> $|=1; # make pipes hot

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

    Ted
    Ted Zlatanov, Aug 7, 2008
    #5
  6. Zhiliang Hu

    Zhiliang Hu Guest

    Re: make pipes hot (was: a weird problem)

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

    Best regards,

    Zhiliang
    Zhiliang Hu, Aug 7, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dorayme
    Replies:
    1
    Views:
    618
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    578
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    613
    richard
    Jan 21, 2011
  4. Beauregard T. Shagnasty

    Re: A Weird Appearance for a Weird Site

    Beauregard T. Shagnasty, Jan 21, 2011, in forum: HTML
    Replies:
    1
    Views:
    436
    Captain Paralytic
    Jan 21, 2011
  5. will
    Replies:
    6
    Views:
    407
    Phrogz
    Dec 27, 2006
Loading...

Share This Page