mkdir $dir || die "Failed to create $dir: $!\n" unless -d $dir;
It's okay anyway - as long as it's able to create a directory - even if a file (other than a directory) with the same name is found.
Thanks guys
Rob Dixon wrote:
Wiggins D'Anconia wrote:
B. Fongo wrote:
I want to use mkdir(blah blah, o777), but want to first find out whether the directory "blah blah" exists. I'm not if the -e option will bw right here. Let's say:
unless (-e blah_blah) mkdir(blah_blah, 0777); # Is this okay?
In general -e checks for file existence, -d checks to see if an existing file is a directory (or complains that the file doesn't exist).
perldoc -f -e
So your above code leaves a condition, where blah_blah exists but is *not* a directory which is likely to cause you problems. But since you haven't told us what happens in this failure case it is hard for us to say, but,
if (-e blah_blah) {
unless (-d blah_blah) {
die "File exists but is not directory";
}
}
else {
# don't forget to check mkdir's failure
mkdir(blah_blah, 0777) or die "Can't make directory: $!";
}
Just a note:
There's no need to do a separate -e call to check whether a directory exists, -d won't throw an error. A simple
die "$dir is not directory" unless -d $dir;
Will check that $dir both exists and is a directory, which is probably all that is needed.
Rob
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>