Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

RE: File handles in an object

by gnat (Beadle)
on Jul 12, 2000 at 00:43 UTC ( [id://22104]=note: print w/replies, xml ) Need Help??


in reply to File handles in an object

You'll have to do two things.

(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:

$line = <$filehandle>; # simple scalar variable = read op @files = <*.c>; # anything else is a directory glob
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.

So you'll need to use a temporary variable:

$tmp = $this->{THAT}->[0]->{THE_OTHER}; $line = <$tmp>;
(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:

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
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():

sub open_file { my ($self, $filename) = @_; local *FH; open FH, $filename or die "opening $filename: $!"; $self->{FH} = *FH; }

Good luck!

Nat

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://22104]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-25 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found