Search Perl pages
Professions, Sciences, Humanities, Business, ...
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...
Conversions, tests, processing, manipulation,...
Integer, Floating point, Matrix, Statistics, Boolean, ...
Algorithms, Memory, Process control, Debugging, ...
Data storage, Integrity, Encryption, Compression, ...
Networks, protocols, Interprocess, Remote, Client Server, ...
Timing, Calendar and Clock, Audio, Video, Printer, Controls...
Management, Filtering, File & Directory access, Viewers, ...
Next: How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?
I/O library (stdio) normally buffers characters sent to devices. This is done for efficiency reasons, so that there isn't a system call for each byte. Any time you use |
write() in Perl, you go though this buffering.
syswrite() circumvents stdio and buffering.
In most stdio implementations, the type of buffering and the size of the
buffer varies according to the type of device. Disk files are block
buffered, often with a buffer size of more than 2k. Pipes and sockets are
often buffered with a buffer size between 1/2 and 2k. Serial devices (e.g.
modems, terminals) are normally line-buffered, and stdio sends the entire
line when it gets the newline.
Perl does not support truly unbuffered output (except insofar as you can
syswrite(OUT, $char, 1)). What it does instead support is ``command buffering'', in which a
physical write is performed after every output command. This isn't as hard
on your system as unbuffering, but does get the output where you want it
when you want it.
If you expect characters to get to your device when you print them there,
you'll want to autoflush its handle, as in the older:
open(DEV, "<+/dev/tty"); # ceci n'est pas une pipe
or the newer
open(DEV, ">/dev/printer"); # but is this?
or even this:
use IO::Socket; # this one is kinda a pipe?
$sock = IO::Socket::INET->new(PeerAddr => 'www.perl.com',
PeerPort => 'http(80)',
Proto => 'tcp');
die "$!" unless $sock;
$document = join('', $sock->getlines());
print "DOC IS: $document\n";
Note the hardcoded carriage return and newline in their octal equivalents. This is the
ONLY way (currently) to assure a proper flush on all platforms, including Macintosh.
You can use
select() and the
$| variable to control autoflushing (see $| and select):
$oldh = select(DEV);
$| = 1;
You'll also see code that does this without a temporary variable, as in
select((select(DEV), $| = 1));
Source: Perl FAQ: Files and Formats
Copyright: Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
Previous: How do I verify a credit card checksum?
(Corrections, notes, and links courtesy of RocketAware.com)
Up to: File Access
Up to: Stdio Stream file operations
Search | About | Comments | Submit Path: RocketAware > Perl >
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments