in reply to directory exists

What you have should tell you if something called /mydir is in the root fs. To see if it is a directory call -d.

my $mydir = '/mydir'; if ( -e $mydir and -d _ ) { print $mydir, ' directory exists.', $/; }

The underscore handle makes -d use the cached stat call from -e.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: directory exists
by ikegami (Patriarch) on Jun 03, 2005 at 02:50 UTC
    Isn't testing for -e and -d redundant? -d includes a -e test.

      Yup. Underneath it calls stat(2), which would return ENOENT if it doesn't exist (file or directory). -d goes on to use the returned struct stat* to check if it's a directory or not.

      --
      We're looking for people in ATL

      Yes. I'd use a test like this in real code, and I guess it got into my recommendation:

      is_dir { -e shift() ? -d _ ? 1 : 0 : undef; }
      With that I can test truth to see if there is a directory of that name, or defined to see if the name is in use.

      After Compline,
      Zaxo

        Even that's redundant. The -d function by itself DWYM, and returns undef if the stat fails, false if the file exists but is not a directory.

Re^2: directory exists
by Anonymous Monk on Jun 03, 2005 at 00:26 UTC
    Hello, thank u for the very fast reply. I tried the syntax you posted but it doesnt work for me ..for some reason. my $dev = '/dev'; if (-e $dev && -d _) { # print "Directory exists \n"; print $dev, ' directory exists.', $/; } but If I use perl -wle '$x=q(/dev); print "OK" if -d $x' , it works fine from the command line

      It works for me, do you have shell quoting problems?

      $ perl -e'my $dev = "/dev";if (-e $dev and -d _) {print $dev," directo +ry exists.",$/}' /dev directory exists. $

      After Compline,
      Zaxo

      You may be "suffering from buffering." Try switching off output buffering by adding the line $| = 1 somewhere before the print statement.

      the lowliest monk

      Hello everyone, THANK YOU FOR ALL YOUR REPLIES...THE SYNTAX YOU SUGGESTED WORKED ONCE I CHANGED A FEW LINES OF CODE IN ANOTHER PLACE..... thank you very much

      Dear anonymous monk. It would be wise if you used code tags when entering multi-line code like the one above, because this way it's not clear where the end of the comment is.

Re^2: directory exists
by Anonymous Monk on Jun 03, 2005 at 00:43 UTC
    Hello, thank u for the very fast reply. I tried the syntax you posted but it doesnt work for me ..for some reason. my $dev = '/dev'; if (-e $dev && -d _) { print $dev, ' directory exists.', $/; } but If I use perl -wle '$x=q(/dev); print "OK" if -d $x' , it works fine from the command line