[Melbourne-pm] An intermittent problem with open for append

Paul Fenwick pjf at perltraining.com.au
Tue May 27 23:14:44 PDT 2008

G'day Tim,

Tim Hogard wrote:

> open OUT,">>/home/foo/que/$ip" || push @error, "Cant save details";
> print OUT "$ip:t:date=",scalar localtime,"\n";
> ... then it prints to OUT all the rest of ${ENV} and CGI vars.

Well, if there's a problem opening the file, then I expect you have 
something in @error that may tell you what's wrong, but I'll assume that if 
it was that simple youd' know about it.  So...

> Can anyone explain why perl 5.005 works yet 5.8.8 doesn't?
> I was under the impresson that the ">>" means tell the OS to
> open in append mode, any data written should go in the file
> and not just end up lost.

It absolutely does mean it should append.  My guess is that you may be 
seeing a buffering issue; if something later causes your program to exit 
unexpectedly, it may not have finished writing to the file.

I'd throw a:

	use IO::Handle;

at the top of your code, and a:

	OUT->flush or die "Can't flush OUT: $!";

when you've finished writing a record to your file.  ->flush will force the 
data to be written, and will return false (and should set $!) if there's any 

> open a file and append their PID and hunt for errors or maybe even trace
> that the append flag is in fact on (is there an easy way to get that info?)
> or maybe its a singal problem where its getting an odd signal.

If you're using strace, you should be able to see the file open with 
O_APPEND as one of the options.  If you have an existing filehandle, you can 
test for O_APPEND using fcntl:

	use Fcntl;

	my $flags = fcntl(MYFILE, F_GETFL, 0);
	print( ($flags & O_APPEND) ? "append" : "not append");



Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681

More information about the Melbourne-pm mailing list