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
Shorewall Router on Linux
SnortShorwall - Using Snort And Shorewall Together
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 701 Members   Members (701) # 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.9
MySQL: 5.0.91-community
Time: 14:37
Members: 701
Hits: 1324396
News: 277
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.

Write Comment
Name:Guest
Title:
BBCode:Web AddressEmail AddressBold TextItalic TextUnderlined TextQuoteCodeOpen ListList ItemClose List
Comment:



Powered by AkoComment 1.0 beta 2!




 
Google Ads



 

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