#!/usr/bin/perl -w use strict; use warnings; sub progress_bar { my ($total, $msg, $width, $off_sym, $on_sym) = @_; $msg ||= ' / ( %) '; $width ||= 32; $off_sym ||= "-"; $on_sym ||= "*"; my $last_msg = ""; local $| = 1; my $psub = sub { my ($count) = @_; my $b_done = ($count < 0); $b_done and $count = $total; my $pcnt = sprintf "%5.1f", 100.0 * $count / $total; my $used = $pcnt * $width / 100; my $unused = $width - $used; my $meter = ($on_sym x $used) . ($off_sym x $unused); my $this_msg = $msg; $this_msg =~ s//$pcnt/g; $this_msg =~ s//$count/g; $this_msg =~ s//$total/g; $this_msg =~ s//$meter/g; my $next_width = $width - length($msg); if ($last_msg ne $this_msg) { print STDERR "\r$this_msg\e[K"; } $b_done and print STDERR "\n"; }; return $psub; } # Main program my $psub = progress_bar(10000, ' File of ( %)'); for (my $i = 0; $i < 10000; $i++) { select(undef,undef,undef,0.01); $psub->($i); } $psub->(-1);