#!/usr/bin/env perl use strict; use warnings; $| = 1; my $i=0; while ( $i < 10 ) { print "$i\r"; $i++; sleep 1; } while ( $i >= 0 ) { print " " x 80 . "\r"; print "$i\r"; $i--; sleep 1; } print "\n";
This script counts up from 0 to 10 (and back to 0) with each number replacing the previous. It does this by using the carriage return character (\r) instead of the new line character (\n), which is essentially a carriage return followed by a line-feed. The $| = 1 makes the output autoflush. One thing to keep in mind is that printing a string that is shorter than the previous will result in artifacts of the previous longer string. You can get around this by printing several spaces with something like: print " " x 80 . "\r";
note: If you want to keep the final output, be sure to print a \n when you are all done or it will get over-written by the next (unrelated) output.
In reply to Re: Controlling already printed text to console.
by frozenwithjoy
in thread Controlling already printed text to console.
by humble
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |