"B. Fongo" <[EMAIL PROTECTED]> wrote:
> I tried several alternatives to solve the problem, but failed. In most
> cases, the script couldn't create a directory if there is a text file
> with the same name. Rob's suggestion works better; except that it
> overrides the directory without warning - if it already existing. But it
> remians some how streng, because the content of the directory remains
> intact.
>
> 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
The code snippet should be using 'or' instead of '|| to work properly.
Consider if I put this snippet in a file called 'x'. When I try to create a
directory also named 'x', it should fail, but does not tell me so:
$ cat x
#!/bin/perl -w
$dir = $ARGV[0];
mkdir $dir || die "Failed to create $dir: $!\n" unless -d $dir;
$ x x
$ echo $?
0 # success???
Now by changing '||' to 'or', we get what you'd expect:
$ x x
Failed to create x: File exists
$ echo $?
17
# non-zero = failed, my OS is HPUX 11, dont know why I didnt get 255
(unsigned -1)
Also consider what perl sees:
Using '||':
$ perl -MO=Deparse,-p x
($dir = $ARGV[0]);
(-d($dir) or mkdir(($dir || die("Failed to create $dir: $!\n"))));
x syntax OK
Using 'or':
$ perl -MO=Deparse,-p x
($dir = $ARGV[0]);
(-d($dir) or (mkdir($dir) or die("Failed to create $dir: $!\n")));
x syntax OK
Hope that helps.
-Jeff
> 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>
>
>
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>