I want my program to simply output to a single line in STDOUT. In
other words i want to print a line, then 'backspace' that line and
print somethign new on the same line. I do not want the terminal
window to scroll.
Any ideas? CAn this be done?
You could do something like this:
----
#!/usr/bin/perl
use strict;
use warnings;
$| = 1; # output autoflush mode
my $last_message_length = 0;
while (my $message = <DATA>) {
chomp $message;
print "\r";
print ' ' x $last_message_length;
print "\r";
print $message; # must not contain \n or \r
sleep(1); # for example
$last_message_length = length $message;
}
__DATA__
First message
Second message
The third message is really very much longer. In fact, it is more than
80 characters
The fourth message is shorter
----
But as you will most likely see if you run this, there are problems if
the message is longer than your terminal width. You could also look into
more robust solutions using, for example, the Curses module from cpan.
DS