Perl file checking
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.Existence
To 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 Executable
To 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 Binary
You 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 Tests
You 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:
[*]-X FILEHANDLE
[*]-X EXPR
[*]-X DIRHANDLE
[*]-X
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 $_ , except for -t , which tests STDIN. Unless otherwise documented, it returns 1for true and '' for false, or the undefined value if the file doesn't exist. Despite the funny names, precedence is the same as any other named unary operator. The operator may be any of:
1 -rFile is readable by effective uid/gid.
2 -wFile is writable by effective uid/gid.
3 -xFile is executable by effective uid/gid.
4 -oFile is owned by effective uid.
5 -RFile is readable by real uid/gid.
6 -WFile is writable by real uid/gid.
7 -XFile is executable by real uid/gid.
8 -OFile is owned by real uid.
9 -eFile exists.
10 -zFile has zero size (is empty).
11 -sFile has nonzero size (returns size in bytes).
12 -fFile is a plain file.
13 -dFile is a directory.
14 -lFile is a symbolic link.
15 -pFile is a named pipe (FIFO), or Filehandle is a pipe.
16 -SFile is a socket.
17 -bFile is a block special file.
18 -cFile is a character special file.
19 -tFilehandle is opened to a tty.
20 -uFile has setuid bit set.
21 -gFile has setgid bit set.
22 -kFile has sticky bit set.
23 -TFile is an ASCII text file (heuristic guess).
24 -BFile is a "binary" file (opposite of -T).
25 -MScript start time minus file modification time, in days.
26 -ASame for access time.
27 -CSame for inode change time (Unix, may differ for other
28 platforms)
you can get -X from http://perldoc.perl.org/functions/-X.html
页:
[1]