in reply to printing in a specific area of the console
The simple way is to not print a newline after each piece. Then, before each new piece, print as many backspaces as you need ("\b") to get you back to the start of where the new data will go. The new data will have to be at least as long as the old, so pad anything shorter with spaces. Do this with standard output unbuffered. Here's a simple sub I wrote to print ongoing totals:
use strict; use FileHandle; use vars qw(@ISA @EXPORT $VERSION); use Exporter; $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw(print_total); sub print_total($$) { my $total = shift; my $label = shift; my $n; if ($total == 1) { STDOUT->autoflush; print $label; } $n = length($total); print "$total"; while ($n-- > 0) { print "\b"; } }
The other fun one is to print sequences of | / - \ to get one of those spinning progress indicators.
|
|---|