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

I am having a problem where I have to check if a text file exists, if it does the file must to be deleted cause the cgi program will create a new file and will append new data to it. Here is an exemple of the check and delete but it's not working. And the program keeps appending more data to it.
my $new_file="new_test.txt"; if ($new_file){unlink $new_file};

Any help?!!!

Replies are listed 'Best First'.
Re: Text File
by MarkM (Curate) on Dec 31, 2002 at 20:43 UTC

    Since unlink() should work whether or not the file exists (it does nothing in the latter case), the other suggestions about adding '-e' do nothing to locate the problem, or solve it.

    All system calls should be checked. When unlink() fails (as you believe to be occurring since the file remains) the system will return an error. Display the error using:

    if (defined $new_file) { unlink($new_file) or die "unlink of $new_file failed: $!\n"; }

    If no error message is occurring, it means that $new_file is not defined or that the unlink() is working. If the error message says something about "permission denied" then it means you do not have permission to unlink() the file from the directory. If the error message says something about "file does not exist" then it means you are not specifying the filename properly, likely because it is in a different directory.

    We cannot offer any further help without the value of $! after unlink() fails.

Re: Text File
by Jenda (Abbot) on Dec 31, 2002 at 18:15 UTC

    It should be:

    if (-e $new_file){unlink $new_file};

    But most probably you just want to open the file for writing and overwrite it if it existed. You should most probably also do some flock()ing to ensure two instances of the script do not overwrite each others data.

    Jenda

Re: Text File
by Wonko the sane (Curate) on Dec 31, 2002 at 18:15 UTC
    You need to use the -e file test operator to see if a file exists or not.
    There are also a slew of other file operators for all sorts of tests.
    A list of these can be found in the Camel book.
    if( -e $new_file ) { unlink( $new_file ); }

    Wonko.
Re: Text File
by jdporter (Paladin) on Dec 31, 2002 at 18:38 UTC
    In addition to what the others said, I'll point out that unlink fails silently if the file does not exist, so all you really need is     unlink $new_file;

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: Text File
by chromatic (Archbishop) on Dec 31, 2002 at 19:16 UTC

    Why open in append mode then? Open the file for normal writing and let the OS create it if it doesn't exist or overwrite it if it does.

Re: Text File
by Grateful_Dude (Acolyte) on Dec 31, 2002 at 19:40 UTC
    why not
    unlink $new_file if -e $new_file;
    ???
Re: Text File
by vek (Prior) on Dec 31, 2002 at 22:17 UTC
    In addition to heeding the good advice offered in this thread you might also want to make sure that new_test.txt actually resides where you think it does. The cgi program you are running will probably be running in the cgi-bin directory and therefore will check that directory for the file.

    If new_test.txt resides in another directory then you'll need to add the path to your $new_file variable:
    my $new_file = "/path-to/new_test.txt";
    -- vek --