Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Everyone, I am new to perl. How do I make sure if a directory exists in the root. I am trying this piece of code which doesnt seem to work $mydir = "/mydir"; if(-e $mydir){ print "directory exists"; }

Replies are listed 'Best First'.
Re: directory exists
by Zaxo (Archbishop) on Jun 03, 2005 at 00:05 UTC

    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

      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

      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

        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.

        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
      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
Re: directory exists
by tlm (Prior) on Jun 03, 2005 at 00:06 UTC

    I'm surprised it doesn't work (assuming, of course that /mydir indeed exists). Is this a Unix system?

    Incidentally, I would use the -d test in this case, which implies -e, and returns true only if the tested file not only exists, but is actually a directory. For example (OS: Linux):

    % perl -wle '$x=q(/boot); print "OK" if -d $x' OK

    the lowliest monk

      This is a unix system
Re: directory exists
by jweed (Chaplain) on Jun 03, 2005 at 00:08 UTC
    What is your expected behavior? It seems to work for me, though I would make the condition:
    ... if (-e $mydir && -d _){ ...
    to explicitly test that the file is a directory.



    Code is (almost) always untested.
    http://www.justicepoetic.net/