此perl代码用于在运行代码时加载
use Term::ProgressBar::IO;
open my $fh, '<', 'passwords.txt' or die "could not open file n.txt: $!";
my $pb = Term::ProgressBar::IO->new($fh);
my $line_count;
while (<$fh>) {
$line_count += 1;
$pb->update();
}
close $fh;
print "total lines $line_count"
C:\Users\USER\Desktop>r.pl
0% [* ]total lines 360问题出在哪里?
发布于 2017-08-23 06:19:27
Term::ProgressBar::IO似乎与常规的文件句柄不兼容,它需要一个在test script中看到的IO::File实例,因此您需要使用
my $fh = IO::File->new('passwords.txt', 'r') or die ...;docs说这个模块可以与任何可查找的文件句柄一起工作,但是它仍然不能工作(对我来说,无论如何)。
relevant line during construction为:
if (ref($count) and $count->can("seek")) {当$count是IO::File类型时,此条件通过,但当$count是常规GLOB时,它将失败,即使是为读写而打开的。GLOB将支持seek方法,但是can("seek")直到在其上调用了一个方法之后才会返回true。
use feature 'say';
open my $fh, '<', 'some-file';
say $fh->can('seek'); # ""
say tell $fh; # 0
say $fh->can('seek'); # ""
say eval { $fh->tell }; # 0
say $fh->can('seek'); # 1这就提出了另一种解决方法(可以在Term::ProgressBar::IO内部实现以解决此问题),即在将文件句柄传递给Term::ProgressBar::IO之前在文件句柄上进行文件句柄方法调用
open my $fh, '<', 'passwords.txt' or die "could not open file n.txt: $!";
eval { $fh->tell }; # endow $fh with methods detectable by UNIVERSAL::can
...发布于 2017-08-29 14:00:10
这是一个简单的流程条形码,输出如下。

$n = 10;
for($i=1;$i<=$n;$i++){
proc_bar($i,$n);
select(undef, undef, undef, 0.2);
}
sub proc_bar{
local $| = 1;
my $i = $_[0] || return 0;
my $n = $_[1] || return 0;
print "\r [ ".("\032" x int(($i/$n)*50)).(" " x (50 - int(($i/$n)*50)))." ] ";
printf("%2.1f %%",$i/$n*100);
local $| = 0;
}https://stackoverflow.com/questions/45827030
复制相似问题