Re: forcing immediate output of a subroutine



On Feb 28, 11:09 am, mla...@xxxxxxxxx wrote:
I wanted to do something fancy for the user when they run my script
from the command line. Basically, I wrote this subroutine so that it
would output what would appear to be a spinning cursor while the
script was running.

##start of Fancy subroutine
sub Fancy
{
$FlashIndex = shift;
++$FlashIndex;

if ($FlashIndex == 1) { $StatusLine = " \\"; }
elsif ($FlashIndex == 2) { $StatusLine = " \|"; }
elsif ($FlashIndex == 3) { $StatusLine = " \/"; }
else { $StatusLine = " \-"; $FlashIndex = 0;}

$StatusLen = length($StatusLine);
print "$StatusLine";
print "\b" x $StatusLen;}

##end of Fancy subroutine

$FlashIndex and $StatusLine are scoped globally, and each time the
function is called, $FlashIndex is passed to the function. The output
should be what appears to be a spinning cursor made up of \,|,/, and
-.

The problem is that the print statement executes at the end of the
script, not during, so I don't get a spinning cursor until the end of
the program. Is this because I am executing it in a subroutine? Is
there something about subroutines that prevents output from being
generated until program termination?

No, it has nothing to do with subroutines. Output to the console is
buffered by default (ie, it doesn't actually print until you give a
newline).

perldoc -q buffer

Basically, just set $| = 1

Paul Lalli

.