in reply to Re^3: 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.

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 } }

Replies are listed 'Best First'.
Re^5: Passing a file handle to a sub. I still don't get it.
by ysth (Canon) on Jun 09, 2005 at 20:20 UTC
    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; }
        One warning: when you use <> or readline in while, assigned to a simple scalar (or by default to $_), perl automatically puts a defined() around your condition, like:
        while ( defined( my $line = readline($fh) ) )
        which keeps the loop from prematurely terminating if the $line read happens to be false. This is not done for you using the OO methods.