Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody, I'm processing a big text file, and want to ouput the progress in the form a percentage. I'm currently doing the following, inside the file reading loop:
my $percentage = sprintf("%.1f",$lineCounter/$linesInFile*100); print "\b\b\b\b\b\b\b\b\b\b".$percentage."%";
It works, but it looks a bit silly with the cursor jumping all over the place. I was just wandering if there isn't a more elegant way of doing this. Or maybe even a textual progress bar like:
|************ |
showing the progress. Thanks for any help.

Replies are listed 'Best First'.
Re: Command line output of Perl script
by gellyfish (Monsignor) on Sep 09, 2005 at 10:15 UTC
Re: Command line output of Perl script
by adrianh (Chancellor) on Sep 09, 2005 at 10:33 UTC

    CPAN is your friend. Take a look at some of the ProgressBar modules.

Re: Command line output of Perl script
by marto (Cardinal) on Sep 09, 2005 at 10:09 UTC
Re: Command line output of Perl script
by blazar (Canon) on Sep 09, 2005 at 10:13 UTC
    You may want "\r". E.g.:
    $ perl -e '$|++; $\="\r"; > print, select undef, undef, undef, .1 > for "01" .."99"'
Re: Command line output of Perl script
by GrandFather (Saint) on Sep 09, 2005 at 10:11 UTC

    You could print your end ticks on one line, then spit the progress blobs out one at a time so the progress bar grows from the first tick to the last:

    | | ************

    Perl is Huffman encoded by design.
Solution: Command line output of Perl script
by Anonymous Monk on Sep 09, 2005 at 13:13 UTC
    Thanks for all the responses. I found them all useful. I never realised how \r works. I always thought it goes to the next line like \n. Turns out that's all I needed. However, I checked out some of the progressbars on CPAN as well as well the Doom style progress bar. Very useful.

    Thanks again for the help.

      In DOS, Windows and printers, CR ("\r") means move the cursor to the begining of the line. LF means move the cursor to the same column on the next line. (Remember that "\n" gets translated to LF when in binmode, and to CRLF when not.)

      In unix, CR ("\r") means move the cursor to the begining of the line. LF ("\n") means move the cursor to the begining of the next line.