http://qs321.pair.com?node_id=917799

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

Hi,i have written following code to delete files that produce between running program after use,but it doesn't work.please help me

@files = ("domainlist","result","tmp"); foreach $file (@files) { unlink($file); }

Replies are listed 'Best First'.
Re: delete file
by Corion (Patriarch) on Aug 01, 2011 at 10:22 UTC

    Have you looked at what $! contains? Also, are the files still held open?

    for my $file (@files) { unlink $file or warn "Couldn't remove '$file': $!"; };

    Maybe the path to these files is not valid anymore because you changed the current directory?

    Also see File::Temp for creating and managing tempfiles.

Re: delete file
by moritz (Cardinal) on Aug 01, 2011 at 10:26 UTC
Re: delete file
by bart (Canon) on Aug 01, 2011 at 11:15 UTC
    If you are on Windows, you have to close the files before you can delete them.

    Also make sure you're in the right directory when you use relative paths.

    And there's no need for a loop, unlink accepts a list of files to delete as arguments.

Re: delete file
by dreadpiratepeter (Priest) on Aug 01, 2011 at 11:16 UTC

    When you say "doesn't work" you provide no useful information to us. If you want to get good answers, you need to say specifically what you were expecting to happen and what actually happened. Pasting any error messages would help too.

    This is one of my big pet peeves. I have an entire QA department at work that seem to think that "doesn't work" is an acceptable bug report. makes me nuts



    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."