in reply to is there a file/directory?

I don't suggest you use
unless (-e $file) { open FILE, "> $file"; # ... }
because that creates a race condition. Instead, use the sysopen() function.
use POSIX qw( O_EXCL O_CREAT ); $perm = 0644; # or whatever sysopen FILE, $file, O_EXCL | O_CREAT, $perm or die "can't create $fil +e: $!";


japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: is there a file/directory?
by japhy (Canon) on May 29, 2001 at 23:14 UTC
    I've been asked to specify what I mean. The problem is that the file might spring into existence between the time you test for existence, and when you create it. That's because there's time in between the -e $file and the open().

    To get around that, you can use sysopen(), which is atomic, which means that the check to see if the file is there, and the opening of the file, are done instantaneously.

    Re: Re: is there a file/directory? With mkdir(), you don't have that problem:

    if (!mkdir($dir, $perm) and $! =~ /^File exists/) { warn "($dir already exists)"; } elsif ($! =~ /^Permission denied/) { warn "(can't create $dir)"; } # ...


    japhy -- Perl and Regex Hacker