Google
 
 
Home arrow Perl Articles arrow Disable Print Buffer In Perl

Main Menu
 Home
 Linux Articles
 FreeBSD Articles
 Apache Articles
 Perl Articles
 Other Articles
 Program Downloads
 Free Books
 News
 The Web Links
 Contact Us

Most Read
Automating SFTP using expect
FreeBSD PPTP VPN
SnortShorwall - Using Snort And Shorewall Together
Shorewall Router on Linux
Shorewall Stand Alone Firewall

Polls
Favorite Linux/BSD
Fedora
Mandrake
Debian
Slackware
Gentoo
Suse
FreeBSD
Other
  

Syndicate
Latest news direct to your desktop
RSS

Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one

Members Online
 Linux-BSD-Central Has a Total of 14090 Members   Members (14090) # Online
 We have 9 Guests Online. Guests 9
 We have 0 Users Online. Users 0

Online Users
No Users Online

Statistics
OS: Linux w
PHP: 5.2.17
MySQL: 5.1.56
Time: 11:00
Members: 14090
Hits: 1790105
News: 281
WebLinks: 15





Disable Print Buffer In Perl   PDF  Print  E-mail 
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);

Comments
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!:sigh  
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!




 


 

Check out TwistByte - The best mobile apps available For awesome Android and IPhone applications!!