Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 621
Nano HE Avatar asked Dec 20 '10 04:12

Nano HE


People also ask

How do I change directory in Perl?

You can use chdir function in Perl to change a directory and go to a new location. You will need to have the required permission to change a directory and go inside the new directory.

How do I write a file path in Perl?

To open a file that's in another directory, you must use a pathname. The pathname describes the path that Perl must take to find the file on your system. You specify the pathname in the manner in which your operating system expects it, as shown in the following examples: open(MYFILE, "DISK5:[USER.


1 Answers

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.

like image 104
Brad Mace Avatar answered Oct 10 '22 16:10

Brad Mace