I am trying to overwrite a print statement in a foreach loop to create somewhat of a progress bar.
What I am doing:
my $arraySize = @listOfIps;
local $| = 1;
my $counter = 0;
my $progressString;
print 'Progress: ';
foreach my $ip (@listOfIps) {
$counter++;
print "\b" x length($progressString) if defined $progressString;
$progressString = "\r$counter / $arraySize - Working on $ip";
print $progressString;
#does stuff here but thats irrelevant to the problem
}
The problem I am having is that when the foreach loop gets to an IP that is shorter than the previous one it has printed eg 10.0.0.1 it still displays the extra characters left over from the previous longer print statement.
The problem:
Progress: 3 / 10 - Working on 200.144.223.211
then overwriting this print statement with the next smaller ip address in the array gives:
Progress: 4 / 10 - Working on 10.0.0.1223.211
and so on... when actually it should print just :
Progress: 4 / 10 - Working on 10.0.0.1
so that it does not have any of the characters from the previous print left over.
There must be something really obvious I am overlooking here, as I can't see any reason why this would not be working.