in reply to Skipping files in a directory

perldoc -X
foreach my $dir (@DIRS) { warn "Directory $dir not found!\n" and next unless -d $dir; # # work with existing directory here # }

Replies are listed 'Best First'.
Re^2: Skipping files in a directory
by svenXY (Deacon) on Dec 11, 2008 at 10:55 UTC
    ++ccn as it is obviously much better to check for the existence of a directory before trying to open it and warn on failure
      as it is obviously much better to check for the existence of a directory before trying to open it and warn on failure

      That might be OK here, but in the general case it is not, because of race conditions. If you check for the existence of a directory, and then open it, it might be deleted by another process between these two operations.

      Thus you have to do the error checking anyway, and don't gain anything by another call to stat (which most file test ops do).

      That's why the general philosophy with file access and IO is "try and see if it worked", not "first test if it might work, and then try".


      Thanks a lot svenXY and ccn