我如何测试是否可以写入文件句柄? [英] How can I test if I can write to a filehandle?

查看:159
本文介绍了我如何测试是否可以写入文件句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些子程序,像 myWrite($ fileName,\ @ data) myWrite()以某种方式打开文件并写出数据。我想修改 myWrite ,这样我就可以像上面的那样用文件句柄作为第一个参数来调用它。 (这个修改的主要原因是把文件的打开委托给调用脚本而不是模块,如果有更好的方法来告诉IO子程序在哪里写,我很乐意听到。 )

为了做到这一点,我必须测试第一个输入变量是否是一个文件句柄。我想通过阅读这个问题

现在我的问题是:我也想测试是否可以写入这个文件句柄。我不知道如何做到这一点。

这是我想要做的:

  sub myWrite {
my($ writeTo,$ data)= @_;
my $ fh;
if(isFilehandle($ writeTo)){#我可以做到这一点
死你是一个不道德的人\
除非(canWriteTo($ writeTo)); #但是我该怎么做呢?
$ fh = $ writeTo;
} else {
打开$ fh,>,$ writeTo;
}
...
}

我需要知道是否我可以写入文件句柄,虽然看到一些通用的解决方案会告诉你是否用>>或<打开了文件句柄,或者如果没有打开等等。



(注意是相关的,但似乎并没有回答我的问题。)

  

打开A,'<','/ some / file';
打开B,'>','/ some / other-file';

{
本地$! = 0;
my $ n = syswrite A,;
#结果:$ n是undef,$!是错误的文件描述符
}
{
local $! = 0;
my $ n = syswrite B,;
#结果:$ n是0,$!是

fcntl 也很有希望。你的里程可能会有所不同,但类似的情况可能是正确的:

pre $ 使用Fcntl;
$ flags = fcntl HANDLE,F_GETFL,0; ($ flags& O_ACCMODE)&(O_WRONLY | O_RDWR)){
printHANDLE是可写的... \\\

} $ bGET FLags
$ b


I have some subroutines that I call like this myWrite($fileName, \@data). myWrite() opens the file and writes out the data in some way. I want to modify myWrite so that I can call it as above or with a filehandle as the first argument. (The main reason for this modification is to delegate the opening of the file to the calling script rather than the module. If there is a better solution for how to tell an IO subroutine where to write, i'd be glad to hear it.)

In order to do this, I must test whether the first input var is a filehandle. I figured out how to do that by reading this question.

Now here's my question: I also want to test whether I can write to this filehandle. I can't figure out how to do that.

Here's what I want to do:

sub myWrite {
  my ($writeTo, $data) = @_;
  my $fh;
  if (isFilehandle($writeTo)) { # i can do this
    die "you're an immoral person\n" 
      unless (canWriteTo($writeTo)); # but how do I do this?
    $fh = $writeTo;
  } else {
    open $fh, ">", $writeTo;
  }
  ...
}

All I need to know is if I can write to the filehandle, though it would be nice to see some general solution that tells you whether you're filehandle was opened with ">>" or "<", or if it isn't open, etc.

(Note that this question is related but doesn't seem to answer my question.)

解决方案

Still experimenting with this, but maybe you can try a zero-byte syswrite to a filehandle and check for errors:

open A, '<', '/some/file';
open B, '>', '/some/other-file';

{
    local $! = 0;
    my $n = syswrite A, "";
    # result: $n is undef, $! is "Bad file descriptor"
}
{
    local $! = 0;
    my $n = syswrite B, "";
    # result: $n is 0, $! is ""
}

fcntl looks promising too. Your mileage may vary, but something like this could be on the right track:

use Fcntl;
$flags = fcntl HANDLE, F_GETFL, 0;  # "GET FLags"
if (  ($flags & O_ACCMODE) & (O_WRONLY|O_RDWR) ) {
    print "HANDLE is writeable ...\n"
}

这篇关于我如何测试是否可以写入文件句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆