You may want to consider changing the following. It's shorter, and it's probably how I would have written the loop. I realize life is not one big Perl-golf challenge, but in this case I prefer the shorter version.
while ($time > 0) {
print " " . ' ' x (3 - length($time)) . "$time $line";
foreach (1..$backcount){print "\b"}
$time -= 1;
sleep 1;
}
to
while ($time--) {
printf(" %3s %s", $time, $line);
print "\b" for (1..$backcount);
# or this could even be written as:
# print "\b" x $backcount;
sleep 1;
}
No need to do the formatting of the time yourself,
sprintf or
printf can do it for you. The
%3s part means format the string
$time, right-justified within a column of 3 characters (space-padded if necessary). You could also do
%3d as part of your printf format to do the same thing with zero-padding instead. See the perldoc page for
printf for more fun options.
Having said that, I haven't actually executed your code, but I wonder if printing out backspaces isn't the most efficient way to erase the line. I don't have any other suggestions though, so I may be off base. Anyway, good job in saving/replacing $| within your sub. Returning globals to their previous values can save a lot of headaches.
blokhead