- Research Overview
- MPhil Information
- Wireless Comms
- Bat System
- Broadband Phone
- Sentient Vehicles
- Task Assignment
- Computing for the Future of the Planet
- Active Floor
- Open-Source CSK Energy
DTG Local Pages
- Cambridge Weather
- Contact Details
Autoflush in Perl
Author: Jonathan Davies
Set the $| variable to 1 to turn on autoflush for the current default filehandle.
Ordinarily, the default filehandle is STDOUT, but the autoflush setting will not be used when you open a new filehandle (say, to output to a file). To autoflush outputs to a file, the following procedure must be adopted:
open (FH, ">file"); my $oldfh = select (FH); $| = 1; select ($oldfh);
Selecting FH makes FH the default filehandle, and we then turn on autoflush for it before restoring the previous default filehandle.
Alternatively, the autoflush() method of an open IO::Handle object can be used.
Addendum: If you just want to activate autoflush (disable buffering) for a file handle without modifying the default filehandle:
open (FH, ">filename"); FH->autoflush(1);
Note that this requires the IO::Handle module to be loaded.