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?
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With