我需要知道如何在Perl中制作动画加载标志,以在程序检查更新时使人们保持娱乐。

我已经尝试使用

print ".";
sleep(0.1);
print ".";

但这似乎不起作用。
有人请帮助我!

print ".";
sleep(0.1);
print ".";

没用

我只希望程序等待1/10秒才能打印下一个.

最佳答案

使用Time::HiRes满足亚秒级计时需求的几种方法

use warnings;
use strict;
use feature 'say';
use Time::HiRes qw(sleep);

STDOUT->autoflush(1);  # or $| = 1;
my $tot_sleep = 0;

# Print dots
print "Loading ";
while (1) {
    $tot_sleep += sleep 0.1;  print ".";
    last if $tot_sleep >= 2;
}
say " done\n";  $tot_sleep = 0;

# Print "spinning" cursor while waiting
print "Loading ... ";
WAIT: while (1) {
    for (qw(- \ | /)) {
        print;  $tot_sleep += sleep (0.1);  print "\b";
        last WAIT if $tot_sleep >= 2;
    }
}
say "\b done\n";

# Print (overwrite) percentile completed (if you know the job size)
my $tot_percent = 0;
while ($tot_percent < 100) {
    $tot_percent += 5;
    print "Loading ... $tot_percent%\r";
    sleep 0.1;
}
say "\n";

我通过等待2秒来模拟“完成”(负载)。因此,这次的if检查代表检查“加载”是否已完成,或者在代码中的那个时候大概可以完成的检查(如果它是一个单独的线程/进程,或者该代码是否在派生的进程中运行)。

对于一个更好的“旋转者”可以使用
use Time::HiRes qw(sleep);
use utf8;
use open qw(:std :encoding(UTF-8));
STDOUT->autoflush(1);

print "Waiting ... ";
WAIT: {
    my $tot_sleep;
    while (1) {
        for ('◑', '◒', '◐', '◓') {
            print; $tot_sleep += sleep 0.1; print "\b";
            last WAIT if $tot_sleep >= 5;
        }
    }
};
say "\b done";

Term::Spinner::Color借用的符号的想法。

这样的“旋转者”当然不会像点一样给出他们等待多长时间的视觉线索。

关于perl - 如何在Perl中制作动画加载标志?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57011212/

10-12 22:16
查看更多