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

Yes, that would work, but why not just use readline() directly?
  • Comment on Re^5: Passing a file handle to a sub. I still don't get it.

Replies are listed 'Best First'.
Re^6: Passing a file handle to a sub. I still don't get it.
by mifflin (Curate) on Jun 09, 2005 at 20:38 UTC
    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.
        Don't worry about false lines. That is only possible when he mucks with $/ or the input line is broken with ^D which is possibly entered to leave the while.