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

后端 未结 1 1762
名媛妹妹
名媛妹妹 2021-02-06 20:26

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 ex

1条回答
  •  生来不讨喜
    2021-02-06 21:16

    Use -d (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.

    0 讨论(0)
提交回复
热议问题