How can I store and access a filehandle in a Perl class?
Posted
by Haiyuan Zhang
on Stack Overflow
See other posts from Stack Overflow
or by Haiyuan Zhang
Published on 2010-06-12T05:28:53Z
Indexed on
2010/06/12
12:03 UTC
Read the original article
Hit count: 199
perl
|filehandle
please look at the following code first.
#! /usr/bin/perl
package foo;
sub new {
my $pkg = shift;
my $self = {};
my $self->{_fd} = undef;
bless $self, $pkg;
return $self;
}
sub Setfd {
my $self = shift;
my $fd = shift;
$self_->{_fd} = $fd;
}
sub write {
my $self = shift;
print $self->{_fd} "hello word";
}
my $foo = new foo;
My intention is to store a file handle within a class using hash. the file handle is undefined at first, but can be initilized afterwards by calling Setfd function. then write can be called to actually write string "hello word" to a file indicated by the file handle, supposed that the file handle is the result of a success "write" open.
but, perl compiler just complains that there are syntax error in the "print" line. can anyone of you tells me what's wrong here? thanks in advance.
© Stack Overflow or respective owner