in reply to script for adding timestamp to stdin

You're looping on input from your log file. I expect that you meant to loop on input from STDIN. Also, you need to update your timestamp variables inside the loop. Try this:
use strict; use warnings; use Data::Dumper; my $table; my $log = 'log_file.txt'; open my $fh, '>>', $log or die "Can't open $log : $!"; while (<STDIN>) { my $comment = $_; chomp $comment; my ($sec,$min,$hour,$mday,$mon,$year,$wday, $yday,$isdst)=localtime(time); printf "%4d-%02d-%02d %02d:%02d:%02d\t", $year+1900,$mon+1,$mday,$hour,$min,$sec; print $comment . "\n"; printf $fh "%4d-%02d-%02d %02d:%02d:%02d\t", $year+1900,$mon+1,$mday,$hour,$min,$sec; print $fh $comment . "\n"; } close $fh;

Replies are listed 'Best First'.
Re^2: script for adding timestamp to stdin
by Anonymous Monk on Jun 02, 2010 at 18:42 UTC
    thanks -- but the log file is not being updated still... why is that? We have a file handle, we're printing to it... er...

      Try

      open my $fh, ... use IO::Handle; $fh->autoflush(); # <--

      File handles are block-buffered by default, i.e. output is not flushed/written to the file until the buffer is full.

        great script