File Tests 

File Tests
  • Finally, Perl provides many methods for determining information about files on the file system using "File tests". For the purposes of this tutorial, there are too many types of file tests to cover them all in depth. Further, they are covered extensively elsewhere. However, the most frequent syntax of file tests used in CGI applications follows the form: 
  • if ([filetest] [filename] && [other filetest] [filename])
           {
           do something
           }
  • Consider the following example that checks to see if a file exists (-e) and is writable (-w) by us, and if so deletes it: 
  • if ((-e "temp.file") && (-w "temp.file"))
           {
           unlink ("temp.file");
           }
  • The following lists several common file tests. 
Test Description
-r File or directory is readable
-w File or directory is writable
-x File or directory is executable
-o File or directory is owned by user
-e File or directory exists
-z File exists and has zero size
-s File or directory exists and has non-zero size
-f Entry is a plain file
-d Entry is a directory
-T File is text
-B File is binary
-M Modification age in days
-A Access age in days

Additional Resources:

Deleting, Renaming and Changing the Permissions of Files
Table of Contents
Getting Information About a File With stat


Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory