in reply to Re: output on top of old output
in thread output on top of old output

This works fine for single digits, but if you increase the for loop to 15, the output gets all wonky:
perl -e '$|++;for(1..15){ print $_;sleep 1; print "\b"} # prints: 1111115
because, of course, you're only backspacing over one character. To avoid extra logic to figure out how many spaces to go back, you can just go to the beginning of the line with \r:
perl -e '$|++;for(1..15){ print $_;sleep 1; print "\r"}

Which one is "better" depends on your desired use, obviously.

Addendum: Ooog, slow typing today.

Replies are listed 'Best First'.
Re^3: output on top of old output
by cog (Parson) on Mar 22, 2005 at 15:46 UTC
    Basically, xorl, "\b" backs up one char, while "\r" rewinds the whole line :-)

    Remember, if you print a newline character, you won't be able to use these control characters to erase it.

      I think it'd be just as handy to use their real names: \b == backspace (go back one space), and \r == carriage return (return to the beginning of the line). Originally, these were from the terminal printer days and were just directives to the print head to do exactly that. Carriage returns (returning the carriage to the beginning of the line) were not intrinsically linked to new lines, so we get different characters for these activities.

Re^3: output on top of old output
by sh1tn (Priest) on Mar 22, 2005 at 16:15 UTC
    ... for(1..15){ print $_;sleep 1; print "\b"x(length$_)} ...