Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6614623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:21:40+00:00 2026-05-25T20:21:40+00:00

I am writing to two files, one a log using a Log_message function and

  • 0

I am writing to two files, one a log using a Log_message function and the other within my file_write function writing to OUT and I want to write it line by line and not be buffered so that it writes line by line and not all in one go at the end of the script.

I have read about buffering and making the filehandle hot but can’t get my code to work.

In this example I have added the $|=1; just before the foreach loop but it still writes in one go. Am I doing something really dumb?

I have enclosed my entire script further down if that 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");
    #printf "resources: %s\n", scalar keys %{ $doc->{ resource } }; 

   $|=1;

    #And I have also tried:

    #use IO::Handle;
    #STDOUT->autoflush(1);

    foreach ( keys %{ $doc->{ resource } } ) {
        #print OUT $doc->{ resource }->{ $_ }->{ id }, "\n";
        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 entire script

#   !c:/Perl/bin/Perl.exe


#-----------------------------------------------
#Modules
#-----------------------------------------------
use XML::Simple;
use LWP;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1; 
use strict;
use warnings;

#-----------------------------------------------
#Declare variables
#-----------------------------------------------
    my $script = "LiveContent Auto Cache Script";   # Name of the script
    my $version = "Version 0.1";
    #my $pubId = "COMP-20110922XXXX";
    my $pubId = "LiveContentDoc";       
    my $OUT = "output.txt";
    my $LOG = "cacher-log.log";     # Location of log file
    my $DATE;                                                   # Date in form 2001-sep-01
  my $DATENR;                                               # Date in form 2001-01-09 
  my $TIME;                                             # Time in form 12:04:03
  my $txtmesg = "";
  my $resource;
  my $xs;
  my $doc;

####################################
########### Main Program ###########
####################################
error_logger();                             # Open Log file and time stamp it
request_url(); #Open the xml url and read it in
file_write();  #write the contents of the xml url to a file


#-----------------------------------------------
sub request_url {
#-----------------------------------------------
my $useragent = LWP::UserAgent->new;
my $request = HTTP::Request->new( GET => "http://digitalessence.net/resource.xml" );
#my $request = HTTP::Request->new( GET => "http://dronlineservices.letterpart.com/web/content.xql?action=index&lang=en&pub=" . $pubId );
$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");
#printf "resources: %s\n", scalar keys %{ $doc->{ resource } };



use IO::Handle;
STDOUT->autoflush(1);

foreach ( keys %{ $doc->{ resource } } ) {
    #print OUT $doc->{ resource }->{ $_ }->{ id }, "\n";
    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";


        #   my $browser = LWP::UserAgent->new;
        #   $browser->timeout(240);
        #   $browser->env_proxy;
        #   $browser->agent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');

            # my $response = $browser->get($testurl);

        #    if ($response->is_success) {
             #     print "\n############################\n";
             #     print "$testurl\n";
             #     print "\n############################\n";
              #   print $response->decoded_content;  # print the response out
            # }
            # else {
             #    my $error = $response->status_line;
            #     Log_message ("\n$DATE - $TIME - WARN - Can't load $ID because: $error");
             #    die $response->status_line;

        #   }



    #my $loadrequest = $ua->get('http://dronlineservices.letterpart.com/web/content.xql?action=doc_html&lang=en&pub=" . $pubId . "&docid=" . $ID');
    sleep 1;

}


Log_message ("\n$DATE - $TIME - INFO - Written the output file");
#close(OUT) or Log_message ("\n$DATE - $TIME - WARN - Failed to close the Output file");
Log_message ("\n$DATE - $TIME - INFO - Closed the output file");
}
#-----------------------------------------------
sub error_logger {
#-----------------------------------------------
    time_stamp();                                                                                                                               # Run Time stamp sub
    open LOG, ">>$LOG" or die ("could not open log file <$LOG>");                               # Open Log File
    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();                   # Run time_stamp every time the log is written to
    my($mesg) = @_;
    print LOG $mesg if $LOG;    # Print to log file
    print $mesg;              # Print to Screen
    $txtmesg = $mesg;
    #print "\nLOGGING: $txtmesg\n";
} 
#-----------------------------------------------
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";
} # end sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:21:41+00:00Added an answer on May 25, 2026 at 8:21 pm

    You forgot to select the handle first.

    select( (select(OUT), $| = 1)[0] );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two php files, one manages database connection and the other retrieves data
I have two C# applications, one is reading a file (File A) line by
I am writing a code in JDK 7 on Unix which compares two files.
I have two text files bala.txt and bala1.txt bala.txt contains text line by line
I have two streams, where one is based off of the other. I'm working
I am processing a number of text files line by line using BufferReader.readlLine() .
I have the option of writing two different formats for a database structure: Article
I'm new to linq and having trouble writing two simple queries. For some reason,
I'm asking this because I'm in the process of writing two such editors for
I am writing a program which has two panes (via CSplitter ), however I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.