|
Disable Print Buffer In Perl
|
|
|
|
|
Contributed by Chad Brandt
|
|
|
|
Saturday, 03 July 2004
There are times you create a perl program that writes to STDOUT or a log file and you do not want the printing buffered. By default perl will buffer your output. This can have unexpected results when you are calling print within a loop, or you are expected data written to your logs instantly. Luckily perl has ways to shut off the buffering of the output. There is a special variable to turn off buffering of STDOUT and you can set autoflush on you File Handles. Here is an example program that shows how you would do each.
#!/usr/bin/perl
############################################################ # Example program to show how to set STDOUT and FILES to # NOT buffer and print immediately # ############################################################ use FileHandle;
# to make STDOUT flush immediately, simply set the variable # this can be useful if you are writing to STDOUT in a loop # many times the buffering will cause unexpected output results $| = 1;
# to prevent buffering when writing to files, set autoflush on # the file handle open (OUT, ">>test.out") || die "could not create test.out - $!"; OUT->autoflush(1);
print OUT "writing to file\n";
close(OUT)
exit(0);|
Thanks!!! Written by Guest on 2009-06-16 17:28:05 My perl script was inserting data into a MySql table but it only entered 36KB into it. As soon as I added these lines it all worked Ok. Thanks again. | Thanks a bunch mate! Written by Guest on 2010-04-20 00:47:21 I was looking to write my output to the file, as the script progresses, the above script did the trick! Cheers. | Double Thanks... Written by Guest on 2010-06-25 16:09:21 Placing this simple statement into my Perl script saved me after a week of trying to figure where my prints were in my loop... Thank you,
|
Only registered users can write comments. Please login or register. Powered by AkoComment 1.0 beta 2! |