看看fcntl选项.也许F_GETFL
有O_ACCMODE
.
编辑:我做了一些谷歌搜索和午餐玩,这里有一些非可移植的代码,但它适用于我的Linux盒子,可能是任何Posix系统(甚至可能是Cygwin,谁知道?).
use strict; use Fcntl; use IO::File; my $file; my %modes = ( 0 => 'Read only', 1 => 'Write only', 2 => 'Read / Write' ); sub open_type { my $fh = shift; my $mode = fcntl($fh, F_GETFL, 0); print "File is: " . $modes{$mode & 3} . "\n"; } print "out\n"; $file = new IO::File(); $file->open('> /tmp/out'); open_type($file); print "\n"; print "in\n"; $file = new IO::File(); $file->open('< /etc/passwd'); open_type($file); print "\n"; print "both\n"; $file = new IO::File(); $file->open('+< /tmp/out'); open_type($file);
示例输出:
$ perl test.pl out File is: Write only in File is: Read only both File is: Read / Write