print
or 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). 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:
use FileHandle; open(DEV, "<+/dev/tty"); # ceci n'est pas une pipe DEV->autoflush(1);
or the newer IO::* modules:
use IO::Handle; open(DEV, ">/dev/printer"); # but is this? DEV->autoflush(1);
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;
$sock->autoflush(); $sock->print("GET /\015\012"); $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; select($oldh);
You'll also see code that does this without a temporary variable, as in
select((select(DEV), $| = 1)[0]);