in reply to Don't Check for Directories
in thread Checking for Directories

Ah, but the sticky bit there is that you want to ignore a failure due to "directory already exists" and not ignore any other reasons for failure, including "file already exists". Before Errno.pm, this was tricky. Now it isn't so bad except that "file already exists" and "directory already exists" put the same value in $!.

So I think the easiest robust solution is still to only create the directory if it doesn't already exists.

Replies are listed 'Best First'.
RE: RE: Don't Check for Directories
by chip (Curate) on Jul 29, 2000 at 01:37 UTC
    Well, I still don't see the point in checking ... after all, what's the next thing you'll do? That's right, create a file in the directory ... and whatever the error is, you can report it.

    Besides, why introduce a race condition without having to?

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      You can report the failure, but not the reason (the file creation will fail for "no such directory" but we won't know why the directory creation failed). I've worked too long in "Support"; I hate programs that fail and give no hint as to why. Or are you suggesting we cache $! after the mkdir and report it if the next file creation failed??

      Good point on the race condition.

      I don't like the follow-up suggestion of using system("mkdir -p"), since you lose portability. But using use File::Path qw(mkpath) makes sense, especially since it is smart enough to remove the most common race condition.

      The improvement is to use mkdir -p. This will create the directory, including parents, but give no error if it exists already. I use this in some scripts a lot. It works. It's even mentioned in the man page.

      -- Kirby

      Tuxtops: Laptops with Linux!

        And what happens when you try this on, say, win32?