in reply to File handles in an object

I got this to work:
use strict; package MyFile; ## Constructor ## sub new { my $self = {}; bless $self; } sub open_file { my $self = shift; open(FH, "textfile") || die "can't open file"; $self->{FH} = \*FH; } sub read_file { my $self = shift; my $line; my $f = $self->{FH}; while (defined ($line = <$f>)) { print "Line from file is: $line\n"; } } my $my_object = new(); $my_object->open_file(); $my_object->read_file();
I think what's happening is:
<$self->{FH}>
is being parsed like
<$self-> {FH} >
That is, a file handle specification, with a subscript outside the <>'s, followed by a >, which is a syntax error.

Alan

Replies are listed 'Best First'.
RE: RE: File handles in an object
by visnu (Sexton) on Jul 11, 2000 at 23:01 UTC
    actually, <$self->{FH}> is a glob. btrott is right in that only a simple scalar reference can be used inside angle brackets to refer to a filehandle. anything else that's more complicated is instead a glob. these will do the same thing:
    $h{foo} = '*.c'; print <$h{foo}>
    and
    print glob $h{foo};
    and also
    print <*.c>;