I am analyzing a log file line by line. It may have anywhere from 2K to 1 million lines that have to be anaylzed. So it can take a while and I wanted to give a way to let the user know it wasn't locked up.

My method to anaylize the file was to read it too an array and then check each line of the array using a foreach loop.

All that stuff works fine.

But when I tried to insert a progress bar, I got an unexpected result and after some testing I am starting to come to the conclussion that Perl doesn't write to the screen like I thought it did.

What I came up with was simply find 10% of the array's size and everytime I come to that number write a character to let the user know we have incrememnted another 10% into the array.
#@log_buffer has 100 elements; $counter = $#log_buffer $buffer = 1; $counter_tens = $counter / 10; $counter_tens = sprintf ("%d",$counter_tens); #to get whole number for (1 .. $counter) { if ($buffer == $counter_tens) { print ("00"); $buffer = 0; } $buffer++ }


So this should write 00 to the screen each time I get 1/10th of the way through the array and I should end up with 20 "0" in a row. And it works.

The issue is that the for loop runs it course AND THEN displays the 20 0's lined up.

So then I tried this to test the behavior.

print ("00"); sleep 1; print ("00"); sleep 1; print ("00"); sleep 1; print ("00"); sleep 1; print ("00"); sleep 1; print ("00"); sleep 1; print ("00"); sleep 1; print ("00"); sleep 1; print ("00"); sleep 1; print ("00");


And I again got the exact same behavior. So this led me to believe that Perl does not print to the console in real time. I am guessing that it interprets what needs to be printed to the screen and then once it reaches a stopping point it then dumps to the screen. So the "sleep 1" doesn't work because it just pauses the script. Where as doing a chomp($input=<>) would display properly, but at the expense of being of no practical use.

So going by this logic, is there a way to tell Perl to go ahead and print to the screen, but then keep right on going through the script?

In reply to Real Time Progress Bar in console by westrock2000

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.