Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Unlinking a file when the file doesnt exist

by Gulliver (Monk)
on Mar 22, 2011 at 18:15 UTC ( [id://894871]=note: print w/replies, xml ) Need Help??


in reply to Re: Unlinking a file when the file doesnt exist
in thread Unlinking a file when the file doesnt exist

What if the file is actually a directory? I have never tried it but the documentation for unlink gives some warnings.

How about:

for my $file (qw(files.txt data.txt)) { unlink $file if -f $file; die "Unable to unlink '$file': $!" if -e $file; }

Update: If the file tests are different then it would die after not attempting to delete a directory and $! would not be set. In that case -f is false and -e is true. If I make them the same that won't happen.

for my $file (qw(files.txt data.txt)) { unlink $file if -f $file; die "Unable to unlink '$file': $!" if -f $file; }

Better yet; do an unlink or warn.

for my $file (qw(files.txt data.txt)) { if ( -f $file) { unlink $file or warn "Unable to unlink '$file': $!"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://894871]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2025-03-28 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (71 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.