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

How can you make an animated loading sign in Perl?

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

我已经尝试使用

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

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

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

不工作

我只想让程序等待 1/10 秒打印下一个 .

标准 sleep function operates in integer seconds. You can use the sleep functions from Time::HiRes 作为支持小数秒的 drop-in 替换。

use strict;
use warnings;
use Time::HiRes 'sleep';

sleep 0.1;

使用 Time::HiRes 满足 sub-second 时间需求,一些方法

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 秒来模拟 "completion"(加载)。因此,这次的 if 检查代表检查 "loading" 是否完成,代码中那个点大概可以做什么(如果它是一个单独的 thread/process,或者如果这段代码在分叉进程中运行)。


可能更好"spinner"可以使用

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 借用的符号的想法。

这样的 "spinners" 当然不会像点那样给出他们等待多长时间的视觉线索。

现有解决方案假定您确实想要 sleep/wait。他们不容易适应用实际工作替换 sleep/wait。我的解决方案是当你在做实际工作时(例如加载),而不仅仅是等待某事。

use Time::HiRes qw( );

$| = 1;

{
   my @syms = qw( - \ | / );
   my ( $i, $t );

   sub start_spinner {
      $t = Time::HiRes::time;
      $i = 0;
      print $syms[$i];
   }

   sub update_spinner {
      my $now = Time::HiRes::time;
      return if $now - $time < 0.1;  # Prevent spinner from spinning too fast.

      $time = $now;
      $i = ( $i + 1 ) % @syms;
      print "\b$syms[$i]";
   }

   sub stop_spinner {
      print "\b \b";
   }
}

start_spinner();
for (1..500) {
   update_spinner();          # Call this as often as possible.
   Time::HiRes::sleep(0.01);  # Simulate a little bit of work.
}
stop_spinner();

关键是使用 Time::HiRes 的 higher-resolution time(和 sleep,如有必要),以及速率限制器 (return if $now - $time < 0.1;) .

如果你真的想打印一行点,可以使用相同的方法。

{
   my $t;

   sub start_spinner {
      $t = Time::HiRes::time;
   }

   sub update_spinner {
      my $now = Time::HiRes::time;
      return if $now - $time < 0.1;  # Prevent spinner from spinning too fast.

      $time = $now;
      print ".";
   }

   sub stop_spinner {
      print "\n";
   }
}

另一种不使用模块的方法是滥用 select():

use warnings;
use strict;
$|=1;

while (1){
    print '.';
    select(undef, undef, undef, 0.1);
}

或者,FreeBSD-style 微调器(使用 Linux 系统调用来刷新屏幕。在 Windows 上,将 clear 更改为 cls) :

while (1){
    for (qw(- \ | / -)){
        system 'clear';
        print $_;
        select(undef, undef, undef, 0.1);
    }
}