我想将打开的文件句柄传递给方法,以便该方法可以写入文件。
但是文件句柄似乎在对象内部关闭。
# 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);
对象,应写入文件:
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;