(1) As pointed out by others, <SOMETHING> is ambiguous. It's either a filehandle read or a directory glob (to get the list of files that match a pattern:
As the comments there say, the rule is: If it's a simple scalar variable, it's a filehandle. If it's a bare word it's a filehandle. Anything else and it's a directory glob.$line = <$filehandle>; # simple scalar variable = read op @files = <*.c>; # anything else is a directory glob
So you'll need to use a temporary variable:
(2) You won't be able to successfully open more than one file (even if you hadn't hard-coded the filename :-). A common hack in Perl to get filehandles in arrays or hashes or subroutines is to use typeglobs, because Perl lets you use a scalar containing a typeglob anywhere you would normally use a filehandle. However, there's some cunning required when opening the file:$tmp = $this->{THAT}->[0]->{THE_OTHER}; $line = <$tmp>;
This gives you two copies of the same glob in @filehandles. What you need to do is create a new glob each time. The best way to do this is with local():open FH, $file1 or die; # open it, okay ... push @filehandles, *FH; # push the typeglob, fine ... open FH, $file2 or die; # reopening it, closing the previously open +ed file! push @filehandles, *FH; # push it again
sub open_file { my ($self, $filename) = @_; local *FH; open FH, $filename or die "opening $filename: $!"; $self->{FH} = *FH; }
Good luck!
Nat
In reply to RE: File handles in an object
by gnat
in thread File handles in an object
by sluap
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |