I have a Perl script running on an old CentOS 5.6 server which has Perl 5.8.8 installed. Unfortunately I can't upgrade either the OS or the version of Perl that's running on this server.
When I'm running this script from the command prompt, despite there being a $| = 1;
statement at the top of the script (in the global scope), it still seems to buffer output to the console (over a ssh session).
Writes to both a log file and STDOUT
are carried out by a function, for example:
#!/usr/bin/perl
$| = 1;
&writelog("Started...");
# Do work with lots of writelog'ing
&writelog("...Done.");
exit(0);
sub writelog {
# This is greatly simplified for the purpose of this question
my ($logentry) = @_;
my $logfile = "/var/log/thelog.log";
my $logline = "$logentry\n";
print $logline;
open (LOGFILE, ">>$logfile");
print LOGFILE, "$logline";
close (LOGFILE);
}
Does the value of $|
only affect output in the current scope, i.e. in this case the script's global scope? Or should it, in the example above, also cause immediate flushing to STDOUT
/LOGFILE
by the print
statements in writelog
?