Gary Stainburn <[EMAIL PROTECTED]> wrote:
> unlink $file if -e $file;
> 
> if (-e $file) {  
>   for($i = 0; $i < 10; $i++) { 
>     sleep(1);
>   }
>   unlink $file;
> }
> 
> The if statement controls access to the block following it.  The 
> condition is only checked once, so the answer to the first part 
> is yes - if another program deletes the file within the 10 seconds 
> your script will try to delete a non-existant file.
> 
> unlink $file if -e $file;
> 
> This will do what you want, and if another prog does delete the file 
> in during the sleep the unlink won't be called.  

But it *will* try to delete a nonexistant file if the other process
comes along between the "if -e $file" and the "unlink $file".

Since there's not any error checking (and since calling unlink() 
twice probably isn't cause for alarm) you won't even notice the
race condition.

But it's still wrong. :)

  use Errno;

  unlink $file or $!{ENOENT} 
    or warn "Couldn't unlink '$file': $!";

Here there's no race, and a sensible message if you're going to 
leave the file sitting there.

-- 
Steve

perldoc -qa.j | perl -lpe '($_)=m("(.*)")'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to