in reply to Does print() write immediately to a file?
Instead of using '-s', you can find the length of the file also by seeking to the end, then using tell. No unbuffering needed. Here's an example program:
#!/usr/bin/perl use strict; use warnings; use Fcntl qw /:seek/; my $file = "/tmp/flup"; open my $fh => "> /tmp/flup" or die; print $fh "Some line of text\n"; my $l0 = -s $file; # General concept, assuming the filepointer can be anywhere. # If you know the file pointer is at the end, a simple tell() # will do, and no seeking at all is necessary. my $old = tell $fh or die "tell: $!"; seek $fh, 0, SEEK_END or die "seek: $!"; my $l1 = tell $fh; die "Nothing in file!\n" unless $l1; seek $fh, $old, SEEK_SET or die "seek: $!"; print "-s $file: $l0. seek: $l1\n"; __END__ -s /tmp/flup: 0. seek: 18
Abigail
|
|---|