Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

delete file

by persianswallow (Novice)
on Aug 01, 2011 at 10:16 UTC ( #917799=perlquestion: print w/replies, xml ) Need Help??

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

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://917799]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-07-06 19:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found