title: Can't turn off buffering in Perl using $|=1;
body: <p>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.</p>
  
  <p>I have read about buffering and making the filehandle hot but can't get my code to work.</p>
  
  <p>In this example I have added the <code>$|=1;</code> just before the <code>foreach</code> loop but it still writes in one go. Am I doing something really dumb?</p>
  
  <p>I have enclosed my entire script further down if that helps.</p>
  
  <pre><code>    #-----------------------------------------------
      sub file_write {
      #-----------------------------------------------
      open OUT, "&gt;&gt;$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-&gt;{ resource } };
      Log_message ("\n$DATE - $TIME - INFO - Found: " . $total . " resources");
      #printf "resources: %s\n", scalar keys %{ $doc-&gt;{ resource } }; 
  
     $|=1;
  
      #And I have also tried:
  
      #use IO::Handle;
      #STDOUT-&gt;autoflush(1);
  
      foreach ( keys %{ $doc-&gt;{ resource } } ) {
          #print OUT $doc-&gt;{ resource }-&gt;{ $_ }-&gt;{ id }, "\n";
          my $ID = $doc-&gt;{ resource }-&gt;{ $_ }-&gt;{ id }, "\n";
          Log_message ("\n$DATE - $TIME - INFO - Found: " . $ID);
          my $testurl = "http://dronlineservices.letterpart.com/web/content.xql?action=doc_html&amp;lang=en&amp;pub=" . $pubId . "&amp;docid=" . $ID;
           print OUT "$testurl\n";
        sleep 1;
  
      }
  </code></pre>
  
  <p>And the entire script</p>
  
  <pre><code>#   !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-&gt;new;
  my $request = HTTP::Request-&gt;new( GET =&gt; "http://digitalessence.net/resource.xml" );
  #my $request = HTTP::Request-&gt;new( GET =&gt; "http://dronlineservices.letterpart.com/web/content.xql?action=index&amp;lang=en&amp;pub=" . $pubId );
  $resource = $useragent-&gt;request( $request );  
  $xs         = XML::Simple-&gt;new();
  $doc        = $xs-&gt;XMLin( $resource-&gt;content );
  
  }
  
  
  #-----------------------------------------------
  sub file_write {
  #-----------------------------------------------
  open OUT, "&gt;&gt;$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-&gt;{ resource } };
  Log_message ("\n$DATE - $TIME - INFO - Found: " . $total . " resources");
  #printf "resources: %s\n", scalar keys %{ $doc-&gt;{ resource } };
  
  
  
  use IO::Handle;
  STDOUT-&gt;autoflush(1);
  
  foreach ( keys %{ $doc-&gt;{ resource } } ) {
      #print OUT $doc-&gt;{ resource }-&gt;{ $_ }-&gt;{ id }, "\n";
      my $ID = $doc-&gt;{ resource }-&gt;{ $_ }-&gt;{ id }, "\n";
      Log_message ("\n$DATE - $TIME - INFO - Found: " . $ID);
  
  
      my $testurl = "http://dronlineservices.letterpart.com/web/content.xql?action=doc_html&amp;lang=en&amp;pub=" . $pubId . "&amp;docid=" . $ID;
       print OUT "$testurl\n";
  
  
          #   my $browser = LWP::UserAgent-&gt;new;
          #   $browser-&gt;timeout(240);
          #   $browser-&gt;env_proxy;
          #   $browser-&gt;agent('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');
  
              # my $response = $browser-&gt;get($testurl);
  
          #    if ($response-&gt;is_success) {
               #     print "\n############################\n";
               #     print "$testurl\n";
               #     print "\n############################\n";
                #   print $response-&gt;decoded_content;  # print the response out
              # }
              # else {
               #    my $error = $response-&gt;status_line;
              #     Log_message ("\n$DATE - $TIME - WARN - Can't load $ID because: $error");
               #    die $response-&gt;status_line;
  
          #   }
  
  
  
      #my $loadrequest = $ua-&gt;get('http://dronlineservices.letterpart.com/web/content.xql?action=doc_html&amp;lang=en&amp;pub=" . $pubId . "&amp;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, "&gt;&gt;$LOG" or die ("could not open log file &lt;$LOG&gt;");                               # 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
  </code></pre>