console print issue

Discussion in 'Perl Misc' started by Bigus, Jul 6, 2004.

  1. Bigus

    Bigus Guest

    This is driving me nuts and I've had it working in different scripts
    before.. here's the code:

    for($i=0;$i<5;$i++){
    sleep 2;
    print ">";
    }

    that should cause an angle bracket to be printed on the commandline every 2
    seconds, but instead all 5 are printed at the same time when it's finished
    the loop. Can anyone shed any light on why?

    Bigus
    Bigus, Jul 6, 2004
    #1
    1. Advertising

  2. > that should cause an angle bracket to be printed on the commandline every
    2
    > seconds, but instead all 5 are printed at the same time when it's finished
    > the loop. Can anyone shed any light on why?


    Sure, just turn on the autoflash.
    $| = 1;
    Eugene Mikheyev, Jul 6, 2004
    #2
    1. Advertising

  3. Bigus

    Bigus Guest

    "Eugene Mikheyev" <> wrote in message
    news:ccegiv$2ek1$...
    > > that should cause an angle bracket to be printed on the commandline

    every
    > 2
    > > seconds, but instead all 5 are printed at the same time when it's

    finished
    > > the loop. Can anyone shed any light on why?

    >
    > Sure, just turn on the autoflash.
    > $| = 1;


    that's odd, because I've never used $| before and in some of my previous
    scripts where I've looped through a folder of log files I've included a
    progress-bar style thing, like:

    >>>>>>>>>>>>>>>>>>>>>
    >>>>>


    where the first row of brackets is the number of log files and on the second
    row a bracket is printed after each file has been processed.. and it's
    worked fine. I guess something else must have forced a flush of teh print
    statements in those cases.

    Anyway, thanks :)
    Bigus, Jul 6, 2004
    #3
  4. Bigus

    Paul Lalli Guest

    On Tue, 6 Jul 2004, Bigus wrote:

    > "Eugene Mikheyev" <> wrote in message
    > news:ccegiv$2ek1$...
    > > > that should cause an angle bracket to be printed on the commandline

    > every
    > > 2
    > > > seconds, but instead all 5 are printed at the same time when it's

    > finished
    > > > the loop. Can anyone shed any light on why?

    > >
    > > Sure, just turn on the autoflash.
    > > $| = 1;

    >
    > that's odd, because I've never used $| before and in some of my previous
    > scripts where I've looped through a folder of log files I've included a
    > progress-bar style thing, like:
    >
    > >>>>>>>>>>>>>>>>>>>>>
    > >>>>>

    >
    > where the first row of brackets is the number of log files and on the second
    > row a bracket is printed after each file has been processed.. and it's
    > worked fine. I guess something else must have forced a flush of teh print
    > statements in those cases.
    >
    > Anyway, thanks :)


    STDOUT defaults to line-buffering (when set to the terminal), files
    default to block-buffering. This could account for the differences you
    see. Also, printing a newline character flushes the buffer as well.

    perldoc -q flush
    and
    perldoc perlop (search for $!)

    for more info.

    Paul Lalli
    Paul Lalli, Jul 6, 2004
    #4
  5. Bigus

    J. Romano Guest

    "Eugene Mikheyev" <> wrote in message news:<ccegiv$2ek1$>...
    > > that should cause an angle bracket to be printed on the commandline every

    > 2
    > > seconds, but instead all 5 are printed at the same time when it's finished
    > > the loop. Can anyone shed any light on why?

    >
    > Sure, just turn on the autoflash.
    > $| = 1;



    If you prefer not to use the autoflush feature, but rather be able
    to flush manually, you can always require the "flush.pl" module and
    call "flush(*STDOUT)" whenever you need to, like in this example:

    #!/usr/bin/perl -w
    use strict;
    require "flush.pl";

    for (1 .. 5)
    {
    sleep 2;
    print ">";
    flush(*STDOUT);
    }
    __END__


    -- Jean-Luc
    J. Romano, Jul 7, 2004
    #5
  6. Bigus wrote:
    >
    > "Eugene Mikheyev" <> wrote in message
    > news:ccegiv$2ek1$...
    > > > that should cause an angle bracket to be printed on the commandline

    > every
    > > 2
    > > > seconds, but instead all 5 are printed at the same time when it's

    > finished
    > > > the loop. Can anyone shed any light on why?

    > >
    > > Sure, just turn on the autoflash.
    > > $| = 1;

    >
    > that's odd, because I've never used $| before and in some of my previous
    > scripts where I've looped through a folder of log files I've included a
    > progress-bar style thing, like:
    >
    > >>>>>>>>>>>>>>>>>>>>>
    > >>>>>

    >
    > where the first row of brackets is the number of log files and on the second
    > row a bracket is printed after each file has been processed.. and it's
    > worked fine. I guess something else must have forced a flush of teh print
    > statements in those cases.


    You could have been printing to STDERR which isn't buffered.


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Jul 7, 2004
    #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. Ben Finney
    Replies:
    2
    Views:
    986
    Egor Bolonev
    Jun 30, 2003
  2. Egor Bolonev
    Replies:
    0
    Views:
    474
    Egor Bolonev
    Jun 30, 2003
  3. Serge Terekhoff
    Replies:
    0
    Views:
    640
    Serge Terekhoff
    Jun 30, 2003
  4. keto
    Replies:
    0
    Views:
    923
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    344
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page