Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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 examining the Monastery: (5)
As of 2024-04-25 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found