in reply to Re: Subroutine to delete one file and copy one file to another
in thread Subroutine to delete one file and copy one file to another

if (-f $delTemp){ unlink $delTemp or die "can't delete >$delTemp< file: $!"; }

This opens a door for a race condition (https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use): Someone can change the file $delTemp between the stat call inside -f and the unlink call.

The clean way to handle this: Just call unlink. Yes, unlink will fail when $delTemp does not exist. Check $! before calling die. POSIX specifies that unlink will return ENOENT if and only if the file was not found.

Something like the following should do the trick, using %! (available since Perl 5.005):

unlink $delTemp or $!{ENOENT} or die "can't delete '$delTemp': $!";

See also Re^2: Subroutine to delete one file and copy one file to another.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)