1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Progress Bar in Perl

Discussion in 'Perl' started by pradeep, Jun 6, 2009.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:

    Introduction



    We all write programs to run on the terminal to do jobs that take time, like processing logs, sending out newsletter, etc. To know the progress of our task we print some messages to know our program is running well and doing its job. But wouldn't it be nice if we could add a progress bar to keep track of the completion of the task like wget, curl, etc.

    Here's how we can do it in Perl, we'll use the perl module Term::ProgressBar to build and update the progress bar.

    The Code



    A very simple example:

    Code:
    #!/usr/bin/perl
    
    $| = 1;
    
    use Term::ProgressBar;
    use Time::HiRes qw/usleep/;
    
    my $number_of_entries = 2000;
    my $progress = Term::ProgressBar->new ({count => $number_of_entries ,name => 'Sending'});
    
    for(1..$number_of_entries)
    {
        usleep(650);
        $progress->update($_);
    }
    
    The output:

    Code:
    [root@deepzdev test]# ./prg_bar.pl
    Sending:  40% [=======================================                                   *]
    
    If you like to print some message and/or show an time estimate of the remaining time, here's another example:

    Code:
    #!/usr/bin/perl
    
    $| = 1;
    
    use Term::ProgressBar;
    use Time::HiRes qw/usleep/;
    
    my $number_of_entries = 10;
    my $progress = Term::ProgressBar->new ({count => $number_of_entries ,name => 'Sending',ETA=>'linear'});
    
    for(1..$number_of_entries)
    {
        sleep(5);
        $progress->update($_);
        $progress->message("\rSent $_ of $number_of_entries");
    }
    
    The output

    Code:
    [root@deepzdev test]# ./prg_bar.pl
    Sent 1 of 10
    Sent 2 of 10
    Sending:  20% [===================                            ]0m40s Left
    
    You can find more advanced option of the module at cpan.org
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
  3. riyan

    riyan New Member

    Joined:
    Apr 22, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    freelancer
    Location:
    indonesia
    how to install perl on pc my pc OS windows

    pleaes help me

    thamk you
     
  4. PradeepKr

    PradeepKr New Member

    Joined:
    Aug 24, 2010
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Nice.
    Suppose I have a program which runs for say 20 mins and want to show progress bar that. In this what would be $number_of_entries?
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Generally, progress bar is used to show the progress of the operations being performed not the time, but if you want number_of_entries will be 20 which is for 20 mins, you should increment the progress bar every 1 minute.
     
  6. PradeepKr

    PradeepKr New Member

    Joined:
    Aug 24, 2010
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Let me reframe my query.
    Suppose I have a program which run for some time (I dont know the exact time).
    Now I want to show the progress. Now what should be the input to your program?
     

Share This Page