in reply to Append to text file not working

The problem is elsewhere. The three variations you found on PM work.

>type foo.pl my $logfile = 'foo.log'; open(my $log_fh, ">>", $logfile) or die "Can't open logfile: $!\n"; print $log_fh scalar(localtime), "\n"; >type foo.log The system cannot find the file specified. >perl foo.pl >type foo.log Thu Feb 21 17:59:01 2008 >perl foo.pl >type foo.log Thu Feb 21 17:59:01 2008 Thu Feb 21 17:59:05 2008

Note: I used a lexical ($log_fh), but using a package variable (LOG1) works too.

Replies are listed 'Best First'.
Re^2: Append to text file not working
by ikegami (Patriarch) on Feb 21, 2008 at 23:02 UTC
    I meant to mention locking. If you have multiple users (i.e. threads, processes, pieces of code) adding to the file simultaneously, you'll need to control access to the file. See flock.
      As long as your writes are below one page of data (2k is safe), then with most filesystems your writes are atomic and no locking synchronization is needed. That looks like it is true in this case.

      If locking synchronization is needed, then flock is not necessarily the right place to start looking because (depending on the filesystem) it can be highly unreliable.

        I'm sure that you meant to specify that your writes are atomic, when writing to files that were opened for append.

        I prefer that posts in public forums err towards the pedantic side. :)

        -Colin.