yav*_*voh 10 perl backspace output-formatting
我的程序(碰巧在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;
}
Run Code Online (Sandbox Code Playgroud)
如果我在其中包含换行符,则不会打印退格符$progressString.但是,如果我省略换行符,则永远不会刷新输出缓冲区,也不会打印任何内容.
对此有什么好的解决方案?
Mic*_*eyn 11
在STDOUT中使用autoflush:
local $| = 1; # Or use IO::Handle; STDOUT->autoflush;
print 'Progress: ';
my $progressString;
while ...
{
# remove prev progress
print "\b" x length($progressString) if defined $progressString;
# do lots of processing, update $counter
$progressString = "$counter / $total"; # No more newline
print $progressString; # Will print, because auto-flush is on
# end of processing
}
print "\n"; # Don't forget the trailing newline
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7876 次 |
| 最近记录: |