Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl STDOUT question

Status
Not open for further replies.

plunkett

Technical User
Joined
Jun 10, 2004
Messages
57
Location
US
I have a perl script that does some printing on a unix machine and it takes several minutes to finish. What i want to do is create a "status" bar that gives the progress of the script. My problem is that I can print something like...

0%[ ]100%

which then goes to...

0%[==== ]100%

once some time has gone by, but I don't know how to print the second status bar _over_ the first (i.e. without a newline). I tried printing backspaces like "\b" to stdout, and that didn't work. So basicly I want this to happen...

# perl test.pl /tmp/files
Printing 987 files: 0%[ ]100%
Completed!
#

... where the space in the status bar fills up with "=" without moving down a new line each time. Anyone know how to pull this off?
 
Curses, foiled again ;-)

If that doesn't work out have a look at some of the terminal modules available on
Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
a module would be great, but there is no gaurentee that each box this thing runs on has that particular module installed. Is there a safer way to do this to gaurentee portability (even if it's harder to code)?
 
Safer, no, the modules are tested, but you could hack the module apart, and include it in your code (depending on the terms of the license), you should still include a reference to it in your licensing agreement.

Or specify Curses.pm as a prerequisite for your application, which would be my preferred option



Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I used to do this kind of thing before curses :-) (I'm old)

You probably don't need Curses, easier to do it by "turning off buffering" (it doesn't really do this but it's convenient to think of it that way) printing the whole line out each time with a CR at the end to put the cursor back at the beginning of the line, like this.

$|=1; # turn off buffering
print "Printing 987 files: 0%[ ]100%\r";
sleep 1;
print "Printing 987 files: 0%[===== ]100%\r";
sleep 1;
print "Printing 987 files: 0%[========== ]100%\r";
sleep 1;
print "Printing 987 files: 0%[====================]100%\r";




Mike

The options are: fast, cheap and right - pick any two. [orientalbow] & [anakin]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top