如何测试是否可以写入文件句柄?
我有一些子例程,我这样调用 myWrite($fileName, \@data)
。 myWrite()
打开文件并以某种方式写出数据。我想修改myWrite
,以便我可以像上面或那样调用它,并将文件句柄作为第一个参数。 (此修改的主要原因是将文件的打开委托给调用脚本而不是模块。如果有更好的解决方案来告诉 IO 子例程在哪里写入,我很高兴听到它。 )
为了做到这一点,我必须测试第一个输入 var 是否是文件句柄。我通过阅读 这个问题。
现在这是我的问题:我还想测试是否可以写入这个文件句柄。我不知道该怎么做。
这就是我想要做的:
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;
}
...
}
我需要知道的是我是否可以写入文件句柄,尽管很高兴看到一些通用解决方案告诉您文件句柄是否是用“>>”打开的或“<”,或者未打开等。
(请注意 这个问题相关,但似乎没有回答我的问题。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
检测句柄的打开情况
正如 Axeman 指出的那样,
$handle->opened()
会告诉您它是否打开。如您所见
,您不能使用 Scalar::Util::openhandle() ,因为它太愚蠢且有缺陷。
打开句柄压力测试
如果您没有使用
IO::Handle->opened
,正确的方法在以下简单的三语言小脚本中进行了演示:运行时会产生以下结果 : :
这就是测试打开文件句柄的方法!
但我相信这甚至不是你的问题。
尽管如此,我还是觉得这个问题需要解决,因为这个问题有太多不正确的解决方案。人们需要睁开眼睛看看这些东西实际上是如何运作的。请注意,如果需要,
Symbol
中的两个函数会使用调用者
的包——这当然经常发生。确定开放句柄的读/写模式
这是您问题的答案:
运行时,会产生以下输出:
现在快乐,Schwern? ☺
Detecting Openness of Handles
As Axeman points out,
$handle->opened()
tells you whether it is open.produces
As you see, you cannot use
Scalar::Util::openhandle()
, because it is just too stupid and buggy.Open Handle Stress Test
The correct approach, if you were not using
IO::Handle->opened
, is demonstrated in the following simple little trilingual script:Which when run produces:
That is how you test for open file handles!
But that wasn’t even your question, I believe.
Still, I felt it needed addressing, as there are too many incorrect solutions to that problem floating around here. People need to open their eyes to how these things actually work. Note that the two functions from
Symbol
use thecaller
’s package if necessary—which it certainly often is.Determining Read/Write Mode of Open Handle
This is the answer to your question:
Which, when run, produces this output:
Happy now, Schwern? ☺
仍在尝试这一点,但也许您可以尝试将零字节系统写入文件句柄并检查错误:
fcntl
看起来也很有希望。你的里程可能会有所不同,但类似这样的事情可能是在正确的轨道上:Still experimenting with this, but maybe you can try a zero-byte syswrite to a filehandle and check for errors:
fcntl
looks promising too. Your mileage may vary, but something like this could be on the right track:如果您使用
IO
(您应该这样做),那么 < code>$handle->opened 会告诉您句柄是否打开。可能需要更深入地研究才能说出其模式。If you're using
IO
(and you should), then$handle->opened
will tell you whether a handle is opened. Might have to delve deeper to tell its mode.听起来您正在尝试重新发明异常处理。不要那样做。除了只写句柄之外,还有很多潜在的错误。被交给一个封闭的手柄怎么样?存在错误的句柄?
mobrule 的方法使用
use Fcntl;
正确确定文件句柄上的标志,但这通常不处理错误和警告。如果要将打开文件的责任委托给调用者,请将适当的异常处理委托给调用者。这允许调用者选择适当的响应。绝大多数时候,要么是死掉,要么是警告或修复那些给你带来不好处理的违规代码。
有两种方法可以处理传递给您的文件句柄上的异常。
首先,如果您可以查看 TryCatch 或 在 CPAN 上尝试::Tiny 并使用该方法异常处理。我使用 TryCatch,它非常棒。
第二种方法是使用 eval 并在 eval 完成后捕获适当的错误或警告。
如果您尝试写入只读文件句柄,则会生成警告。捕获尝试写入时生成的警告,然后可以向调用者返回成功或失败。
这是一个示例:
输出:
It sounds like you are trying to reinvent exception handling. Don't do that. There are lots of potential errors besides being handed a write-only handle. How about being handed a closed handle? A handle with an existing error?
mobrule's method with
use Fcntl;
correctly determines the flags on a filehandle, but this does not generally handle errors and warnings.If you want to delegate to the caller the responsibility of opening the file, delegate to the caller the appropriate handling of exceptions. This allows the caller to choose the appropriate response. The vast majority of times, it will be either to die or warn or fix the offending code that handed you a bad handle.
There are two way to handle exceptions on a file handle passed to you.
First, if you can look at TryCatch or Try::Tiny on CPAN and use that method of exception handling. I use TryCatch and it is great.
A second method is use eval and catch the appropriate error or warning after the eval is finished.
If you attempt to write to a read-only file handle, it is a warning that is generated. Catch the warning that is generated from your attempted write and you can then return success or failure to the caller.
Here is an example:
The output:
-w 运算符可用于测试文件或文件句柄是否可写
输出:
The -w operator can be used to test whether a file or a filehandle is writeable
Output: