【发布时间】:2011-06-27 21:53:44
【问题描述】:
我的程序(恰好在 Perl 中,虽然我不认为这个问题是 Perl 特有的)在程序中的某个点输出状态消息,格式为 Progress: x/yy,其中 x 和 yy 是一个数字,例如:Progress: 4/38。
我想在打印新状态消息时“覆盖”先前的输出,这样我就不会在屏幕上填满状态消息。到目前为止,我已经尝试过:
my $progressString = "Progress\t$counter / " . $total . "\n";
print $progressString;
#do lots of processing, update $counter
my $i = 0;
while ($i < length($progressString)) {
print "\b";
++$i;
}
如果我在$progressString 中包含换行符,则不会打印退格字符。但是,如果我省略换行符,则输出缓冲区永远不会被刷新,也不会打印任何内容。
有什么好的解决方案?
【问题讨论】:
标签: perl backspace output-formatting