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

烂漫一生 提交于 2020-12-29 01:47: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 exists or not?


回答1:


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.



来源:https://stackoverflow.com/questions/4486951/how-to-see-if-a-directory-exists-or-not-in-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!