我想将一个打开的文件句柄传递给一个方法,以便该方法可以写入该文件.
但是文件句柄似乎在对象内部关闭.
# open the file
open(MYOUTFILE, ">$dir_1/stories.txt") or die "Can't open file stories.txt"; #open for write, overwrite
print MYOUTFILE "outside"; #works
my $story = ObjectStory->new();
$story->appendToFile(\*MYOUTFILE);
close(MYOUTFILE);
Run Code Online (Sandbox Code Playgroud)
对象,应写入文件:
package ObjectStory;
# constructor
sub ObjectStory::new {
my ($class) = @_;
%hash = ();
bless \%hash, $class;
}
# addToFile method
sub ObjectStory::appendToFile {
my ($class, $MYOUTFILE) = @_;
# check if open
if (tell($MYOUTFILE) != -1) {
print $MYOUTFILE
"\n File Handle is not open!"; # File handle is always closed...
}
print $MYOUTFILE "test";
}
# the 1 is necessary, because other scripts will require this Module.
# require MyModule results in true, only if there is a 1 at the end of the module
1;
Run Code Online (Sandbox Code Playgroud)
tell仅当出现错误时,运算符才返回-1 .没有理由期望您显示的代码出现错误情况,并且它肯定不是检测文件句柄是否打开的方法.
在opened从方法IO::Handle会做你想要什么,所以你可以写
unless ($filehandle->opened) { ... }
Run Code Online (Sandbox Code Playgroud)
但请注意,您的原始代码尝试编写有关文件句柄的消息,而不是对已关闭的文件句柄打开,因此它永远不会起作用!
您将需要添加use IO::Handle到您的模块,除非您运行的是Perl 5的版本14或更高版本,它已被更改IO::File,以便IO::Handle按需加载(等等).
另请注意,不需要使用包名称为所有子例程名称添加前缀.这就是package声明的用途 - 更改默认命名空间,以便您不必这样做.
以代码的这种修改为例
package ObjectStory;
sub new {
my ($class) = @_;
my %hash;
bless \%hash, $class;
}
sub appendToFile {
my ($self, $fh) = @_;
die 'File Handle is not open!' unless $fh->opened;
print $fh "test\n";
}
1;
Run Code Online (Sandbox Code Playgroud)