How to see if a directory exists or not in Perl?

PerlFile IoDirectory

Perl Problem Overview


To see if a file exists before using it, we can use:

if (-e "filename.cgi")
{
 #proceed with your code
} 

But how to indentify a directory exists or not?

Perl Solutions


Solution 1 - Perl

Use -d (http://perldoc.perl.org/functions/-X.html">full list of file tests)

if (-d "cgi-bin") {
    # directory called cgi-bin exists
}
elsif (-e "cgi-bin") {
    # cgi-bin exists but is not a directory
}
else {
    # nothing called cgi-bin exists
}

As a note, -e doesn't distinguish between files and directories. To check if something exists and is a plain file, use -f.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNano HEView Question on Stackoverflow
Solution 1 - PerlBrad MaceView Answer on Stackoverflow