Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: What's the right way to write a method which returns one line at a time from a file?

by AnomalousMonk (Bishop)
on Nov 22, 2020 at 07:08 UTC ( #11124006=note: print w/replies, xml ) Need Help??


in reply to Re^2: What's the right way to write a method which returns one line at a time from a file?
in thread What's the right way to write a method which returns one line at a time from a file?

if ( my $line = readline( $self->{file_handle} ) ) { return $line; } else { return; }

I appreciate old-school, but I think the quoted code will fail to return the last line of a file if it is '0' with no terminating newline. I haven't tested it, but wouldn't

if (defined(my $line = readline( $self->{file_handle} ))) { return $line; } else { return; }
or even just
    return readline($self->{file_handle});
(readline returns undef at eof) be better?


Give a man a fish:  <%-{-{-{-<

  • Comment on Re^3: What's the right way to write a method which returns one line at a time from a file?
  • Select or Download Code

Replies are listed 'Best First'.
Re^4: What's the right way to write a method which returns one line at a time from a file?
by Cody Fendant (Friar) on Nov 28, 2020 at 03:16 UTC

    Thanks for your contribution, sorry for the late response. In my case I happen to know that will never happen but perfectly valid point. My code is working but I will improve it with your suggestion.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2021-03-03 21:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (94 votes). Check out past polls.

    Notices?