-r File or directory is readable by this (effective) user or group
-w File or directory is writable by this (effective) user or group
-x File or directory is executable by this (effective) user or group
-o File or directory is owned by this (effective) user
-R File or directory is readable by this real user or group
-W File or directory is writable by this real user or group
-X File or directory is executable by this real user or group
-O File or directory is owned by this real user
-e File or directory name exists
-z File exists and has zero size (always false for directories)
-s File or directory exists and has nonzero size (the value is the size in bytes)
-f Entry is a plain file
-d Entry is a directory
-l Entry is a symbolic link
-S Entry is a socket
-p Entry is a named pipe (a “fifo”)
-b Entry is a block-special file (like a mountable disk)
-c Entry is a character-special file (like an I/O device)
-u File or directory is setuid
-g File or directory is setgid
-k File or directory has the sticky bit set
-t The filehandle is a TTY (as reported by the isatty() system function; filenames can’t be tested by this test)
-T File looks like a “text” file
-B File looks like a “binary” file
-M Modification age (measured in days)
-A Access age (measured in days)
-C Inode-modification age (measured in days)
Perl has a special shortcut to help us not do so much work. The virtual filehandle _
(just the underscore) uses the information from the last file lookup that a file test op-
erator performed. Perl only has to look up the file information once now:
if( -r $file and -w _ ) {
... }
Stacked File Test Operators
Previous to Perl 5.10, if we wanted to test several file attributes at the same time, we
had to test them individually, even if using the _ filehandle to save ourselves some work.
Suppose we wanted to test whether a file was readable and writable at the same time.
We’d have to test whether it’s readable and then also test whether it’s writable:
if( -r $file and -w _ ) {
print "The file is both readable and writable!\n";
}
It’s much easier to do this all at once. Perl 5.10 lets us “stack” our file test operators by
lining them all up before the filename:
use 5.010;
if( -w -r $file ) {
print "The file is both readable and writable!\n";
}
阅读(420) | 评论(0) | 转发(0) |