You've already got lots of good comments here but there is a little more to discuss on this topic to fully flesh it out. There are couple ways to flush buffering.
$old_fh = select(OUTPUT_HANDLE);
$| = 1;
The 'select' statement above selects OUTPUT_HANDLE as the place to 'print' or 'write' their output and returns the name of the old file_handle that previously was recieving the printed output. You can then set the OUTPUT_HANDLE to be unbuffered and use the returned old handle to later to reset your old file handle as the receiver of output. If you wanted to do the same in a little more bizarre way you could write:
select((select(STDERR), $| = 1))
Alternatively you can use the IO:Handle module:
Or even again alternatively:
OORRR even again for linguistic nice-ness:
autoflush ONE_HANDLE 1; # unbuffer for clarity
autoflush ANOTHER_HANDLE 0; # buffer this for speed
The above three come with an expense as the 'use'd IO::Handle module require 1000's of lines of code to be read and compiled. It may often be better to use $| directly. But there you go, you have choice!
Additionally I should add that removing buffering does come at an expense ... speed! Computers have buffers for a reason. My rule of thumb is only to turn buffering off if its required, that is, if I'm not seeing my output when I *really* need to. And usually that's when I'm into the testing stage of a script and find I'm not seeing what I should, I then set things unbuffered.
Lastly, Perl's 'print' in an unbuffered state isn't truly unbuffered. Truly unbuffered mean every character one at a time is written. Perl's 'print' is what's called "command buffered". That is, a physical write is made after an ouput command. It means your unbuffered prints aren't as intensive on your system while still getting the results you need.
Are you posting in the right place? Check out Where do I post X? to know for sure.
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
Want more info? How to link or
or How to display code and escape characters
are good places to start.