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

I am sure there has got to be an easy solution to this, but I cannot find anything...

I have some perl scripts that run for a very long time. I would like to continuously print some status without filling up the screen. Basically, print some status, clear it when there is new status to print, and print the new status.

Any help would be tremendously appreciated!

Replies are listed 'Best First'.
Re: Overwriting printed text
by Abigail-II (Bishop) on Feb 06, 2004 at 20:35 UTC
    #!/usr/bin/perl use strict; use warnings; no warnings qw /syntax/; $| = 1; my @a = qw {| / - \ }; my $a; while (1) { print "\r", $a [$a ++ % @a]; select undef, undef, undef, 0.25; } __END__

    Abigail

      Thanks Abigail!

      The \r was what I was looking for. I knew it had to be easy!

Re: Overwriting printed text
by hv (Prior) on Feb 06, 2004 at 20:31 UTC

    Not sure about other systems, but many Unix terminals support a backspace character (chr(8)) to step back one, so you can space out a message by repeatedly printing backspace, space, backspace something like this:

    my $clear = "\x08 \x08"; while (not $finished) { my $string = status(); print $string; # without a newline! do_more_stuff(); print $clear x length($string); }

    Hugo

Re: Overwriting printed text
by halley (Prior) on Feb 06, 2004 at 20:51 UTC
    abigail's posting is more canonically used for "replace the whole line" applications. That is, the character "\r" will work as a carriage-return without proceeding to the next line, on most terminal-like devices and emulators today.

    See peek - output a one-line preview of stdin for my example using this technique.

    --
    [ e d @ h a l l e y . c c ]

Re: Overwriting printed text
by cees (Curate) on Feb 06, 2004 at 22:13 UTC

    It looks like you have some good answers above already, but you might also want to take a look at Term::ProgressBar. Even if it doesn't do exactly what you want, it may give you some hints on implementation.

Re: Overwriting printed text
by Vautrin (Hermit) on Feb 06, 2004 at 20:55 UTC

    Check out this excerpt from O'Reilly's Spidering Hacks.

    HTH,

    Vautrin,


    Want to support the EFF and FSF by buying cool t-shirts? Click here