in reply to Re: Flocking and In-place editing
in thread Flocking and In-place editing

Thanks to Mark for the quick reply. Would you then flock the file like this?
use Fcntl ':flock'; @ARGV = "file.tab"; $^I = ".bak"; flock(ARGV,LOCK_EX); seek(ARGV, 0, 2); while (<>) { s/foo/bar/; print; } flock(ARGV,LOCK_UN);
Does anybody know if this is right, or even the way to lock whilst using inplace editing? Regards, Phil

Replies are listed 'Best First'.
Re: Re: Re: Flocking and In-place editing
by Anonymous Monk on Jan 27, 2004 at 18:00 UTC
    Always error check your syscalls. It's easy to do. If you had written this:
    flock(ARGV,LOCK_EX) or die "flock failed: $!";
    You would have seen an error message like this:
    flock failed: Bad file descriptor at foo line 6.
    The problem is that the ARGV filehandle isn't open util you hit the while (<>) { line, so you can't lock it. After that, it's too late to lock. Follow one of the other suggestions, and either lock a separate lockfile, or write your own in-place edit code. The built-in $^I is nice for a quick script, but it's not always the best way to go.