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

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

Replies are listed 'Best First'.
Re^7: Passing a file handle to a sub. I still don't get it.
by ysth (Canon) on Jun 10, 2005 at 00:54 UTC
    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.
        ^D should yield an eof, which should result in readline returning undef, not "", so the defined() wouldn't make a difference.

        A false line happens without mucking with $/ when you have a 0 at the end of a file, not followed by a newline.