perl - 如何在Perl中制作动画加载标志?

标签 perl loading

我需要知道如何在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/

相关文章:

debugging - C++\CLI 应用程序在加载时崩溃

javascript - 从 After Effects 导出 Logo 动画以用作加载

Perl 替换一个字符串

linux - 我如何丢弃或忽略 "syn"数据包以在使用 listen() 函数的 linux 上的 perl 套接字编程中打开连接?

perl - 使用带有 Moose 类型约束的祝福 CodeRefs

python - Python 模块的动态加载

silverlight - 如何在Silverlight中实现 "Please Wait"屏幕

php - 网站安全问题

regex - 单个正则表达式捕获组是否可以捕获没有某些中间字符的短语?

android - 加载 Google map 时的 ProgressBar