lynx has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I am rather new at perl but have so far successfully written a few scripts. My last one processes a lot of data and takes a while to run, so I implemented a simple progress report in the form of "xx % done.". This is now printed for every new value in a new line. I would like the script to update just one status line with the percentage done on it, how can I do that? The only possibiliy I found so far is to clear the whole screen, but that can't be the best method... I work under Windows. (update) Found it, carriage return did not work without unbuffering STDOUT ( $| = 1 ), now it works :-).
  • Comment on Clearing / rewriting a line in the console (windows)

Replies are listed 'Best First'.
Re: Clearing / rewriting a line in the console (windows)
by BUU (Prior) on May 31, 2004 at 21:15 UTC
    print "\r";
      Sorry, forgot to mention that \r does not work, or I use it incorrectly. Example:
      foreach $n (1..10) { print "\r"; print "$n"; sleep 1; }
      When run nothing happens for 9 seconds and then 10 is printed, but nothing else.
        When run nothing happens for 9 seconds and then 10 is printed, but nothing else
        You need to turn off output buffering; in its simplest form, adding $|=1 should do the trick.

        Dave.

        "10 is printed but nothing else" should have given you the clue that "\r", was, in fact, doing *something* to the other characters. Beyond that, it depends on the console, some consoles delete the entire line and some just return the cursor to the front of the line, so you might need to pad your output with spaces if line you print currently is smaller then the line you printed before.
Re: Clearing / rewriting a line in the console (windows)
by saskaqueer (Friar) on Jun 01, 2004 at 08:59 UTC
    #!perl -w $| = 1; use strict; use Time::HiRes qw( sleep ); for ( 1 .. 100 ) { print "\r \r${_}% Complete."; sleep( 0.33 ); }
Re: Clearing / rewriting a line in the console (windows)
by blueAdept (Beadle) on Jun 04, 2004 at 15:59 UTC
    I saw a wonderful example of Win32::Console sometime in the last year that was a demo application that featured drop down menus, background colors, etc.. I *thought* it was a demo bundled up with either the module or one of the common perl distributions/versions. Alas I can't find it anywhere though..and that sucks because I thought it was a gem as far as demo apps go.

    In short though if you want a lot of power/ability Win32::Console is an option. If I find the demo app I'll post a link or info of where to find it.

      I think that this may be the demo you are thinking of?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
        Yes! Thats where I saw it.. While puting together a perl 5.8 compile/installation on a windows box a while ago. Its the test script for the Win32::Console module itself. That explains why I couldn't find it on my system. It doesn't install with the module itself - its only included in archive for testing the build.(thanks!)