Perl——文件操作
特殊文件句柄
perl提供了6种特殊的文件句柄:
STDIN ,
STDOUT ,
STDERR ,
DATA ,
ARGV,
ARGVOUT。
访问模式
模式 | 描述 |
< 或 r | 只读方式打开,将文件指针指向文件头。 |
> 或 w | 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
>> 或 a | 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
+< 或 r+ | 读写方式打开,将文件指针指向文件头。 |
+> 或 w+ | 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。 |
+>> 或 a+ | 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。 |
文件测试
File Test | Meaning |
-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) |
if (-r $filename and -w $filename) {
... }
- 文件测试操作符没写文件名和文件句柄,则默认文件名保存在$_里
foreach (@lots_of_filenames) {
print "$_ is readable\n" if -r;
}
select
使用select更改默认文件输出句柄