Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loading/processing animation in perl

Tags:

perl

Is there a way to display an animation while Perl is processing a file or something else? Maybe the sequence of | / - | \ (rotating pipe) instead of just printing dots.

Thanks for your help.

like image 748
Milde Avatar asked Dec 03 '10 11:12

Milde


2 Answers

The simple rotating pipe can be created using code like this:

#!/usr/bin/perl

use strict;
use warnings;

$|++; # turn off output buffering;

my @chars = qw(| / - \ );

my $i = 0;

print $chars[$i];

while (1) {
  sleep 1;
  print "\b", $chars[++$i % @chars];
}

For something more complex, take a look at Term::ProgressBar.

like image 80
Dave Cross Avatar answered Nov 17 '22 20:11

Dave Cross


Sure, something like this will do it:

perl -e '$|++; foreach $i (0..10) { print "\r", substr("|/-\\", ($i % 4), 1); sleep 1; }'

You can put code like this inside your processing loop to display an appropriate spinner.

like image 3
Greg Hewgill Avatar answered Nov 17 '22 22:11

Greg Hewgill