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

How can I dynamically update a value on STDOUT? I have a value that uses File::Tail to parse a log and return a valu each time, which is working. The idea is that it gives the user a realtime summary status of the application. However, when displaying the results, I don't really need to show a history of values, only the current value on STDOUT. In other words, instead of printing out the following as a string:

1 3 10 20

it should print 1, then replace that with a 3, then replace that with a 10, then with 20, as it parses the file and determines the value.

I had thought that using

use IO::Handle; *STDOUT->autoflush();
would work, but it seems to be for something else. What is the suggested way of doing this?

-- Burvil

Replies are listed 'Best First'.
Re: How to dynamically update value in STDOUT?
by Sidhekin (Priest) on Nov 05, 2007 at 01:44 UTC

    I think you are looking for a carriage return (without line feed):

    sub do_stuff { sleep 2; # for the sake of the example .. } $|=1; # turn off buffering, or you may never see what you write ... my @v = (1, 3, 10, 20); while (@v) { printf "\r%2d", shift @v; do_stuff(); }

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

      Thanks. Yes, that was it. I had tried your suggestion earlier, but forgot the '\r'. It's working now.

      -- Burvil