在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
There are some short expressions in Perl that allow you to test files, which is handy if you want to check what a file allows before using it, or if it exists at all. We'll look at a few of the common tests in this section. ExistenceTo see if a file exists before using it, we can use: if (-e "filename.cgi") { #proceed with your code }
The -e part is the existence test. After that we have a space, followed by the file we want to test. As in the other sections, we could also use a variable to hold the file name: $neededfile="myfile.cgi"; if (-e $neededfile) { #proceed with your code }
Now, you can do something based on whether or not the file is there. If it is not, you can give an error page or move on to something else. Two other existence tests you can use may help if you need to know whether or not the file has anything in it: File exists, has a size of zero: -z File exists, has non-zero size: -s Readable, Writable, or ExecutableTo see if the file is allowed to be read, written to, or executed we can use these: Readable: -r Writable: -w Executable: -x So, if we want to check whether we can read a file before we try to open it, we could do this: $readfile="myfile.cgi"; if (-r $readfile) { #proceed with your code }
The same goes for the writable and executable tests. These can be a handy way to keep from trying to write to files that don't have write permissions, and various other things. Text or BinaryYou can test the file to see if it is text or if it is binary using these: Text File: -T Binary File: -B They work the same way as the others as well. Multiple TestsYou can test for two or more things at a time using the "and" (&&) or the "or" ( || ) operators. So, if you want to know if a file exists and is readable before opening it, you could do this: $readfile="myfile.cgi"; if ( (-e $readfile) && (-r $readfile) ) { #proceed with your code }
File tests are helpful in applications that make use of files often in the code. Using these can help avoid errors, or alert you to an error that needs to be fixed (a required file not able to be read, for instance). Well, have fun testing those files!
-----------Comment by Orientsun-------------- In addion, Perl provide a X parameter (Perl said -X is a special function) to us to checking file's information, the details of X as bellow:
A file test, where X is one of the letters listed below. This unary operator takes one argument, either a filename, a filehandle, or a dirhandle, and tests the associated file to see if something is true about it. If the argument is omitted, tests 1 -r File is readable by effective uid/gid. 2 -w File is writable by effective uid/gid. 3 -x File is executable by effective uid/gid. 4 -o File is owned by effective uid. 5 -R File is readable by real uid/gid. 6 -W File is writable by real uid/gid. 7 -X File is executable by real uid/gid. 8 -O File is owned by real uid. 9 -e File exists. 10 -z File has zero size (is empty). 11 -s File has nonzero size (returns size in bytes). 12 -f File is a plain file. 13 -d File is a directory. 14 -l File is a symbolic link. 15 -p File is a named pipe (FIFO), or Filehandle is a pipe. 16 -S File is a socket. 17 -b File is a block special file. 18 -c File is a character special file. 19 -t Filehandle is opened to a tty. 20 -u File has setuid bit set. 21 -g File has setgid bit set. 22 -k File has sticky bit set. 23 -T File is an ASCII text file (heuristic guess). 24 -B File is a "binary" file (opposite of -T). 25 -M Script start time minus file modification time, in days. 26 -A Same for access time. 27 -C Same for inode change time (Unix, may differ for other 28 platforms) you can get -X from http://perldoc.perl.org/functions/-X.html |
请发表评论