Search code examples
perlperl-module

progress bar in command line perl script


I am trying to print progress in % in command prompt. But it is not working properly.

I want to print the progress as :: Status 10% Completed when 20% will complete it will show Status 20% Completed in that same place not in new line. Could you please help me.

Code::

$count++;
$per=($count/$total)*100;
print "\nStatus:  $per Completed.\r";
sleep 1;

Solution

  • You can do something like this:

    use strict;
    use warnings;
    
    use Time::HiRes qw(usleep);
    local $| = 1;
    
    my @nums = 1 .. 20;
    
    foreach my $c (@nums) {
      print "$c";
      usleep(100000);
      print ("\b" x length($c));
    }
    print "\n";