Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I'm writing to a file and every now and then I need to clear it out. Many processes can write to the file, but only one will have write access at any given time (it's locked exclusive for the length of the write)

At certain times, I want to copy the file and then clear the lines from the original. Because it may have serveral active file handles, I dont want to delete it - just empty it

Any ideas on how to do this will be appreciated

Replies are listed 'Best First'.
Re: Clearing out the file
by jwkrahn (Abbot) on Jul 25, 2006 at 06:39 UTC
    You probably want to use the truncate function.
Re: Clearing out the file
by davido (Cardinal) on Jul 25, 2006 at 07:44 UTC

    open the file for simple output (not for append, just plain old simple output). If the file open is successful, the file will be truncated, as per the POD: open.

    { open my $wipe_file, '>', $filename or die $!; close $wipe_file or die $!; #done }

    It would be wise to lock the file beforehand so that you know nobody else is using it at the moment. ...doesn't seem prudent to obliterate the contents of a file that's currently being read by some other process, now does it?

    At any rate, opening a file for output, by default, truncates the file to zero length.


    Dave

      Thanks, As metioned above, the file is locked exclusive
Re: Clearing out the file
by McDarren (Abbot) on Jul 25, 2006 at 08:02 UTC
    The Camel (3rd Edition) has a very excellent discussion on file locking in Chapter 16 - Interprocess Communication. One particular paragraph from that discussion appears to directly answer your question.

    I quote:

    "To get an exclusive lock, typically used for writing, you have to be more careful. You cannot use a regular open for this; if you use an open mode of <, it will fail on files that don't exist yet, and if you use >, it will clobber any files that do. Instead, use sysopen on the file so it can be locked before getting overwritten. Once you've safely opened the file for writing but haven't yet touched it, successfully acquire the exclusive lock and only then truncate the file. Now you may overwrite it with the new data."
    use Fcntl qw(:DEFAULT :flock); sysopen(FH, "filename", O_WRONLY | O_CREAT) or die "can't open filename: $!"; flock(FH, LOCK_EX) or die "can't lock filename: $!"; truncate(FH, 0) or die "can't truncate filename: $!"; # now write to FH

    Cheers,
    Darren

Re: Clearing out the file
by madtoperl (Hermit) on Jul 25, 2006 at 07:35 UTC
    Hi Ananymous Monk,

    If you want to make the file size to zero and needs the file,you can give like this,
    open FILE,">>filename"; truncate FILE, 0; close FILE
    Hope this maybe useful for you.

    Thanks and Regards,
    madtoperl.
      open FILE,">>filename"; truncate FILE, 0; close FILE

      truncate works also on filenames:

      $ echo abcde > file $ perl -e 'truncate "file", 2' $ cat file ab

      --
      David Serrano

Re: Clearing out the file
by Anonymous Monk on Jul 25, 2006 at 07:52 UTC
    Thanks for your answers. truncate works for me.