为什么我的Perl有福的文件句柄不能用`can('print')``返回true? [英] Why doesn't my Perl blessed filehandle doesn't return true with `can('print')`'?
问题描述
出于某种原因,我无法使用Expect.pm的 log_file
方法获得文件句柄。我最初得到了如何将文件句柄传递给Perl Expect的log_file函数?,其中建议我使用一个IO :: Handle文件句柄传递给方法。这似乎是一个不同的问题,所以我想我会开始一个新的问题。
For some reason, I can't get filehandles working with Expect.pm's log_file
method. I originally got help on How can I pass a filehandle to Perl Expect's log_file function?, where it was suggested that I use an IO::Handle filehandle to pass to the method. This seems to be a different issue, so I thought I'd start a new question.
这是Expect.pm的违规部分:
This is the offending section of Expect.pm:
if (ref($file) ne 'CODE') {
croak "Given logfile doesn't have a 'print' method"
if not $fh->can("print");
$fh->autoflush(1); # so logfile is up to date
}
那么,我试过这个示例代码:
So, then, I tried this sample code:
use IO::Handle;
open $fh, ">>", "file.out" or die "Can't open file";
$fh->print("Hello, world");
if ($fh->can("print"))
{
print "Yes\n";
}
else
{
print "No\n";
}
当我运行这个时,我得到两个(在我看来)冲突的项目。一个单行文件,说'你好,世界'和'不'的输出。在我看来, $ fh-> can
行应该返回true。我错了吗?
When I run this, I get two (to my mind) conflicting items. A file with a single line that says 'Hello, world', and output of 'No'. To my mind, the $fh->can
line should return true. Am I wrong here?
推荐答案
奇怪的是,您需要创建一个真正的 IO :: File
对象获取可以
方法工作。尝试
Odd, it looks like you need to create a real IO::File
object to get the can
method to work. Try
use IO::File;
my $fh = IO::File->new("file.out", ">>")
or die "Couldn't open file: $!";
这篇关于为什么我的Perl有福的文件句柄不能用`can('print')``返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!