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. Advertisements

  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. Advertisements

  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. Advertisements

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. Roderik
    Replies:
    9
    Views:
    36,975
    pasquinelli
    Nov 6, 2007
  2. Ben Finney
    Replies:
    2
    Views:
    1,198
    Egor Bolonev
    Jun 30, 2003
  3. Miktor
    Replies:
    7
    Views:
    12,655
    Joe Bacigalupa
    Aug 9, 2005
  4. keto
    Replies:
    0
    Views:
    1,359
  5. Replies:
    5
    Views:
    3,729
    James Kanze
    Mar 4, 2008
  6. Qian Xu
    Replies:
    5
    Views:
    1,515
    Qian Xu
    Dec 23, 2008
  7. David Cournapeau

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

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    634
    David Cournapeau
    Dec 30, 2008
  8. Kris Rudin

    console app w/o seeing console?

    Kris Rudin, Nov 11, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    277
    Randy Charles Morin
    Nov 11, 2003
Loading...