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': $!"; } }