Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

delayed print statement?

by Anonymous Monk
on Jul 09, 2001 at 11:20 UTC ( [id://94919]=perlquestion: print w/replies, xml ) Need Help??

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

Hello everyone,

I'm experiencing long delays in my print statements and was wondering why.
print "Connecting to database... "; #this print works # Connect to the SQL database my $dbh = DBI->connect($dsn,'username','password',{RaiseError=>0, Prin +tError=>0}); if ($dbh) { print "Connected.\n"; #gets printed }else{ print "Cannot connect to database. (",$dsn,")\nQuitting\n"; exit; } print "Processing File... "; # delayed till near end. my $sth = $dbh->prepare( qq{ SELECT field1 FROM Table WHERE field2 = ?}); open(FILE, 'data'); # gonna process a BIG file while(<FILE>) { $sth->execute($_); # etc etc... } close FILE; print "Done processsing file\n";
Any print statements at "Processing File" and afterwards are not done until near the end of the program.
I've tried print STDOUT "stuff"; and select (STDOUT); before those print statements but it didn't work.

Any help would be appreciated. Thanks.
-Mel

Replies are listed 'Best First'.
Re: delayed print statement?
by epoptai (Curate) on Jul 09, 2001 at 11:24 UTC

      And, as has been pointed out many a time, $| affects the currently selected file handle. The turn off buffering on something else, you have to temporarily select, e.g., like so:

      select((select(SOMETHING_ELSE), $| = 1)[0]);

      --bwana147

      You're right epoptai, I just read Suffering from Buffering? this weekend because I was having the same sort of problem and it helped a great deal to understand the pros/cons of buffering and how to get around it, if needed.

Re: delayed print statement?
by snowcrash (Friar) on Jul 09, 2001 at 11:36 UTC
    I suspect your problem is that perl buffers output, i.e. the output aint printed immediatly but later for some efficiency reason. Output to STDOUT typically is line buffered, that means it should be written when you send a newline (\n)...
    setting the $OUTPUT_AUTOFLUSH variable ($|) to some value other than 0 will help.

    for further help on that topic try to read:
  • perldoc -q buffer
  • man perlvar and look for $|

    hope to help
    snowcrash //////
Re: delayed print statement?
by Anonymous Monk on Jul 09, 2001 at 20:12 UTC
    print "Connecting to database... "; #this print works

    Others have already answered your question, but I'll point out that the above print probably does not work the way you expected. The only reason it appears to work is because the Connected or Cannot connect line is printed almost immediately afterwards.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found