给你一个IO::File对象或一个typeglob(\*STDOUT或Symbol::symbol_to_ref("main::FH")); 您将如何确定它是读取还是写入句柄?无法扩展接口以传递此信息(我将覆盖在实际关闭之前和之前close添加调用).flushsync
目前我正在尝试flush和sync文件句柄并忽略错误"Invalid argument"(这是我尝试flush或sync读取文件句柄时得到的):
eval { $fh->flush; 1 } or do {
#this seems to exclude flushes on read handles
unless ($! =~ /Invalid argument/) {
croak "could not flush $fh: $!";
}
};
eval { $fh->sync; 1 } or do {
#this seems to exclude syncs on read handles
unless ($! =~ /Invalid argument/) {
croak "could not sync $fh: $!";
}
};
Run Code Online (Sandbox Code Playgroud)
看看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);
Run Code Online (Sandbox Code Playgroud)
示例输出:
$ perl test.pl
out
File is: Write only
in
File is: Read only
both
File is: Read / Write
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1667 次 |
| 最近记录: |