views:

1330

answers:

6

I have a little Perl script (On Windows) that checks some files for me as an aid to my day-to-day business. At the moment it prints out something like...

0%
25%
50%
75%
Complete

But I can remember scripts I've used in the past that didn't print progress on a line-by-line basis, but which updated the output on the display, presumably by moving the cursor back and over-printing what was there.

Anyone know what magic is required? Portability isn't important to me, the script is quite disposable.

+1  A: 

You should be able to print a backspace character '\b' to move the cursor back so you can overwrite what you printed previously.

Jarod Elliott
A: 

I don't know if this works in Perl, but in C/C++ you can use

\b
for a backspace. Using several of those, you can move the cursor to overwrite old values.

schnaader
+4  A: 

In addition to the other answers, \r will go back to the beginning of the current line

1800 INFORMATION
+5  A: 

You could use curses and make a nice progress bar.

http://search.cpan.org/~mdxi/Curses-UI/lib/Curses/UI/Dialog/Progress.pm

EDIT: Or do something like this:

print "#####                                 [ 10%]\r";
# Do something
print "##########                            [ 20%]\r";
# Do something else
print "###############                       [ 30%]\r";
# Do some more
# ...
# ...
# ...
print "##################################### [100%]\n";
print "Done.\n";
daniels
I think on Windows you need to call `binmode(STDOUT)`, before `\r` will work the way you want.
Brad Gilbert
+4  A: 

If you ever need to do something in Perl, it's very likely that someone has done it and uploaded it to CPAN. Look at some of the modules with "progress" in their name.

brian d foy
+5  A: 

You might be interested in Smart Comments. This would be probably easier than coding Your own progress bars.

zoul
Thank you zoul, that a very smart ;-) module you are recommending here.
nav.jdwdw