I am writing two files, one of which uses the Log_message function, and the other in my file_write function writing to OUT, and I want to write it line by line and not buffer it so it is written line by line, and not all at a time at the end of the script .
I read about buffering and made a hot file descriptor, but couldn't get my code to work.
In this example, I added $|=1;just before the loop foreach, but it still writes at a time. Am I doing something really dumb?
I have included my entire script further if this helps.
sub file_write {
open OUT, ">>$OUT" or Log_message ("\n$DATE - $TIME - ERROR - Could not create filelist.doc \t");
Log_message ("\n$DATE - $TIME - INFO - Opened the output file");
my $total = scalar keys %{ $doc->{ resource } };
Log_message ("\n$DATE - $TIME - INFO - Found: " . $total . " resources");
$|=1;
foreach ( keys %{ $doc->{ resource } } ) {
my $ID = $doc->{ resource }->{ $_ }->{ id }, "\n";
Log_message ("\n$DATE - $TIME - INFO - Found: " . $ID);
my $testurl = "http://dronlineservices.letterpart.com/web/content.xql?action=doc_html&lang=en&pub=" . $pubId . "&docid=" . $ID;
print OUT "$testurl\n";
sleep 1;
}
And the whole script
use XML::Simple;
use LWP;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
use strict;
use warnings;
my $script = "LiveContent Auto Cache Script";
my $version = "Version 0.1";
my $pubId = "LiveContentDoc";
my $OUT = "output.txt";
my $LOG = "cacher-log.log";
my $DATE;
my $DATENR;
my $TIME;
my $txtmesg = "";
my $resource;
my $xs;
my $doc;
error_logger();
request_url();
file_write();
sub request_url {
my $useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new( GET => "http://digitalessence.net/resource.xml" );
$resource = $useragent->request( $request );
$xs = XML::Simple->new();
$doc = $xs->XMLin( $resource->content );
}
sub file_write {
open OUT, ">>$OUT" or Log_message ("\n$DATE - $TIME - ERROR - Could not create filelist.doc \t");
Log_message ("\n$DATE - $TIME - INFO - Opened the output file");
my $total = scalar keys %{ $doc->{ resource } };
Log_message ("\n$DATE - $TIME - INFO - Found: " . $total . " resources");
use IO::Handle;
STDOUT->autoflush(1);
foreach ( keys %{ $doc->{ resource } } ) {
my $ID = $doc->{ resource }->{ $_ }->{ id }, "\n";
Log_message ("\n$DATE - $TIME - INFO - Found: " . $ID);
my $testurl = "http://dronlineservices.letterpart.com/web/content.xql?action=doc_html&lang=en&pub=" . $pubId . "&docid=" . $ID;
print OUT "$testurl\n";
sleep 1;
}
Log_message ("\n$DATE - $TIME - INFO - Written the output file");
Log_message ("\n$DATE - $TIME - INFO - Closed the output file");
}
sub error_logger {
time_stamp();
open LOG, ">>$LOG" or die ("could not open log file <$LOG>");
Log_message ("\n$DATE - $TIME - -----------------------------------------\ \t");
Log_message ("\n$DATE - $TIME - INFO - Start of Application\ \t");
Log_message ("\n$DATE - $TIME - INFO - $script\ \t");
Log_message ("\n$DATE - $TIME - INFO - $version\ \t");
Log_message ("\n$DATE - $TIME - -----------------------------------------\ \t");
}
sub Log_message {
time_stamp();
my($mesg) = @_;
print LOG $mesg if $LOG;
print $mesg;
$txtmesg = $mesg;
}
sub time_stamp {
my($Sec,$Min,$Hour,$Day,$MonthNr,$Year) = localtime(time());
my $Month=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")[$MonthNr];
$Sec = sprintf("%02d",$Sec);
$Min = sprintf("%02d",$Min);
$Day = sprintf("%02d",$Day);
$MonthNr = sprintf("%02d",++$MonthNr);
$Year = 1900 + $Year;
$DATE = "$Year-$Month-$Day";
$DATENR = "$Year-$MonthNr-$Day";
$TIME = "$Hour:$Min:$Sec";
}