in reply to Re^2: Passing a file handle to a sub. I still don't get it.
in thread Passing a file handle to a sub. I still don't get it.

What tlm neglected to mention was what you should do :)

Because <> can do two different things, readline and glob (and in your case is doing the wrong one), you should explicitly say while(readline($opts{-handle})).

Replies are listed 'Best First'.
Re^4: Passing a file handle to a sub. I still don't get it.
by mifflin (Curate) on Jun 09, 2005 at 20:17 UTC
    So, if I changed the sub to have a local $fh it would do the right thing?
    sub from_handle { my $self = shift; my %opts = @_; my $fh = $opts{-handle}; while (<$fh>) { #... and here we're supposed to read from FH } }
      Yes, that would work, but why not just use readline() directly?
        I agree. I think readline() 'reads' better to the eye anyways.
        I was just reading the perldocs on IO::File and IO::Handle and it looks like getline is the method that it uses. I think I might've used this instead...
        while ($_ = $opts{-handle}->getline()) { print; }
        or better yet...
        while (my $line = $opts{-handle}->getline()) { print $line; }