Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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 ( [id://465311]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-24 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found