Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Screen Output Buffering

by mvaline (Friar)
on Aug 19, 2004 at 20:52 UTC ( [id://384450]=perlquestion: print w/replies, xml ) Need Help??

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

This is a little question, but it bothers me that I can't figure out what's going on. Here is the code in question:
print "\nStage 1 of 3: Processing employee data... "; retrieve_employee_data(); print "finished.\nStage 2 of 3: Processing punch data... "; retrieve_punch_data(); print "finished.\nStage 3 of 3: Creating output... "; output_db(); print "finished.\n\nProgram execution complete.\n";
When I run this, the program prints the first line: "Stage 1 of 3...", then prints "finished" and newlines, then it pauses flashing (presumably while it runs retrieve_punch_data()) and doesn't print "Stage 2 of 3..." until after it finishes. What's going on?

Replies are listed 'Best First'.
Re: Screen Output Buffering
by mortis (Pilgrim) on Aug 19, 2004 at 20:59 UTC
    I'm surprised this hasn't been answered already. STDOUT in perl is buffered by default. To make sure STDOUT is autoflushed, set $| to a true value early in your program. STDOUT is not buffered by default.

    If another file handle has been selected, you may need to select STDOUT before setting $|.

    See perldoc perlfun and perldoc perlvar fore more information on flushing and $|.

    hth,

    Kyle

      mortis,
      To make sure STDOUT is autoflushed, set $| to a true value early in your program

      I am sorry about nitpicking but setting $| to a true value will turn on auto-flushing for the currently selected file handle. I know it is being pedantic, but just because STDOUT is the default selected file handle doesn't mean it has to be:

      #!/usr/bin/perl use strict; use warnings; open (FILE, '>', 'foo.txt') or die "Unable to open foo.txt for writing + : $!"; select FILE; $| = 1; # FILE is now unbuffered print "hello, world\n"; print STDOUT "goodbye cruel world\n"; # still buffered

      Cheers - L~R

      Thanks! That worked. I guess I didn't grep perlfun and perldoc accurately enough. I appreciate the help.
Re: Screen Output Buffering
by ikegami (Patriarch) on Aug 19, 2004 at 21:00 UTC
    you're seeing the effects of buffering. Try "$|=1;" to disable the buffering, or "$temp=$|; $|=1; $|=$temp;" to flush it. If STDOUT is sent to a terminal/console, printing a "\n" will flush the buffer too.
Re: Screen Output Buffering
by ccn (Vicar) on Aug 19, 2004 at 21:01 UTC

    Did you turn on STDOUT autoflushing with $|=1 as described in perlvar?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://384450]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found